Woocommerce Mini Cart Widget product price override

為{幸葍}努か 提交于 2019-12-02 04:13:10

This is how I set price to 0 for free products:

function set_free_items_price( $price, $cart_item, $cart_item_key ) {

     $returned_item_price = $cart_item['data']->get_price();

     if (isset($cart_item['free_item']['value'])
         && $cart_item['free_item']['value'] == true) {
         $returned_item_price = 0;
     }

     return get_woocommerce_currency_symbol() . $returned_item_price;
}

I hooked inside class, so it hook looks like this:

add_filter( 'set_free_items_price', array(__CLASS__, 'woocommerce_cart_item_price_filter'), 10, 3 );

But if you are using it as procedural function, your hook should look like this:

add_filter( 'set_free_items_price', 'woocommerce_cart_item_price_filter', 10, 3 );

Keep in mind this is also affecting price row in regular cart. And, if you are not seeing changes in mini cart, try to update cart on regular cart page, and the go back and check again if mini-cart values has been changed.

Swapnil Ghone

Instead of using "woocommerce_before_calculate_totals" you can use "woocommerce_cart_item_price" filter, some thing like

add_filter('woocommerce_cart_item_price','modify_cart_product_price',10,3);

function modify_cart_product_price( $price, $cart_item, $cart_item_key){
    $price = wc_price($custom_price, 4); 
    return $price;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!