44
$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?

dcolumbus
  • 9,596
  • 26
  • 100
  • 165

10 Answers10

58

Here is the code for overriding price of product in cart

add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );

function add_custom_price( $cart_object ) {
    $custom_price = 10; // This will be your custome price  
    foreach ( $cart_object->cart_contents as $key => $value ) {
        $value['data']->price = $custom_price;
        // for WooCommerce version 3+ use: 
        // $value['data']->set_price($custom_price);
    }
}

Hope it will be useful...

kontur
  • 4,934
  • 2
  • 36
  • 62
Ratnakar - StoreApps
  • 4,261
  • 23
  • 17
  • Thanks a lot! Do you have quite a bit of experience with WooCommerce? – dcolumbus Oct 16 '12 at 21:10
  • 6
    This updates the cart. But still in the quick cart icon at the top it displays actual product price. Any hooks? – Vasanthan.R.P Jun 06 '13 at 10:16
  • Yup, does work with variations. Use $cart_object->cart_contents[$key]['variation']['variation-name'] tho, as there's no "set" method for variations – Diego Oct 20 '13 at 10:00
  • Hey, could you please give me an example on how to make it work? I want to add the product with ID 598 to cart with an overrided price. I tried your code but don't know where should I put the product ID. Thank you. – Feel The Noise Jan 16 '14 at 13:02
  • 2
    Seems like these two answers should be combined. – Kirby Apr 02 '15 at 16:35
  • hey there, i tried your solution but what i'm trying to do is add custom price for each product when added to cart. I guess your solution iterate over added cart items. Any help you can do ? – Habib Rehman Feb 17 '17 at 06:43
  • 5
    Sorry to nag, but this doesn't seem to work in the latest version of Woo Commerce (currently 3.0.4). I had to use `$value['data']->set_price($custom_price);` because it appears the inner workings of `WC_Product_Simple` has changed. – Ray Perea Apr 26 '17 at 18:04
  • Added comment about API change in WooCommerce version 3 to the answer, so it's more discoverable. – kontur Sep 01 '17 at 10:51
  • @Vasanthan.R.P faced your issue in 2018 :) any solution/hook to update price in mini cart/quick cart ? it is showing old price which is set using `woocommerce_product_get_price` filter – Nirmal Goswami Feb 17 '18 at 09:57
  • This solution doesn't work in current Woocommerce versions. You can use `$value['data']->set_regular_price($custom_price); && $value['data']->set_sale_price($custom_price);` instead. Which I think is easier and intuitive. – Jean Manzo Dec 16 '20 at 17:06
46

You need to introduce an if statement for checking product id, in above code:

add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );

function add_custom_price( $cart_object ) {
    $custom_price = 10; // This will be your custome price  
    $target_product_id = 598;
    foreach ( $cart_object->cart_contents as $value ) {
        if ( $value['product_id'] == $target_product_id ) {
            $value['data']->price = $custom_price;
        }
        /*
        // If your target product is a variation
        if ( $value['variation_id'] == $target_product_id ) {
            $value['data']->price = $custom_price;
        }
        */
    }
}

Add this code anywhere and make sure that this code is always executable.

After adding this code, when you'll call:

global $woocommerce; 
$woocommerce->cart->add_to_cart(598);

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.

Mike Doe
  • 16,349
  • 11
  • 65
  • 88
Ratnakar - StoreApps
  • 4,261
  • 23
  • 17
  • 1
    How can I set a custom price based on user input? This custom price is hard coded. Any suggestions? Something like function add_custom_price( $cart_object, $custom_price ) – shish Jul 11 '14 at 08:19
  • 1
    how can i dynamically get the target product id of the product of which add to cart button is clicked? – Abhishek Kumar Mar 23 '15 at 12:13
  • 2
    Seems like these two answers should be combined. – Kirby Apr 02 '15 at 16:34
  • @ratnakar-Ratnakar-Store-Apps your code added the product but when goto cart page its price reset – Firefog Nov 26 '15 at 17:39
  • @AbhishekKumar did you find any way for those id's ? i would like to add custom price for each product added – Habib Rehman Feb 17 '17 at 06:44
  • $target_product_id = 598; $custom_price = 10; need to make this dynamic, how can do that ? – Asif Rao Nov 17 '17 at 12:01
14

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.

add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );

function add_custom_price( $cart_object ) {
    $custom_price = 10; // This will be your custom price  
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        $cart_item['data']->set_price($custom_price);   
    }
}
Reyo
  • 148
  • 3
  • 11
Saran
  • 358
  • 4
  • 9
7

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 :

add_action( 'woocommerce_before_calculate_totals', 'mj_custom_price' );

function mj_custom_price( $cart_object ) {
   $woo_ver = WC()->version; 
   $custom_price = 10;
   foreach ( $cart_object->cart_contents as $key => $value )
   {
       if($woo_ver < "3.0.0" && $woo_ver < "2.7.0")
       {
           $value['data']->price = $custom_price;
       }
       else
       {
           $value['data']->set_price($custom_price);
       }
   }            
}

Many Thanks

Mohd Jafar
  • 291
  • 3
  • 9
4

For the Wordpress and Woocommerce latest version,Please use like this

add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );

function add_custom_price( $cart_object ) {
    foreach ( $cart_object->cart_contents as $key => $value ) {
        $custom_price = 5;
        $value['data']->set_price($custom_price); 
    }
}
sumon cse-sust
  • 434
  • 4
  • 8
3

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 of price

Here is an example:

add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );
function add_custom_price( $cart_object ) {
    $custom_price = 10; // This will be your custome price  
    foreach ( $cart_object->cart_contents as $key => $value ) {
        $value['data']->set_price = $custom_price;
    }
}

I hope this helps people in the future :)

danyo
  • 5,686
  • 20
  • 59
  • 119
2

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.

function add_donation_to_cart() { 

    $cart_item_data = array('price' => $_REQUEST['donate_amount']);
    $woocommerce->cart->add_to_cart( 5395, 1, '', array(), $cart_item_data);
}


add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );

function add_custom_price( $cart ) {
    foreach ( $cart->cart_contents as $key => $value ) {
        $value['data']->price = $value['price'];
    }
}
tobros91
  • 668
  • 1
  • 9
  • 24
1

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

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Sark
  • 4,458
  • 5
  • 26
  • 25
1

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

Community
  • 1
  • 1
Lance Cleveland
  • 3,098
  • 1
  • 33
  • 36
1

You can use the following

add_filter( 'woocommerce_cart_item_price', 'kd_custom_price_message' );

function kd_custom_price_message( $price ) {

        $textafter = ' USD'; 
        return $price . $textafter;
}
7uc1f3r
  • 28,449
  • 17
  • 32
  • 50
shrddha patel
  • 127
  • 1
  • 12