【问题标题】:Allow only one product category in cart at once in Woocommerce在 Woocommerce 中一次只允许购物车中的一个产品类别
【发布时间】:2019-03-02 13:51:16
【问题描述】:

如何将 Woocommerce 购物车配置为一次只允许一种产品类别类型?

【问题讨论】:

    标签: php wordpress woocommerce cart custom-taxonomy


    【解决方案1】:

    以下代码将允许仅将来自一个产品类别的商品添加到购物车,避免添加到购物车并显示自定义通知:

    add_filter( 'woocommerce_add_to_cart_validation', 'only_one_product_category_allowed', 20, 3 );
    function only_one_product_category_allowed( $passed, $product_id, $quantity) {
    
        // Getting the product categories term slugs in an array for the current product
        $term_slugs   = wp_get_post_terms( $product_id, 'product_cat', array('fields' => 'slugs') );
    
        // Loop through cart items
        foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item ){
    
            // Check if the product category of the current product don't match with a cart item
            if( ! has_term( $term_slugs, 'product_cat', $cart_item['product_id'] ) ){
    
                // Displaying a custom notice
                wc_add_notice( __('Only items from one product category are allowed in cart'), 'error' );
    
                // Avoid add to cart
                return false; // exit
            }
        }
        return $passed;
    }
    

    代码进入活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。


    添加(更新) - 相同,但仅适用于父产品类别

    add_filter( 'woocommerce_add_to_cart_validation', 'only_one_product_category_allowed', 20, 3 );
    function only_one_product_category_allowed( $passed, $product_id, $quantity) {
        $parent_term_ids = $item_parent_term_ids = array(); // Initializing
    
        // Loop through the current product category terms to get only parent main category term
        foreach( get_the_terms( $product_id, 'product_cat' ) as $term ){
            if( $term->parent > 0 ){
                $parent_term_ids[] = $term->parent; // Set the parent product category
            }
        }
    
        // Loop through cart items
        foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item ){
            // Loop through the cart item product category terms to get only parent main category term
            foreach( get_the_terms( $cart_item['product_id'], 'product_cat' ) as $term ){
                if( $term->parent > 0 ){
                    $item_parent_term_ids[] = $term->parent; // Set the parent product category
                }
            }
    
            // Check if parent product categories don't match
            if( ! array_intersect( $parent_term_ids, $item_parent_term_ids ) ){
    
                // Displaying a custom notice
                wc_add_notice( __('Only items from one product category are allowed in cart'), 'error' );
    
                // Avoid add to cart
                return false; // exit
            }
        }
        return $passed;
    }
    

    代码进入活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。

    【讨论】:

    • 有没有办法让它在父类别而不是嵌套类别中起作用?
    • 非常感谢!很抱歉我把它漏掉了。直到我测试了这段代码才意识到这一点。
    • 只有一个子类别级别。产品 - > 嵌套产品类型(衬衫、袜子等)和工作表(无嵌套类别)
    • @IsaacByrne 我在之后添加了一些代码……检查它是否是您需要的。
    • 完美运行!再次感谢!
    猜你喜欢
    • 2017-09-27
    • 1970-01-01
    • 1970-01-01
    • 2017-03-26
    • 1970-01-01
    • 2019-10-20
    • 2019-05-01
    • 1970-01-01
    • 2023-04-05
    相关资源
    最近更新 更多