WooCommerce: Add product to cart with price override?

79,530

Solution 1

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...

Solution 2

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.

Solution 3

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);   
    }
}

Solution 4

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

Solution 5

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); 
    }
}
Share:
79,530
dcolumbus
Author by

dcolumbus

Updated on July 09, 2022

Comments

  • dcolumbus
    dcolumbus almost 2 years
    $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
    dcolumbus over 11 years
    Thanks a lot! Do you have quite a bit of experience with WooCommerce?
  • Vasanthan.R.P
    Vasanthan.R.P almost 11 years
    This updates the cart. But still in the quick cart icon at the top it displays actual product price. Any hooks?
  • Diego
    Diego over 10 years
    Yup, does work with variations. Use $cart_object->cart_contents[$key]['variation']['variation-na‌​me'] tho, as there's no "set" method for variations
  • Feel The Noise
    Feel The Noise over 10 years
    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.
  • shish
    shish almost 10 years
    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 )
  • Abhishek Kumar
    Abhishek Kumar about 9 years
    how can i dynamically get the target product id of the product of which add to cart button is clicked?
  • Kirby
    Kirby about 9 years
    Seems like these two answers should be combined.
  • Kirby
    Kirby about 9 years
    Seems like these two answers should be combined.
  • Firefog
    Firefog over 8 years
    @ratnakar-Ratnakar-Store-Apps your code added the product but when goto cart page its price reset
  • Habib Rehman
    Habib Rehman about 7 years
    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
    Habib Rehman about 7 years
    @AbhishekKumar did you find any way for those id's ? i would like to add custom price for each product added
  • Habib Rehman
    Habib Rehman about 7 years
    is it possible to add each product's custom price in cart rather than iterating over added cart products ?
  • danyo
    danyo about 7 years
    Try my example above.
  • danyo
    danyo about 7 years
    Have you tried the exact code? I have this running in a live premium plugin with 1000's of installs. Which version of WooCommerce do you have installed?
  • Saran
    Saran about 7 years
    Latest version Both WP Version 4.7.3 and woocommerce 3.0.1
  • Ray Perea
    Ray Perea about 7 years
    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.
  • kontur
    kontur over 6 years
    Added comment about API change in WooCommerce version 3 to the answer, so it's more discoverable.
  • Asif Rao
    Asif Rao over 6 years
    $target_product_id = 598; $custom_price = 10; need to make this dynamic, how can do that ?
  • Nirmal Goswami
    Nirmal Goswami about 6 years
    @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
  • Jean Manzo
    Jean Manzo over 3 years
    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.
  • G.F.
    G.F. almost 3 years
    Hi, where you use add_donation_to_cart function ?