woocommerce_after_calculate_totals not working to update cart item price

Question

what I am trying to achive is if user adds a particual product in cart its price should change to zero

Here is the simple code snippet which I am using to update cart item price but

add_action('woocommerce_before_calculate_totals','set_bonus_product_pice');
function set_bonus_product_pice($cart_object){
     if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;

       $free_product_ID = 10;
       foreach ( $cart_object->cart_contents as $key=>$cart_item) { 
           if($cart_item['product_id'] == $free_product_ID){
                 $cart_item['data']->set_price(0);
                 break;
           }
        }
 }

I also tried dubugging it further it works fine until wc_get_price_excluding_tax method is called in class-wc-cart.php file , some how wc_get_price_excluding_tax method removes the change from product object.

0
Swapnil Ghone 3 years 2020-08-27T08:10:45-05:00 0 Answers 30 views 0

Leave an answer

Browse
Browse