0

I want to remove / takeout the price of a specific category products from Cart Total.

Please see this screenshot:

Cart Page

What hook could I use to do this?

Thanks.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Zahid Iqbal
  • 134
  • 3
  • 10
  • There is an error in your code, the brackets don't match up. With your code it seems you are adding a fee, how does that relate to removing the product price? Be more specific. – RST Nov 13 '16 at 09:52
  • 1
    So basicly you are just asking for code? Stackoverflow is about helping to solve code errors/problems, not about code on demand. – RST Nov 13 '16 at 10:13
  • Add the code exactly how you tried it to your post, not as comment. Explain what happened and what should have happened. – RST Nov 13 '16 at 10:19

1 Answers1

2

The right hook to do that is woocommerce_before_calculate_totals using has_term() WordPress conditional function to filter product categories in cart items. This way you can null the price for that cart items.

This is the code (added compatibility for Woocommerce 3+):

add_action( 'woocommerce_before_calculate_totals', 'custom_price_product_category', 20, 1 );
function custom_price_product_category( $cart ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    foreach ( $cart->get_cart() as $cart_item ) {
        // When a product has 'glass' as category we null the price.
        if( has_term( 'glass', 'product_cat', $cart_item["product_id"] ) ){
            // Added Woocommerce 3+ compatibility
            if( version_compare( WC_VERSION, '3.0', '<' ) )
                $item['data']->price = '0';
            else
                $item['data']->set_price(0);
        }
    }
}

This goes in function.php file of your active child theme (or theme) or also in any plugin file.

This code is tested and works.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399