如果未达到某个类别的最低数量,则阻止 WooCommerce 结帐,除非添加另一个类别

发布于 2025-01-10 02:54:39 字数 1967 浏览 0 评论 0原文

我正在购物车中进行检查,以应用一条规则:如果添加冷藏类别中的商品,则至少需要 3 件冷藏类别商品才能结账。 - 这有效。

但是,如果还添加了捆绑类别中的商品,则不应强制执行上述冷藏规则。

例如,至少需要 3 件冷藏类别商品,除非购物车中有捆绑商品,在这种情况下,请忽略冷藏规则。

我至少有 3 个冷冻规则有效,但如果检测到捆绑类别中的项目,我无法获取排除此规则的代码?

基于如果未达到特定类别的最低数量,则阻止 WooCommerce 结帐答案代码,这是我的尝试:

function action_woocommerce_check_cart_items() {
    // Only run on the cart or checkout pages
    if ( is_cart() || is_checkout() ) {     
        // Minimum
        $minimum = 3;
        
        // Category
        $category = 'chilled';
        $category2 = 'bundles';
        
        // Initialize
        $total = 0;
        
        // Loop through cart items        
        foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {        
            // Product id
            $product_id = $cart_item['product_id'];

            // Has certain category
            if ( has_term( $category, 'product_cat', $product_id ) ) {              
                // Add to total
                $total += $cart_item['quantity'];
            }elseif (has_term ($category2, 'product_cat', $product_id)) {
                break;
                
            }
        }
        
        // When total is greater than 0 but less than the minimum
        if ( $total > 0 && $total < $minimum ) {
            // Notice
            wc_add_notice( sprintf( __( '<strong>A minimum of %s products are required from the CHILLED category before checking out.</strong>', 'woocommerce' ), $minimum ), 'error' );
            
            // Optional: remove proceed to checkout button
            remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
        }
    }
}   
add_action( 'woocommerce_check_cart_items' , 'action_woocommerce_check_cart_items', 10, 0 );

I am doing a check in the cart to apply a rule that if a item from the chilled category is added, a minimum of 3 chilled category items are required to check out. - This works.

However, if an item from the bundles category is also added, then the above chilled rule should not be enforced.

e.g. Minimum of 3 chilled category items required, unless a bundle item is in the cart, in which case, disregard the chilled rule.

I have the minimum 3 chilled rule working, but I can't get the code to exclude this rule if a item from the bundles category is detected?

Based on Prevent WooCommerce checkout if minimum quantity for a specific category is not reached answer code, this is my attempt:

function action_woocommerce_check_cart_items() {
    // Only run on the cart or checkout pages
    if ( is_cart() || is_checkout() ) {     
        // Minimum
        $minimum = 3;
        
        // Category
        $category = 'chilled';
        $category2 = 'bundles';
        
        // Initialize
        $total = 0;
        
        // Loop through cart items        
        foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {        
            // Product id
            $product_id = $cart_item['product_id'];

            // Has certain category
            if ( has_term( $category, 'product_cat', $product_id ) ) {              
                // Add to total
                $total += $cart_item['quantity'];
            }elseif (has_term ($category2, 'product_cat', $product_id)) {
                break;
                
            }
        }
        
        // When total is greater than 0 but less than the minimum
        if ( $total > 0 && $total < $minimum ) {
            // Notice
            wc_add_notice( sprintf( __( '<strong>A minimum of %s products are required from the CHILLED category before checking out.</strong>', 'woocommerce' ), $minimum ), 'error' );
            
            // Optional: remove proceed to checkout button
            remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
        }
    }
}   
add_action( 'woocommerce_check_cart_items' , 'action_woocommerce_check_cart_items', 10, 0 );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽袖吟 2025-01-17 02:54:39

在当前循环中结束执行还不够,还需要在 if 条件中添加额外的规则。

所以你得到:

function action_woocommerce_check_cart_items() {
    // Only run on the cart or checkout pages
    if ( is_cart() || is_checkout() ) {     
        // Minimum
        $minimum = 3;
        
        // Category
        $category = 'chilled';
        $category_2 = 'bundles';
        
        // Initialize
        $total = 0;
        $flag = true;
        
        // Loop through cart items        
        foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {        
            // Product id
            $product_id = $cart_item['product_id'];

            // Has certain category
            if ( has_term( $category, 'product_cat', $product_id ) ) {              
                // Add to total
                $total += $cart_item['quantity'];
            // Has other category
            } elseif ( has_term( $category_2, 'product_cat', $product_id ) ) { 
                // Break loop
                $flag = false;
                break;
            }
        }
        
        // When total is greater than 0 but less than the minimum & flag is still true
        if ( ( $total > 0 && $total < $minimum ) && $flag ) {
            // Notice
            wc_add_notice( sprintf( __( 'A minimum of %s products are required from the %s category before checking out.', 'woocommerce' ), $minimum, $category ), 'error' );
            
            // Optional: remove proceed to checkout button
            remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
        }
    }
}   
add_action( 'woocommerce_check_cart_items' , 'action_woocommerce_check_cart_items', 10, 0 );

End the execution in the current loop will not suffice, you also need to add an extra rule to the if condition.

So you get:

function action_woocommerce_check_cart_items() {
    // Only run on the cart or checkout pages
    if ( is_cart() || is_checkout() ) {     
        // Minimum
        $minimum = 3;
        
        // Category
        $category = 'chilled';
        $category_2 = 'bundles';
        
        // Initialize
        $total = 0;
        $flag = true;
        
        // Loop through cart items        
        foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {        
            // Product id
            $product_id = $cart_item['product_id'];

            // Has certain category
            if ( has_term( $category, 'product_cat', $product_id ) ) {              
                // Add to total
                $total += $cart_item['quantity'];
            // Has other category
            } elseif ( has_term( $category_2, 'product_cat', $product_id ) ) { 
                // Break loop
                $flag = false;
                break;
            }
        }
        
        // When total is greater than 0 but less than the minimum & flag is still true
        if ( ( $total > 0 && $total < $minimum ) && $flag ) {
            // Notice
            wc_add_notice( sprintf( __( 'A minimum of %s products are required from the %s category before checking out.', 'woocommerce' ), $minimum, $category ), 'error' );
            
            // Optional: remove proceed to checkout button
            remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
        }
    }
}   
add_action( 'woocommerce_check_cart_items' , 'action_woocommerce_check_cart_items', 10, 0 );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文