$replace_order = new WC_Cart();
$replace_order->empty_cart( true );
$replace_order->add_to_cart( "256", "1");
The above code add product 256
to the Cart 1
time. But the issue I’m having is that I want to be able to completely override the product price… as far as I can tell, the only thing I can do it apply a coupon to the Cart.
Is there a way to completely override the price to something totally custom?
Here is the code for overriding price of product in cart
Hope it will be useful…
You need to introduce an
if
statement for checking product id, in above code:Add this code anywhere and make sure that this code is always executable.
After adding this code, when you’ll call:
Only this product will be added with overridden price, other products added to cart will be ignored for overriding prices.
Hope this will be helpful.
I have tried all above code samples and latest woocommerce 3.0 is not support any of the above example. Use below code and working perfectly for me.
After release of woocommerce version 3.0.0 product price is update on add to cart using set_price($price) function. The example is given as below :
Many Thanks
For the WordPress and Woocommerce latest version,Please use like this
For eveeryone that got here from Google. The above is now deprecated as i found out updating to WooCommerce 3.0.1.
Instead of the above you now need to use
set_price
instead ofprice
Here is an example:
I hope this helps people in the future 🙂
This is how i did it, first i add my custom price to cart_item_data witch can save custom data to cart items, then i use woocommerce_before_calculate_totals, loop the cart and add the previously added price.
With WooCommerce 2.5 I found this to be a 2-part process. The first step is to change the run-time display of pricing when added to the cart via the woocommerce_add_cart_item filter. The second part is to set the persistent session data which is read during checkout via the woocommerce_get_cart_item_from_session filter. This seems to be faster than hooking the calculate totals filters (such as woocommerce_before_calculate_totals) as they are run very frequently in WooCommerce.
More details here:
woocommerce change price while add to cart
To make it dynamic ( override price for each item in cart separately ), you need to save the override product price in session with cart item key as session key using
woocommerce_add_to_cart
hook.by using these session values you can calculate correct Cart Total and make the altered price appear in the Order Item as well
You can use the following