• To begin with I have a number of products that have an original price set in the administration end.

    But then I also have a csv file that I’m reading from and if the products sku number matches a product in the file the price changes to whatever the csv file contains.

    And that works fine but when the user add product to cart and goes to cart the price is still the original one. So i need help how to change the price globally.

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

    This function is easily found and it also does what it should as long as the custom_price is hardcoded. But whatever changes I make, as to adding another parameter and so on, fails for me. The custom_price should take whatever price the product has been given.

    https://www.ads-software.com/plugins/woocommerce/

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    This function is easily found and it also does what it should as long as the custom_price is hardcoded.

    Wouldn’t that mean the issue is in your CSV handling code, not the above snippet?

    Thread Starter gerrez

    (@gerrez)

    Maybe. But how would you handle the dynamical price? If I change that function to something like:

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

    It errors out on missing parameters even though I send them.

    Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    You cannot ‘send’ a price via that function/hook. Your price logic needs to be inside the function.

    Thread Starter gerrez

    (@gerrez)

    Any idea then to how i could somehow get to access that new price value inside the function? That’s the problem to begin with. I could do all shorts of logic but as long I can’t access the new/edited value the output will never be what it should anyway.

    Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    You’d call whatever price parsing/csv code you have there. I cannot help you with that part – I don’t know what else you’re doing to get prices.

    Thread Starter gerrez

    (@gerrez)

    Ok then. I’m kind of stuck on my own so let’s take the relevant code here step by step.

    In my price.php template file:

    global $product;
    do_action('show_price', $product);?>

    In my functions.php:

    add_action( 'show_price', 'show_price_function');
    
    function show_price_function($product) {
      if ($product->is_type( 'simple')) {
        if ($product->get_sku()) {
          $file_handle = fopen("filename", "r");
    
          while (!feof($file_handle) ) {
            $line_of_text = fgets($file_handle);
            $parts = explode(';', $line_of_text);
            $var = $product->get_sku();
    
            if ($parts[0] == $var) {
              $extra = 5;
              $answer = round((($extra / 100) * $parts[2]) + $parts[2], 0);
              $product->set_price($answer);
            };
          };
          fclose($file_handle);
        };
    
        $incl = $product->get_price_including_tax();
        $excl = $product->get_price_excluding_tax();
    
        //warning deprecated functions-->?>
        <p class="price"><?php echo woocommerce_price($excl);?> excl. tax</p>
        <p class="price" style="display:none;"><?php echo woocommerce_price($incl);?> incl. tax</p>
        <button class="tax">Se price incl. tax</button>

    Also in my functions.php

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

    So the price that I display in the two paragraphs are the new ones.
    What I at first thought happened with the set_price function was that the price for that product was changed throughout the site which it doesn’t though. But that’s the value I would like to be able to somehow access inside the custom price function. Any help in direction greatly appreciated!

    Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    The logic which parses the file and gets a price should be moved to a separate plugin, then you can use it in both ‘show_price’ and ‘add_custom_price’.

    Really though, this could be optimised further. Consider getting a developer to customise this for you, jobs.wordpress.net

    Hi,

    In the late version of woocommerce $value[‘data’]->price = $custom_price; this code is not updating the price in cart.

    Can you please send me the late code to update price of product in cart programmatically?

    Thanks

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘change product price in cart’ is closed to new replies.