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

标签 wordpress woocommerce product cart checkout

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

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

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

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

基于Prevent WooCommerce checkout if minimum quantity for a specific category is not reached答案代码,这是我的尝试:

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 );

最佳答案

在当前循环中结束执行还不够,还需要在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 );

关于wordpress - 如果未达到某个类别的最小数量,则阻止 WooCommerce 结帐,除非添加另一个类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71265569/

相关文章:

php - 检查产品类别是否为顶级或没有子类别

php - 产品 "read more"按钮在 woocommerce 商店中不起作用

mysql - WordPress 自定义搜索

css - 由于产品标题,商店产品未对齐

list - Haskell 中的列表元素相乘

php - 从 Woocommerce 商店页面中的特定自定义元数据过滤产品

htaccess 中附加域的 Wordpress SSL 问题

mysql - 如何从大型 MySQL 数据库中导出数千个表

php - 将 PHP 版本更改为 8.1,但 WordPress 认为我们正在使用版本 7.4?

php - Woocommerce 购物车页面添加额外的 <p> 标签