我试图将 woocommerce 限制为仅以 5、10 或 15 的固定数量进行销售。 下面的代码片段(我在这个论坛上找到的)允许我将最小数量设置为 5,但我想知道是否有人可以建议是否可以将其修改为允许 5、10 或 15。
感谢您提供的任何帮助。
add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
function wc_minimum_order_amount() {
global $woocommerce;
$minimum = 5;
if ( $woocommerce->cart->cart_contents_count < $minimum ) {
$woocommerce->add_error( sprintf( 'Cards can only be purchased in multiples of %s Please ammend your order.' , $minimum ) );
}
}
最佳答案
感谢 Gareth 的回答,但我实际上找到了一种更好的方法来实现我的需要,只是发布以防其他人需要它。
我发现最好限制购物车以 5 的倍数出售,这可以通过以下代码来实现。
// check that cart items quantities totals are in multiples of 5
add_action( 'woocommerce_check_cart_items', 'woocommerce_check_cart_quantities' );
function woocommerce_check_cart_quantities() {
global $woocommerce;
$multiples = 5;
$total_products = 0;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$total_products += $values['quantity'];
}
if ( ( $total_products % $multiples ) > 0 )
$woocommerce->add_error( sprintf( __('You need to buy in quantities of %s products', 'woocommerce'), $multiples ) );
}
关于Woocommerce 订单数量的倍数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20175748/