This is the general idea with this as I am trying my best to start up a restaurant. There are two dishes - "The Today's Special" and the "The Today's Green Alternative".
If any of those two are added to cart, the customer usually comes to the restaurant to eat / pick their order up and leave.
BUT... If the customer orders 10 or more of any of these two - or ten counting them both together, the restaurant offers FREE DELIVERY within the same city / local area.
So, how?
- TWO product ID’s. Their QTY can be changed on checkout.
- If on checkout and their QTY is changed, the checkout auto-updates itself
- If one or both of these products make up a QTY of 10 (or more), unset the fields
- When removing the fields, make them non-required = avoid errors such as:
- "You did not fill out..."
- If the QTY of one or them both together is less than 10, bring back the fiedls
- If nr 6 happens, all the fields should obviously go back to normal (required etc)
This is what I am working with:
add_filter( 'woocommerce_checkout_fields' , 'ls_unset_checkout_fields_based_on_qty_or_id',10,1);
function ls_unset_checkout_fields_based_on_qty_or_id( $fields ) {
// this should be based on a certain amount of product ID:s, example as below
$targeted_ids = array ( 100, 200 );
// the removal of fields = if target ID:s are in the cart and the QTY of either one of them is
// less than 10, remove the fields as selected below
$minimum_quantity = 10;
// flag = initialize?
$flag = false;
// check if these conditions are met
foreach ( WC()->cart->get_cart_item_quantities() as $product_id => $cart_item_quantity ) {
if ( in_array( $product_id, $targeted_ids ) ) {
if ( $cart_item_quantity >= $minimum_quantity ) {
$flag = true;
break;
}
}
}
// if both target ID:s have a minimum of 10 (qty), remove the fields below and make them non-required
// this would leave us with First name, Last name, Email, Phone
// if the QTY of any of the two target ID:s are changed to below 10 while on checkout, add back all the fields
if ( $flag ) {
unset( $fields['shipping']['shipping_country'] );
unset( $fields['shipping']['shipping_state'] );
unset( $fields['billing']['billing_country'] );
unset( $fields['billing']['billing_state'] );
unset( $fields['shipping']['shipping_company'] );
unset( $fields['shipping']['shipping_country'] );
unset( $fields['shipping']['shipping_address_1'] );
unset( $fields['shipping']['shipping_address_2'] );
unset( $fields['shipping']['shipping_city'] );
unset( $fields['shipping']['shipping_state'] );
unset( $fields['shipping']['shipping_postcode'] );
unset( $fields['billing']['billing_address_1'] );
unset( $fields['billing']['billing_address_2'] );
unset( $fields['billing']['billing_city'] );
unset( $fields['billing']['billing_postcode'] );
unset( $fields['billing']['billing_country'] );
unset( $fields['billing']['billing_state'] );
unset( $fields['billing']['billing_company'] );
}
return $fields;
}
Tried understanding if there's an option to use JavaScript (or jQuery), but I am not so skilled. Just removing them if their QTY is fine, it is the problem of "removing and adding" fields back and forth as the checkout updates itself based on product QTY changes.
Any help is appreciated.