WooCommerce Cart - Dynamic Price variable pass into custom price hook

I am getting the dynamic custom price in a variable that I want to pass to the hooked function in woocommerce_before_calculate_totals hook in cart. But it isn't working.

This is my code:

  $add=200; //I want to pass this variable in add_action hook
  add_action( 'woocommerce_before_calculate_totals', 'add_custom_total_price');
  function add_custom_total_price($cart_object) {
     global $woocommerce;
     $custom_price =$add; // This is my custom price
    foreach ( $cart_object->cart_contents as $key => $value ) {
       $value['data']->price = $custom_price;
    }
 } 

How can I achieve this?

Thanks

Solutions

To add Price in your woocommerce cart before calculate total -

 add_action( 'woocommerce_before_calculate_totals', 'add_custom_total_price');

  function add_custom_total_price($cart_object) {
     global $woocommerce,$add,$custom_price; 
     $add=200;//Dynamic Price variable
     $custom_price =$add;//Dynamic Price variable pass to custom price
    foreach ( $cart_object->cart_contents as $key => $value ) {
       $value['data']->price = $custom_price;
    }
 } 

To make it work you just need to define $custom_price variables as global in your function, this way:

$custom_price = 200; 

add_action( 'woocommerce_before_calculate_totals', 'add_custom_item_price', 10, 1 );
function add_custom_item_price( $cart_object ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    global $custom_price;

    foreach (  $cart_object->get_cart() as $item_values ) {
        $item_values['data']->price = $custom_price;
    }
} 

This code is tested and working (for woocommerce versions 2.5+ and 2.6+) .

Naturally this code goes on function.php file of your active child theme or theme.

Similar questions

Woocommerce Adding price amount class ("woocommerce-Price-amount amount) to return $price
I am building a measurement Store with Woo and need some special Outputs for Pricing I am not able to fix. The Price Output needs to be: Price per Package and the Price per Unit so I need to add the class="woocommerce-Price-amount amount" for Display 1) I had used this Snippet to Display the Unit of Package Price: function cw_change_pro...
Add Variable product into cart automatically when cart total is more than 25
I want to add automatically one variations product into cart when cart total is become more than 25. The automatically adding variation product price need to set as 0 and also customers cant change the qty or need to disable the Qty filed for free product. I added below code and its working fine only the issue when page reloading or cart updating t...
Dynamic price and filter by price widget woocommerce
I use the «Exchange Rates Today» plugin to display prices based on the exchange rate (the developer apparently no longer supports it). But the plugin still works for me as it should: the cost of the product is specified in$, in the settings of this plugin, the currency exchange rate is specified and the price is displayed on the site, taking into a...
Why can't I pass a custom field variable to 'numberposts' variable in Wordpress array?
I'm going crazy here - I can't figure out why this doesn't work! I have a wordpress template with multiple queries to show different post types. I am able to do this no problem: Which passses the value of "1" into the 'numberposts' variable. What I NEED to do is instead replace that value of "1" with a value passed from a custom field in the admin ...
woocommerce add dynamic price while add to cart
My task is : I have test, test1, test2, test3 ==> 4 products The test product price is $0. While add to cart the price will be added to that particular 'test' product is $500 How to achieve this. I use the following hook But it only shows the total as 500. I need to show this price as product price in my entire cart. How to do this. Please help me....

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy .