Woocommerce - 仅为购物车中具有特定属性的产品设置最小数量和步骤

问题描述 投票:0回答:0

我在一个销售葡萄酒的 woocommerce 网站上工作。我只想为具有特定属性的产品设置多次购买价值(步骤)。

每件产品均以瓶装形式出售,容量为 1 升、2 升或 3 升。所以“瓶子”属性 (=>pa_bottle) 有 3 个术语:1 升、2 升、3 升。

我希望当属性为“1 升”的项目总数是 3 的倍数时,购物车被视为有效。

例如这个购物车是有效的:

  • 1 x Prosecco 葡萄酒 - 1 升
  • 2 x Prosecco 葡萄酒 - 3 升
  • 2 x 香槟酒 - 1升

我找到了一个非常适合类别的脚本。我想要一个适用于该属性的类似脚本。

这是原剧本:

add_action( 'woocommerce_check_cart_items', 'woocommerce_check_cart_quantities' );
function woocommerce_check_cart_quantities() {
$multiples = 3;
$total_products = 0;
$category_ids = array( 74 );
$found = false;

foreach ( WC()->cart->get_cart() as $cart_item ) {
    if ( has_product_category( $cart_item['product_id'], $category_ids ) ) {
        
        $total_products += $cart_item['quantity'];
        $found = true;
    }
}
if ( ( $total_products % $multiples ) > 0 && $found )
    wc_add_notice( sprintf( __('You need to buy in quantities of %s products', 'woocommerce'), $multiples ), 'error' );
}

这里我尝试替换脚本

if ( has_product_category( $cart_item['product_id'], $category_ids ) ) {

与脚本

if ( has_term( '1liter', 'pa_bottle', $cart_item['product_id'] ) ) {

但它不起作用,因为它设置了购物车中所有产品的步长值,而我希望它只设置在具有 1 升属性的产品上。

谢谢你的帮助。

woocommerce attributes cart product-quantity step
© www.soinside.com 2019 - 2024. All rights reserved.