0

I have the following code to generate custom prices:

add_action( 'woocommerce_before_calculate_totals', 'update_custom_price', 1, 1 );
function update_custom_price( $cart_object ) {
    foreach ( $cart_object->cart_contents as $cart_item_key => $value ) {
      $price = my_custom_calculate_func($value);
      $value['data']->set_price($price);
    }
}

It works great on the cart page, but on the WooCommerce mini cart widget, it doesn't display the correct price but does calculate the proper sub-total.

enter image description here

I believe the code for this exists as a template, so I have copied the file from ../wp-content/plugins/woocommerce/templates/cart/mini-cart.php to ../wp-content/mytheme/woocommerce/cart/mini-cart.php but changing this file does nothing. I have deleted everything in this file and it stays the same.

Any guidance appreciated.

Jack Robson
  • 2,184
  • 4
  • 27
  • 50

1 Answers1

1

Product price is calculated in mini-cart.php at line 39:

$product_price     = apply_filters( 'woocommerce_cart_item_price', WC()->cart->get_product_price( $_product ), $cart_item, $cart_item_key );

You can edit this line in mini-cart.php in in your theme folder, or use filter in functions.php:

add_filter( 'woocommerce_cart_item_price', 'woocommerce_cart_item_price_filter', 10, 3 );
function woocommerce_cart_item_price_filter( $price, $cart_item, $cart_item_key ) {
    // your code to calculate $new_price

    return $new_price;
}
KAGG Design
  • 1,945
  • 8
  • 14