Skip to content
Advertisement

WooCommerce cart items price suffix for specific product category

I need to add a suffix text to the price of a woocommerce product from a specific category while on WooCommerce cart.

Here is my code:

add_filter( 'woocommerce_cart_item_price', 'filter_woocommerce_cart_item_price', 10, 3 ); 
add_filter( 'woocommerce_cart_item_subtotal', 'filter_woocommerce_cart_item_price', 10, 3 ); 
function filter_woocommerce_cart_item_price( $wc, $cart_item, $cart_item_key ) { 
    if(!in_array('67',$cart_item['data']->get_category_ids()))
    {
        return $wc;
    }
    else{
        return $wc . ' Monthly';
    }
}; 

The problem is it only works on simple products. Doesn’t seem to affect the prices of variable products (I tested on 3 different sites).

Any idea what I’m missing?

Advertisement

Answer

To make it work for product variations items in cart too, use instead the following:

add_filter( 'woocommerce_cart_item_price', 'cart_item_amounts_prefix', 10, 3 ); 
add_filter( 'woocommerce_cart_item_subtotal', 'cart_item_amounts_prefix', 10, 3 ); 
function cart_item_amounts_prefix( $amount, $cart_item, $cart_item_key ) { 
    if( has_term( array(67), 'product_cat', $cart_item['product_id'] ) ){
        $amount .= ' ' . __("Monthly", "woocommerce");
    }
    return $amount;
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement