这是我在尽力开一家餐厅时的总体想法。这里有两道菜——“今天的特别菜”和“今天的绿色替代菜”。
如果这两者中的任何一个被添加到购物车中,顾客通常会来餐厅吃饭/取餐后离开。
但是。。。
如果顾客点了10份或更多这两份中的任何一份,或者加在一起算10份,餐厅将在同一城市/当地提供免费送货服务。
那么,怎么做呢?
-
两个产品ID。他们的数量可以在结账时更改。
-
如果签出时其数量发生更改,则签出会自动更新自身
-
如果其中一个或两个产品的数量为10(或更多),请取消设置字段
-
删除字段时,将其设为非必需字段=避免出现以下错误:
-
“你没有填写…”
-
如果其中一个或两个加在一起的数量小于10,则带回字段
-
如果出现nr 6,所有字段显然应恢复正常(必需等)
这就是我正在处理的内容:
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;
}
我试着理解是否有使用JavaScript(或jQuery)的选项,但我不太熟练。如果它们的数量很好,只需删除它们,这就是在签出根据产品数量变化进行更新时来回“删除和添加”字段的问题。
任何帮助都将不胜感激。