在Woocommerce中检查购物车项目中的产品类别 [英] Checking cart items for a product category in Woocommerce

查看:54
本文介绍了在Woocommerce中检查购物车项目中的产品类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在woocommerce中,我尝试使用以下方式检入特定产品类别的购物车商品:

In woocommerce, I'm trying to check in cart items for a particular product category using:

add_action('woocommerce_before_cart', 'fs_check_category_in_cart');
function fs_check_category_in_cart() {
    // Set $cat_in_cart to false
    $cat_in_cart = false;
    // Loop through all products in the Cart        
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        $product = $cart_item['data'];
        echo '<pre>',print_r($product),'</pre>';
        // If Cart has category "download", set $cat_in_cart to true
        if ( has_term( 'downloads', 'product_cat', $product->get_id() ) ) {
            $cat_in_cart = true;
            break;
        }
    }
    // Do something if category "download" is in the Cart      
    if ( $cat_in_cart ) {

        // For example, print a notice
        wc_print_notice( 'Category Downloads is in the Cart!', 'notice' );
        // Or maybe run your own function...
        // ..........
    }
}

我一直无法实现。在进一步检查时,当我 print_r($ product)
时,数组的末尾看起来像:

Ive been unable to achieve it. On further inspection when I print_r( $product ) the end of the array look like:

            [current_class_name:WC_Data_Store:private] => WC_Product_Data_Store_CPT
            [object_type:WC_Data_Store:private] => product-simple
    )

    [meta_data:protected] => 
)
1

数组末尾的此1附加其我尝试引用的任何变量的self。所以我得到

This 1 at the end of the array is attaching its self to any variable I try and reference. So I get

downloads1 

如果有人知道这个数字可能从何而来,这一直使我感到压力!

If anyone knows where this number maybe coming from its been stressing me out!

为了记录,也做 print_r ($ woocommerce)在数组末尾有1。

Just for the record also doing print_r( $woocommerce ) has the 1 at the end of the array.

我们将不胜感激。

推荐答案

使用WordPress nofollow noreferrer> has_term() 条件函数,您需要使用 $ cart_item ['product_id'] 来处理产品变体中的产品类别。

To check product categories in cart items using WordPress has_term() conditional function, you need to use $cart_item['product_id'] instead to handle checking product categories in product variations too.

通过这种方式,由于产品变体类型不处理任何自定义分类标准,因此可以在父变量产品中检查产品类别。因此,现在它适用于所有情况。

This way it check in the parent variable product for the product category as product variation type doesn't handle any custom taxonomy. So now it will work for all cases.

所以您重新访问的代码将是:

So your revisited code will be:

add_action('woocommerce_before_cart', 'check_product_category_in_cart');
function check_product_category_in_cart() {
    // HERE set your product categories in the array (can be IDs, slugs or names)
    $categories = array('downloads');
    $found      = false; // Initializing

    // Loop through cart items      
    foreach ( WC()->cart->get_cart() as $cart_item ) {
        // If product categories is found
        if ( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ) {
            $found = true; // Set to true
            break; // Stop the loop
        }
    }

    // If any defined product category is found, we display a notice
    if ( $found ) {
        wc_print_notice( __('Product Category "Downloads" is in Cart items!'), 'notice' );
    }
}

代码会出现在您活跃孩子的function.php文件中主题(或活动主题)。经过测试,可以正常工作。

Code goes in function.php file of your active child theme (or active theme). Tested and works.

这篇关于在Woocommerce中检查购物车项目中的产品类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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