I want to add an extra class to the cart table row if the product is free (price of 0).
I do have a way to add an extra class:
function custom_add_cart_item_classes( $class, $values, $values_key ) { if ( isset( $values[ 'product_id' ] ) ) { $class .= ' cart_item_' . $values[ 'product_id' ]; // This is the problem: $cart_item is not available here $get_item_cart_price = $cart_item['data']->get_price(); if ($get_item_cart_price == '0') : $class .= ' cart_item_free'; endif; } return $class; } add_filter( 'woocommerce_cart_item_class', 'custom_add_cart_item_classes', 10, 3 );
The problem is, that I cannot access $cart_item
in the function.
I made a var_dump
for $values
and found the product ID in it.
Is there any way to get the $cart_item
with the product ID? Or any other way to load that element here?
EDIT: I need the cart item because the price is changed in the cart. The regular product price is not free.
Advertisement
Answer
Tested OK with WC 4.9.0
function custom_add_cart_item_classes( $class, $values, $values_key ) { if ( isset( $values[ 'data' ] ) ) { $product = $values[ 'data' ]; if(!$product->get_price()){ $class .= ' cart_item_free'; } } return $class; } add_filter( 'woocommerce_cart_item_class', 'custom_add_cart_item_classes', 10, 3 );