【问题标题】:One Category Per Order Woocomerce每个订单一个类别 Woocommerce
【发布时间】:2020-06-22 10:51:04
【问题描述】:

我正在尝试将我的 Woocommerce 商店中的客户限制为一次只能从 1 个类别订购。 我正在尝试的代码只是出于某种原因限制了一切。

function is_product_the_same_cat($valid, $product_id, $quantity) {
global $woocommerce;
// start of the loop that fetches the cart items
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
    $_product = $values['data'];
    $terms = get_the_terms( $_product->id, 'product_cat' );
    $target_terms = get_the_terms( $product_id, 'product_cat' ); //get the current items
    foreach ($terms as $term) {
        $cat_ids[] = $term->term_id;  //get all the item categories in the cart
    }
    foreach ($target_terms as $term) {
        $target_cat_ids[] = $term->term_id; //get all the categories of the product
    }           
}
$same_cat = array_intersect($cat_ids, $target_cat_ids); //check if they have the same category
if(count($same_cat) > 0) return $valid;
else {
    wc_add_notice( 'This product is in another category!', 'error' );
    return false;
}
}
add_filter( 'woocommerce_add_to_cart_validation', 'is_product_the_same_cat',10,3);

我并没有尝试将其限制为每个类别 1 个产品,而是尝试限制它们,以便他们每个订单只能从 1 个类别中订购产品。例如,一旦他们将“糖果”类别的产品添加到购物篮中,他们就无法添加“糖果”以外的任何其他类别的产品。

谢谢!

【问题讨论】:

    标签: wordpress woocommerce cart


    【解决方案1】:

    假设每个产品只包含一个类别

    function filter_woocommerce_add_to_cart_validation( $passed, $product_id, $quantity, $variation_id = null, $variations = null ) {
        // If passed
        if ( $passed ) {
            
            // If cart is NOT empty when a product is added     
            if ( !WC()->cart->is_empty() ) {
                
                // Set vars
                $current_product_category_ids = array();
                $in_cart_product_category_ids = array();
                
                // Get current product categories via product_id
                $current_product_category_ids = wc_get_product_term_ids( $product_id, 'product_cat' );
    
                // Loop through cart items checking for product categories
                foreach ( WC()->cart->get_cart() as $cart_item ) {
                    // Get product categories from product in cart via cart item product id
                    $in_cart_product_category_ids = array_merge( $in_cart_product_category_ids, wc_get_product_term_ids( $cart_item['product_id'], 'product_cat' ) );
                }
                
                // Removes duplicate values
                $in_cart_product_category_ids = array_unique( $in_cart_product_category_ids, SORT_NUMERIC );
                
                // Compare
                $compare = array_diff( $current_product_category_ids, $in_cart_product_category_ids );
                
                // Result is NOT empty
                if ( !empty ( $compare ) ) {
                    wc_add_notice( 'This product is in another category!', 'error' );
                    $passed = false;
                }
            }
        }
    
        return $passed;
    }
    add_filter( 'woocommerce_add_to_cart_validation', 'filter_woocommerce_add_to_cart_validation', 10, 5 );
    

    【讨论】:

    • 嗯,好点子。产品确实有多个类别。在排除项中我只需要 3 个类别,有没有办法定义这 3 个类别?如果做不到这一点,有没有办法可以为此使用 ACF 字段?
    • 是的,如果修改了这段代码,这当然是可能的。我可以指出,我们在堆栈溢出时一次问 1 个问题。当您的问题得到解答时,您会提出 2 个新问题。
    • 标记为正确,因为它适用于类别。现在创建的新问题询问自定义分类法:stackoverflow.com/questions/62554327/…。感谢您的帮助。
    猜你喜欢
    • 2022-10-14
    • 2019-08-29
    • 2015-07-16
    • 2018-04-10
    • 2017-03-26
    • 2014-02-03
    • 2018-06-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多