Check for Product Category in cart items with WooCommerce

喜你入骨 提交于 2019-12-11 04:18:01

问题


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 ).


回答1:


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.



来源:https://stackoverflow.com/questions/54297453/check-for-product-category-in-cart-items-with-woocommerce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!