is it possible to change product price in Woocommerce Mini Cart Widget? I\'ve overrided price in cart using tips from WooCommerce: Add product to cart with price override? but i
To change the price on the WooCommerce Mini Cart Widget you have to use this filter hook: woocommerce_widget_cart_item_quantity
You can check the line 63 of the file: woocommerce/templates/cart/mini-cart.php to see how the filter hook is created.
apply_filters( 'woocommerce_widget_cart_item_quantity', '' . sprintf( '%s × %s', $cart_item['quantity'], $product_price ) . '', $cart_item, $cart_item_key );
As the name of the filter hook indicates it can be used not only for the price but also to show the quantity.
For example you can use it to apply a discount to certain products based on a condition. In this case I used a value stored in the meta data of the product.
add_filter('woocommerce_widget_cart_item_quantity', 'custom_wc_widget_cart_item_quantity', 10, 3 );
function custom_wc_widget_cart_item_quantity( $output, $cart_item, $cart_item_key ) {
$product_id = $cart_item['product_id'];
// Use your own way to decide which product's price should be changed
// In this case I used a custom meta field to apply a discount
$special_discount = get_post_meta( $product_id, 'special_discount', true );
if ($special_discount) {
$price = $cart_item['data']->get_price();
$final_price = $price - ( $price * $special_discount );
// The final string with the quantity and price with the discount applied
return sprintf( '%s × %s %s', $cart_item['quantity'], $final_price, get_woocommerce_currency_symbol() );
} else {
// For the products without discount nothing is done and the initial string remains unchanged
return $output;
}
}
Note that this will only change how price is displayed, if you have to change it internally use also this action hook: woocommerce_before_calculate_totals