Get the product category terms from cart items in WooCommerce

steecp

I only get the product category on some producs, on some I don't

function savings_33_55_cart() {
    foreach ( WC()->cart->get_cart() as $key => $cart_item ) { 
        for ($i=0; $i < $cart_item['quantity'] ; $i++) {   
            $productId = $cart_item['data']->get_id();

            echo "PROD ID: " . $productId . "<br>";

            $terms = get_the_terms( $productId, 'product_cat' );

            foreach ($terms as $term) {
                $product_cat = $term->name;
                echo "PRODUCT CATEGORY: " . $product_cat . "<br>"; 
            }
        }
    }
}

add_action( 'woocommerce_cart_totals_before_order_total', 'savings_33_55_cart' );

I expect to get a product category on every product, but I only get the product category on some products

LoicTheAztec

First your $product_cat variable is not defined.

To get a product category on cart items, you need to get the parent variable product ID for product variations as they don't handle custom taxonomies as product categories or product tags.

To get the correct product ID for any custom taxonomy terms on cart items always use:

$product_id = $cart_item['product_id']; 

Instead of:

$product_id = $cart_item['data']->get_id(); 


Now if you need to get the product category term names, instead of using get_the_terms() function, you can use wp_get_post_terms() and implode() functions like:

$term_names = wp_get_post_terms( $product_id, 'product_cat', ['fields' => 'names'] );

// Displaying term names in a coma separated string
if( count( $term_names ) > 0 )
    echo __("Product categories") . ": " . implode( ", ", $term_names ) . '<br>':

// OR displaying term names as a vertical list
if( count( $term_names ) > 0 )
    echo __("Product categories") . ": " . implode( "<br>", $term_names ) . '<br>';


So in a cart items foreach loop:

// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
    $quantity     = $cart_item['quantity']; 
    $product_id   = $cart_item['product_id'];
    $variation_id = $cart_item['variation_id'];

    echo __("PRODUCT ID: ") . $product_id . "<br>";

    $term_names = wp_get_post_terms( $product_id, 'product_cat', array('fields' => 'names') );

    if ( count($term_names) > 0 ) {
        echo __("PRODUCT CATEGORY: ") . implode("<br>", $term_names) . "<br>";
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Get WooCommerce cart items count except for a product Category

Restricting cart items to be from the same product category in WooCommerce

Set a minimum quantity for cart items from specific WooCommerce product category

Checking cart items for a product category in Woocommerce

Check for Product Category in cart items with WooCommerce

How to get product variation attribute slugs from Woocommerce cart items

Sync custom product price by product category on cart items in Woocommerce

How to get product category ids from order items in WooCommerce

Cart total based on product category and items count in Woocommerce

Sort specific product category cart items at the end in WooCommerce

Add a body class for a specific product category on WooCommerce cart items

Disable payment gateway based on number of items and product category in WooCommerce cart

Get product category and tag terms as meta keyword in WooCommerce

Get all attributes with their terms related to a product category in Woocommerce

Disable other product categories for a cart item from specific category in Woocommerce

Hide “remove item” from cart for WooCommerce product category

Get "primary" category image from WooCommerce product

How to remove cart items from WooCommerce Session for a specific category

Using get_the_terms() doesn't return child terms of a specific parent category applied to a WooCommerce product

WooCommerce: Get purchased items for a product category using a SQL query

Send email if woocommerce order has items from a specific product category

WooCommerce redirect to product category on add to cart

Counting cart-items of specific product category

Set Woocommerce products category items prices when a specific product ID is in cart

If product A is in cart, remove from cart any items in category B, C and D

How to get the terms from WooCommerce product attribute taxonomies?

Get in WooCommerce cart the product ID of a cart item

Prevent add to cart if cart contains a specific Woocommerce product category

woocommerce get product category id