使用WooCommerce检查购物车中的产品类别 [英] Check for Product Category in cart items with WooCommerce

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

问题描述

我正在尝试创建一个条件,即如果购物车中存在类别 ilutulestik 中的任何产品,则仅在WooCommerce结帐页面上显示函数。



但是,当前似乎无法获得购物车信息。我认为这是因为,如果我在代码上使用 if($ cat_in_cart)条件,即使我有<购物车中存在code> ilutulestik 类别。



我尝试了多种方法来获取购物车信息,但没有一种方法似乎有效。我将包括以下两种尝试方式:



尝试1

  add_action('woocommerce_before_cart','kontrollime_ilutulestiku_olemasolu'); 

函数kontrollime_ilutulestiku_olemasolu()
{

全球$ woocommerce;
$ cat_in_cart = false;

foreach(WC()-> cart-> get_cart()as $ cart_item_key => gt $ values)
{
$ item = $ values ['data' ];
$ item_id = $ item-> id;

if(has_term('ilutulestik-2','product_cat',$ item_id)))
{
$ cat_in_cart = true;
休息时间;
}
}
}

尝试2

  add_action('woocommerce_before_cart','kontrollime_ilutulestiku_olemasolu'); 

函数kontrollime_ilutulestiku_olemasolu($ package)
{

全球$ woocommerce;
$ cat_in_cart = false;

foreach($ package ['contents'] as $ product)
{
//获取产品类别
$ product_cats = wp_get_post_terms(
$ product ['product_id'],'product_cat',array('fields'=>'names'));
//如果未设置category_name固定费用
if(in_array(’ilutulestik-2’,$ product_cats))
{
$ cat_in_cart = true;
休息时间;
}
}
}

我希望这段代码检查购物车中是否有属于 Ilutulestik (或slug ilutulestik-2 )类别的产品,以及是否确实,将 $ cat_in_cart 的值更改为 true ,所以以后我可以使用 if激活任何代码($ cat_in_cart)

解决方案

将购物车商品与产品类别配合使用的正确方法是:

  add_action('woocommerce_before_cart','action_before_cart'); 
function action_before_cart(){
$ categories = array(’ilutulestik-2’);
$ has_category = false;

//遍历购物车项目
foreach(WC()-> cart-> get_cart()as $ cart_item){
//检查产品类别
if(has_term($ categories,'product_cat',$ cart_item ['product_id'])){
$ has_category = true;
休息时间;
}
}

//测试输出(显示通知)
if($ has_category){
wc_print_notice(sprintf('产品类别% s在购物车中!',reset($ categories)),'notice');
}
}

代码会出现在您活跃孩子的function.php文件中主题(或活动主题)。经过测试并有效。






如果您还需要检查带有产品类别的父项,您将改为使用:

  //用于检查父产品类别的自定义条件函数
函数has_product_categories($ product_id, $ categories){
//初始化
$ parent_term_ids = $ categories_ids = array();
$ taxonomy =‘product_cat’;

//将类别术语名称和子项转换为类别术语ID
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;
}
}

//循环浏览当前产品类别术语以仅获取父主类别术语
foreach(get_the_terms($ product_id,$ taxonomy)as $ term){
if($ term-> parent> 0){
$ parent_term_ids [] = $ term-> parent; //设置父产品类别
$ parent_term_ids [] = $ term-> term_id; //(和孩子)
}否则{
$ parent_term_ids [] = $ term-> term_id; //这是Main category术语,我们对其进行设置。
}
}
return array_intersect($ categories_ids,array_unique($ parent_term_ids))吗?真假;
}

add_action(‘woocommerce_before_cart’,‘action_before_cart’);
function action_before_cart(){
$ categories = array(’ilutulestik-2’);
$ has_category = false;

//遍历购物车项目
foreach(WC()-> cart-> get_cart()as $ cart_item){
//检查产品类别
if(has_product_categories($ cart_item ['product_id'],$ categories)){
$ has_category = true;
休息时间;
}
}

//测试输出(显示通知)
if($ has_category){
wc_print_notice(sprintf('产品类别% s在购物车中!',reset($ categories)),'notice');
}
}

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


I'm trying to make a condition, where a function displays on WooCommerce checkout page only, if any product from category ilutulestik is present in the shopping cart.

Currently, however, it does not seem to be able to obtain cart info. I assume that because, if I use if ( $cat_in_cart ) condition on a code, the function I use it on, does not display, even if I have a product from ilutulestik category present in the shopping cart.

I've tried many different methods to obtain cart info, but none have seemed to work. I'll include 2 ways of how I tried below:

Try 1

add_action('woocommerce_before_cart', 'kontrollime_ilutulestiku_olemasolu');

    function kontrollime_ilutulestiku_olemasolu()
    {

        global $woocommerce;
        $cat_in_cart = false;

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

                if ( has_term( 'ilutulestik-2', 'product_cat', $item_id ) )
                {
                    $cat_in_cart = true;
                    break;
                }
            }
    }

Try 2

add_action('woocommerce_before_cart', 'kontrollime_ilutulestiku_olemasolu');

     function kontrollime_ilutulestiku_olemasolu($package)
     {

        global $woocommerce;
        $cat_in_cart = false;

            foreach ($package['contents'] as $product)
            {
                // get product categories
                $product_cats = wp_get_post_terms( 
$product['product_id'], 'product_cat', array('fields' => 'names') );
                // if it has category_name unset flat rate
                    if( in_array('ilutulestik-2', $product_cats) )
                    {
                    $cat_in_cart = true;
                    break;
                    }  
            }         
    }

I expect this piece of code to check if there is a product in cart that belongs to category Ilutulestik (or slug ilutulestik-2) and if it does, change $cat_in_cart value to true, so later I could activate any code with if ( $cat_in_cart ).

解决方案

The correct way for cart items to be used with product categories is:

add_action('woocommerce_before_cart', 'action_before_cart');
function action_before_cart() {
    $categories   = array('ilutulestik-2');
    $has_category = false;

    // Loop through cart items
    foreach ( WC()->cart->get_cart() as $cart_item ) {
        // Check for product categories
        if ( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ) {
            $has_category = true;
            break;
        }
    }

    // Testing output (display a notice)
    if ( $has_category ) { 
        wc_print_notice( sprintf( 'Product category "%s" is in cart!', reset($categories)), 'notice' );
    }
}

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


If you need to check also for parent terms with product categories, you will use instead:

// Custom conditional function that checks for parent product categories
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;
}

add_action('woocommerce_before_cart', 'action_before_cart');
function action_before_cart() {
    $categories   = array('ilutulestik-2');
    $has_category = false;

    // Loop through cart items
    foreach ( WC()->cart->get_cart() as $cart_item ) {
        // Check for product categories
        if ( has_product_categories( $cart_item['product_id'], $categories ) ) {
            $has_category = true;
            break;
        }
    }

    // Testing output (display a notice)
    if ( $has_category ) {
        wc_print_notice( sprintf( 'Product category "%s" is in cart!', reset($categories)), 'notice' );
    }
}

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

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

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