我用WooCommerce
制作的WooCommerce
卖12顿现成的饭菜。顾客也只能购买每种类型的一餐。但它必须达到以下精确之和:
5件,12件,24件,36件。
为什么?
准备好的食物是用恒温箱运送的,里面有精确数量的即食。
有四种类型的等温盒,它们包含:
因此,我不得不在购物车页面的结账处强制购买5、12、24或36顿饭。
这个数字也可以通过添加2餐A+3餐B来达到。
重要的是,例如,如果他们试图购买63A+ 3B产品,结账按钮就消失了,报警器就会显示:
你只能购买5,12,24,36顿饭。
发布于 2020-02-15 12:52:58
function checkout_validate() {
// Only run in the Cart or Checkout pages
if( is_cart() || is_checkout() ) {
// Get number of items in the cart.
$items_in_cart = WC()->cart->get_cart_contents_count();
// Allowed amounts
$allowed_amounts = array(5, 12, 24, 36);
// If items in cart NOT equal to allowed amounts, show error message
if ( !in_array( $items_in_cart, $allowed_amounts)) {
wc_add_notice( __( 'Remember that you can only buy 5, 12, 24 or 36 meals. You have ' . $items_in_cart . ' maeals in your cart.', 'woocommerce' ), 'error' );
remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
}
}
}
add_action( 'woocommerce_check_cart_items' , 'checkout_validate' );
https://stackoverflow.com/questions/60238522
复制相似问题