限制购物车商品必须来自WooCommerce中的同一产品类别 [英] Restricting cart items to be from the same product category in WooCommerce

查看:60
本文介绍了限制购物车商品必须来自WooCommerce中的同一产品类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当购物车中添加了带有特殊产品类别"cat_x"的商品并显示一些不同的自定义通知时,我正在使用以下代码从购物车中删除其他WooCommerce产品类别商品.该代码来自 该线程 ,并且可以正常工作好吧:

I am using the code below to remove other WooCommerce product category items from the cart when there is an item with a special product category 'cat_x' added in cart and display some different custom notices. The code came from this thread and just works well:

add_action( 'woocommerce_check_cart_items', 'checking_cart_items' );
function checking_cart_items() {
    $special = false;
    $catx = 'cat_x';
    $number_of_items = sizeof( WC()->cart->get_cart() );

    if ( $number_of_items > 0 ) {

        // Loop through all cart products
        foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
            $item = $values['data'];
            $item_id = $item->id;

            // detecting if 'cat_x' item is in cart
            if ( has_term( $catx, 'product_cat', $item_id ) ) {
                if (!$special)
                    $special = true;
            }
        }

        // Re-loop through all cart products
        foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
            $item = $values['data'];
            $item_id = $item->id;

            if ( $special ) // there is a 'cat_x' item in cart
            { 
                if ( $number_of_items == 1 ) { // only one 'cat_x' item in cart
                    if ( empty( $notice ) )
                        $notice = '1';
                }
                if ( $number_of_items >= 2 ) { // 'cat_x' item + other categories items in cart
            // removing other categories items from cart
                    if ( !has_term( $catx, 'product_cat', $item_id ) ) {
                        WC()->cart->remove_cart_item( $cart_item_key ); // removing item from cart
                        if ( empty( $notice ) || $notice == '1' )
                            $notice = '2';
                    }
                }
            } else { // Only other categories items
                if ( empty( $notice ) )
                    $notice = '3';
            }
        }

        // Firing notices
        if ( $notice == '1' ) { // message for an 'cat_x' item only (alone)
            wc_add_notice( sprintf( '<p class="woocommerce-error">bla bla bla one category X item in the cart</p>' ), 'success' );
        } elseif ( $notice == '2' ) { // message for an 'cat_x' item and other ones => removed other ones 
            wc_add_notice( sprintf( '<p class="woocommerce-error">bla bla bla ther is already category X in the cart => Other category items has been removed</p>' ), 'error' );
        } elseif ( $notice == '3' ) { // message for other categories items (if needed)
            wc_add_notice( sprintf( '<p class="woocommerce-error">bla bla bla NOT category X in the cart</p>' ), 'success' );
        }
    }
} 

具有条件函数 has_term()的方法也适用于类别数组,但我尝试过使用一个类别而不是一个类别来在该代码中设置类别数组. 但是它不起作用.

Has the conditional function has_term() works also with arrays of categories, I have tried instead of one category, to set an array of categories in that code. But it’s not working.

但是,我的需求发生了变化:我不想让客户有可能从不同类别中选择购物车项目.因此,购物车中必须始终有来自同一产品类别的商品.

However, my needs have changed: I don’t want to let the customer have the possibility to select cart items from different categories. So the cart must always have items from the same product category.

请帮忙吗?

谢谢.

推荐答案

使自定义函数挂接到woocommerce_add_to_cart_validation过滤器钩子将以一种更为简单的方式完成此工作,而无需设置类别数组.

Making a custom function hooked in woocommerce_add_to_cart_validation filter hook is going to do the job in a much more simpler way, without any need to set an array of categories.

因此,您的代码将更快,更紧凑.另外,您可以显示自定义通知来警告客户.

So your code will be much more faster and compact. Additionally you can display a custom notice to warn the customer.

如果购物车中有其他类别的商品,则此代码将避免添加到购物车中:

This code will avoid adding to cart, if an item of a different category is in cart:

add_filter( 'woocommerce_add_to_cart_validation', 'add_to_cart_validation_callback', 10, 3 );
function add_to_cart_validation_callback( $passed, $product_id, $quantity) {
    // HERE set your alert text message
    $message = __( 'MY ALERT MESSAGE.', 'woocommerce' );

    if( ! WC()->cart->is_empty() ) {
        // Get the product category terms for the current product
        $terms_slugs = wp_get_post_terms( $product_id, 'product_cat' array('fields' => 'slugs'));

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

代码会出现在您活动的子主题(或主题)的function.php文件或任何插件文件中.

此代码已经过测试,可以正常工作

This code is tested and it works

限制购物车商品仅来自不同的产品类别:

在函数中替换条件:

if( ! has_term( $product_cats, 'product_cat', $cart_item['product_id'] )) { 

通过

if( has_term( $product_cats, 'product_cat', $cart_item['product_id'] )) {

这篇关于限制购物车商品必须来自WooCommerce中的同一产品类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆