简体   繁体   English

避免在WooCommerce中添加来自不同产品类别的购物车产品

[英]Avoid add to cart products from different product categories in WooCommerce

On WooCommerce, I have tried " Restricting cart items to be from the same product category " answer code, and it works. 在WooCommerce上,我尝试了限制购物车商品来自同一产品类别 ”的回答代码,并且它可以正常工作。 But if the user adds the product from the product page, the product will be in the cart. 但是,如果用户从产品页面添加产品,则该产品将在购物车中。

Any advice or help please? 有什么建议或帮助吗?

Because woocommerce_add_to_cart_validation hook is located on both WC_Cart and WC_Ajax add_to_cart() methods, so it's triggered when a product is added to cart via ajax or normally via single product pages… So the code works in all cases on add to cart event. 因为woocommerce_add_to_cart_validation钩子同时位于WC_Cart和WC_Ajax add_to_cart()方法上,所以当通过ajax或通常通过单个产品页面将产品添加到购物车时会触发它。因此,在添加到购物车事件中,代码在所有情况下均适用。


Handling parent product categories too 也处理父产品类别

Now " Restricting cart items to be from the same product category in WooCommerce " doesn't handle parent product categories, as WordPress has_term() conditional function doesn't handle parent terms, so parent product categories. 现在, 在WooCommerce中将购物车商品限制为来自同一产品类别 ”将无法处理父产品类别,因为WordPress的has_term()条件函数无法处理父条款,因此父产品类别也无法处理。

To make it work with parent product categories too, you will need something more elaborate: 为了使其也能与父产品类别一起使用,您将需要更详细的说明:

add_filter( 'woocommerce_add_to_cart_validation', 'custom_checking_product_added_to_cart', 10, 3 );
function custom_checking_product_added_to_cart( $passed, $product_id, $quantity) {

    // HERE your alert text message
    $message = __( 'MY ALERT MESSAGE.', 'woocommerce' );

    if( ! WC()->cart->is_empty() ) {
        $term_ids = array(); // Initializing

        // Loop through product category WP_Term objects set for the current the product
        foreach( wp_get_post_terms( $product_id, 'product_cat') as $term ) {
            $terms_ids[$term->term_id] = $term->term_id; // Add the term ID to the array

            // Add the parent term ID to the array, if it exist
            if( $term->parent > 0 )
                $terms_ids[$term->parent] = $term->parent; 
        }

        // Loop through cart items
        foreach (WC()->cart->get_cart() as $cart_item ){
            if( ! has_product_categories( $product_id, $term_ids ) ) {
                $passed = false;
                wc_add_notice( $message, 'error' );
                break;
            }
        }
    }
    return $passed;
}

// Custom conditional function that handle parent product categories too
function has_product_categories( $product_id, $categories ) {
     // Initializing
    $parent_term_ids = $categories_ids = array();
    $taxonomy        = 'product_cat';

    // Convert categories term names and slugs to categories term ids
    foreach ( $categories as $category ){
        if( is_numeric( $category ) ) {
            $categories_ids[] = (int) $category;
        } elseif ( term_exists( sanitize_title( $category ), $taxonomy ) ) {
            $categories_ids[] = get_term_by( 'slug', sanitize_title( $category ), $taxonomy )->term_id;
        }
    }

    // Loop through the current product category terms to get only parent main category term
    foreach( get_the_terms( $product_id, $taxonomy ) as $term ){
        if( $term->parent > 0 ){
            $parent_term_ids[] = $term->parent; // Set the parent product category
            $parent_term_ids[] = $term->term_id; // (and the child)
        } else {
            $parent_term_ids[] = $term->term_id; // It is the Main category term and we set it.
        }
    }
    return array_intersect( $categories_ids, array_unique($parent_term_ids) ) ? true : false;
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file. 代码在您的活动子主题(或主题)的function.php文件中,或者在任何插件文件中。

Code is tested and it works 代码经过测试,可以正常工作


Restricting cart items to be only from different product categories 限制购物车商品仅来自不同的产品类别

In the first function, replace: 在第一个函数中,替换为:

if( ! has_product_categories( $cart_item['product_id'], $term_ids ) ) {

by 通过

if( has_product_categories( $cart_item['product_id'], $term_ids ) ) {

Related: Restricting cart items to be from the same product category in WooCommerce 相关: 在WooCommerce中将购物车项目限制为来自同一产品类别

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果用户未登录 Woocommerce,请避免将特定产品类别添加到购物车 - Avoid add to cart for specific product categories if user is unlogged in Woocommerce 为 WooCommerce 中特定产品类别的购物车项目自动添加产品 - Auto add a product for cart item from specific product categories in WooCommerce Woocommerce添加到购物车产品仅限4种特定产品 - Woocommerce Add to cart product Restriction to 4 specific products 避免 Woocommerce 中特定产品类别的购物车项目价格更新 - Avoid cart items price update for specific product categories in Woocommerce 隐藏产品价格并禁用 Woocommerce 中特定产品类别的添加到购物车 - Hide product price and disable add to cart for specific product categories in Woocommerce 自动将产品添加到购物车,部分 WooCommerce 产品类别除外 - Auto add product to cart except for some WooCommerce product categories 当2个不同类别的产品时,更改woocommerce购物车页面中的“数量”文本 - Change the “Quantity” text in the woocommerce cart page when 2 products from different categories Woocommerce:产品页面其他分类的产品(交叉产品) - Woocommerce: products from other categories on the product page (cross products) WooCommerce正在显示不同类别的产品 - WooCommerce is displaying Products from different categories Ajax上的JS警报添加到购物车中Woocommerce中多个产品类别的计数 - JS alert on ajax add to cart for multiple product categories count in Woocommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM