Woocommerce: Create dropdown field and filter items of it by categories in cart
Question
I created a cutom field for the Woocommerce check out page with several items:
add_filter( 'woocommerce_after_order_notes', 'haendler_location_checkout_field' );
function haendler_location_checkout_field( $checkout ) {
woocommerce_form_field( 'haendler_location', array(
'type' => 'select',
'class' => array('my-field-class form-row-wide'),
'label' => __('Händler', 'woocommerce'),
'required' => true,
'options' => array(
'' => __('Bitte wählen Sie einen Händler aus', 'woocommerce' ),
'ACC' => __('ACC KG (Hamburg) VAN', 'woocommerce' ),
'SOMMERDAMM' => __('Autohaus Sommerdamm (Worms) VAN', 'woocommerce' ),
'EMR' => __('EMR Campers (Stuttgart) WOMO', 'woocommerce' ),
'THOMA' => __('Eurocar Thoma (Dueren) WOMO', 'woocommerce' ),
'WVS' => __('WVS Wohnmobilvermietung (Saarlouis) VAN & WOMO', 'woocommerce' ),
'OWL' => __('Wohnmobil Zentrum OWL (Verl) VAN & WOMO', 'woocommerce' ),
)
), $checkout->get_value( 'haendler_location' ));
}
Now I would like to show only special items depending on catagory of the products in the cart:
- retailers which sell vans or vans & womos if only vans are in the cart
- retailers which sell womos or womos & vans if only womos are in the cart
- retailers which sell womos & van if vans and womos are in the cart
I tried a lot of variations in the code to realize this but without success. So I hope somebody can helpt me. It would be great.
My last try is someting like this:
add_action( 'woocommerce_after_order_notes', 'get_cat' );
function get_cat( $cat ) {
global $woocommerce;
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
$cat = wc_get_product_category_list( $cart_item['product_id'] );
}
}
// Add the custom checkout field
add_filter( 'woocommerce_after_order_notes', 'haendler_location_checkout_field' );
function haendler_location_checkout_field( $checkout ) {
if($cat == 'Van') {
$van = array(
'dealer2' => " 'NAUTIC' => __('Natic Campers (Vans)', 'woocommerce') ",
'dealer1' => " 'EMR' => __('EMR Campers (Vans & Womos)', 'woocommerce') ",
);
$options_van = implode(', ', $van);
}
if($cat == 'Womo') {
$van = array(
'dealer3' => " 'HOLIDY' => __('Holidy Camper (Womos)', 'woocommerce') ",
'dealer1' => " 'EMR' => __('EMR Campers (Vans & Womos)', 'woocommerce') ",
);
$options_van = implode(', ', $van);
}
else {
$van = array(
'dealer1' => " 'EMR' => __('EMR Campers (Vans & Womos)', 'woocommerce') ",
);
$options_van = implode(', ', $van);
};
woocommerce_form_field( 'haendler_location', array(
'type' => 'select',
'class' => array('my-field-class form-row-wide'),
'label' => __('Händler', 'woocommerce'),
'required' => true,
'options' => array(
$options_van
)
),
$checkout->get_value( 'haendler_location' ));
}
What is wrong? Has someone an idea?
0
customization, dropdown, filters, woocommerce
3 years
2020-08-20T06:10:46-05:00
2020-08-20T06:10:46-05:00 0 Answers
51 views
0
Leave an answer