使用 WooCommerce 检查购物车项目中的产品类别

Check for Product Category in cart items with WooCommerce

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

不过目前好像无法获取购物车信息。我假设是因为,如果我在代码上使用 if ( $cat_in_cart ) 条件,我使用它的功能不会显示,即使我的购物车中有来自 ilutulestik 类别的产品。

我尝试了很多不同的方法来获取购物车信息,但 none 似乎行之有效。我将在下面列出我尝试过的两种方法:

尝试 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;
                }
            }
    }

尝试 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;
                    }  
            }         
    }

我希望这段代码检查购物车中是否有属于类别 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;
    
    // 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' );
    }
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。


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

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

    if( is_string( $categories ) ) {
        $categories = (array) $categories; // Convert string to array
    }

    // Convert categories term names and slugs to categories term ids
    foreach ( $categories as $category ){
        $result = (array) term_exists( $category, $taxonomy );
        if ( ! empty( $result ) ) {
            $categories_ids[] = reset($result);
        }
    }

    // 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' );
    }
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。