I need to add a custom class for the specific products in cart page
So I’ve depended on this post /questions/72185196/add-css-classes-to-woocommerce-cart-items-with-or-based-on-product-categorie
.
Then made my code:
// Add class to grouped items in the cart
function add_grouped_items_class_to_cart_item($classes, $cart_item, $cart_item_key)
{
// Check if the item is a grouped product
if (isset($cart_item['data']) && $cart_item['data']->is_type('wooco')) {
$product = $cart_item['data'];
$product_type = $product->get_type();
$product_ids = array();
$metadata = $product->get_meta_data();
$data_array = $metadata[0]->get_data();
$linked_items = $data_array['value'];
$items = explode(',', $linked_items);
foreach ($items as $item) {
//$parts = explode('/', $item);
//$product_ids[]= $parts[0];
$classes[] = 'wooco_item';
}
}
return $classes;
}
add_filter('woocommerce_cart_item_class', 'add_grouped_items_class_to_cart_item', 10, 3);
But the code hasn’t worked at all and no custom class has added to the products
After I asked chatgpt and many ai bots (as copilot, gemini, claude ..) they unanimously agree that this code must applied for classic woocommerce cart page but my woocommerce uses blocks in cart page and I have to use javascript to get what I need .
But I can’t use javascript here at all for this function because it must use ajax with php function to detect the type of the product based on its id then apply the custom class and this takes a period of time.
Here is the structure of my cart page :
<table class="wc-block-cart-items wp-block-woocommerce-cart-line-items-block" tabindex="-1">
<thead>
<tr class="wc-block-cart-items__header">
<th class="wc-block-cart-items__header-image"><span>Product</span></th>
<th class="wc-block-cart-items__header-product"><span>Details</span></th>
<th class="wc-block-cart-items__header-total"><span>Total</span></th>
</tr>
</thead>
<tbody>
<tr class="wc-block-cart-items__row" tabindex="-1">
<!-- the rest data -->
</tr>
</tbody>
</table>
Help me please to apply the custom class.