-1

I need to change the woo-commerce cart price as per the extra cart item data added. So which is the recommended the hook for using this.

I have used action hook 'woocommerce_before_calculate_totals' for changing the product price. First I took the product original price using the get_price() method and modified price and then set the product price using the set_price() method.

But some other plugin uses the filter 'woocommerce_product_get_price' to change the product price. So changes that I made are lost.

So which is the recommended hook for changing the product price in the cart ??

junaid TK
  • 51
  • 8

1 Answers1

0

Use woocommerce_before_calculate_totals to change cart price -

function change_wc_cart_item_price( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;

    foreach ( $cart->get_cart() as $item ) {
        $item['data']->set_price( 100 ); // Set item price 100
    }
}
add_action( 'woocommerce_before_calculate_totals', 'change_wc_cart_item_price', 99 );
itzmekhokan
  • 2,597
  • 2
  • 9
  • 9
  • currently I am using this "woocommerce_before_calculate_totals" hook. Once other plugin uses woocommerce_product_get_price this, it will remove my changes. Hope "woocommerce_before_calculate_totals" this will be the best option. – junaid TK Aug 20 '19 at 09:17
  • Did you find a solution to this? I'm gaving an issue with multiple calls to woocommerce_before_calculate_totals as well. Thanks! – K_C Dec 02 '19 at 19:48