将总购物车数量限制为 WooCommerce 中的特定倍数
Restrict total cart quantity to specific multiples in WooCommerce
我希望客户购物车中的产品数量是 6 或 12 或 4 的倍数。我在论坛中找到了乘数 6 的代码,但需要针对其他系数进行编辑。
基于代码线程:
// check that cart items quantities totals are in multiples of 6
add_action( 'woocommerce_check_cart_items', 'woocommerce_check_cart_quantities' );
function woocommerce_check_cart_quantities() {
$multiples = 6;
$total_products = 0;
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$total_products += $values['quantity'];
}
if ( ( $total_products % $multiples ) > 0 )
wc_add_notice( sprintf( __('You need to buy in quantities of %s products', 'woocommerce'), $multiples ), 'error' );
}
基于Woocommerce Order Quantity in Multiples答案代码:
// Limit cart items with a certain shipping class to be purchased in multiple only
add_action( 'woocommerce_check_cart_items', 'woocommerce_check_cart_quantities_for_class' );
function woocommerce_check_cart_quantities_for_class() {
$multiples = 6;
$class = 'bottle';
$total_products = 0;
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$product = get_product( $values['product_id'] );
if ( $product->get_shipping_class() == $class ) {
$total_products += $values['quantity'];
}
}
if ( ( $total_products % $multiples ) > 0 )
wc_add_notice( sprintf( __('You need to purchase bottles in quantities of %s', 'woocommerce'), $multiples ), 'error' );
}
如何处理 6、12 或 4 的倍数而不是一个?
由于 12 已经是 4 或 6 的倍数,您只需使用以下代码检查 4 或 6 的倍数项 (已注释):
add_action( 'woocommerce_check_cart_items', 'check_cart_items_quantities_conditionally' );
function check_cart_items_quantities_conditionally() {
$multiples = array(4, 6); // Your defined multiples in an array
$items_count = WC()->cart->get_cart_contents_count(); // Get total items cumulated quantity
$is_multiple = false; // initializing
// Loop through defined multiples
foreach ( $multiples as $multiple ) {
if ( ( $items_count % $multiple ) == 0 ) {
$is_multiple = true; // When a multiple is found, flag it as true
break; // Stop the loop
}
}
// If total items cumulated quantity doesn't match with any multiple
if ( ! $is_multiple ) {
// Display a notice avoiding checkout
wc_add_notice( sprintf( __('You need to buy in quantities of %s products', 'woocommerce'), implode(' or ', $multiples) ), 'error' );
}
}
我希望客户购物车中的产品数量是 6 或 12 或 4 的倍数。我在论坛中找到了乘数 6 的代码,但需要针对其他系数进行编辑。
基于
// check that cart items quantities totals are in multiples of 6
add_action( 'woocommerce_check_cart_items', 'woocommerce_check_cart_quantities' );
function woocommerce_check_cart_quantities() {
$multiples = 6;
$total_products = 0;
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$total_products += $values['quantity'];
}
if ( ( $total_products % $multiples ) > 0 )
wc_add_notice( sprintf( __('You need to buy in quantities of %s products', 'woocommerce'), $multiples ), 'error' );
}
基于Woocommerce Order Quantity in Multiples答案代码:
// Limit cart items with a certain shipping class to be purchased in multiple only
add_action( 'woocommerce_check_cart_items', 'woocommerce_check_cart_quantities_for_class' );
function woocommerce_check_cart_quantities_for_class() {
$multiples = 6;
$class = 'bottle';
$total_products = 0;
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$product = get_product( $values['product_id'] );
if ( $product->get_shipping_class() == $class ) {
$total_products += $values['quantity'];
}
}
if ( ( $total_products % $multiples ) > 0 )
wc_add_notice( sprintf( __('You need to purchase bottles in quantities of %s', 'woocommerce'), $multiples ), 'error' );
}
如何处理 6、12 或 4 的倍数而不是一个?
由于 12 已经是 4 或 6 的倍数,您只需使用以下代码检查 4 或 6 的倍数项 (已注释):
add_action( 'woocommerce_check_cart_items', 'check_cart_items_quantities_conditionally' );
function check_cart_items_quantities_conditionally() {
$multiples = array(4, 6); // Your defined multiples in an array
$items_count = WC()->cart->get_cart_contents_count(); // Get total items cumulated quantity
$is_multiple = false; // initializing
// Loop through defined multiples
foreach ( $multiples as $multiple ) {
if ( ( $items_count % $multiple ) == 0 ) {
$is_multiple = true; // When a multiple is found, flag it as true
break; // Stop the loop
}
}
// If total items cumulated quantity doesn't match with any multiple
if ( ! $is_multiple ) {
// Display a notice avoiding checkout
wc_add_notice( sprintf( __('You need to buy in quantities of %s products', 'woocommerce'), implode(' or ', $multiples) ), 'error' );
}
}