• Resolved berbercito

    (@berbercito)


    Hi,
    I have been testing Wp All Import + Import Products from any XML or CSV to WooCommerce in local and I am really impressed by its power and ease of use.

    I am trying to import products using a CSV and at the same time I want to modify the price on the fly using the maths function.

    This is the short code I am using [MATH({price[1]},”+”,”2″)]. I have noticed that Wp All Import rounds the prices before running the mats function resulting in incorrect output.

    For example the original price of the first product in the CSV is 4,400000000 and the output is 6 instead of 6,4 after using [MATH({price[1]},”+”,”2″)].

    If I use this short code [MATH({price[1]},”*”,”1″)] the result is 4 instead of 4,4

    ?Is there a way to tell Wp All Import not to round prices?

    By the way I am using commas for decimals and dots for thousands separation. I also have defined two decimals as the default for prices output in Woocommerce settings.

    I have also tried using different themes to avoid posible rounding functions that may be running in the backend but no success.

    I would be so thankful if you could shed some light on what I should do or what I am actually doing wrong.

    Looking forward to your help.

    • This topic was modified 4 years, 6 months ago by berbercito.
    • This topic was modified 4 years, 6 months ago by berbercito.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author WP All Import

    (@wpallimport)

    Hey @berbercito,

    ?Is there a way to tell Wp All Import not to round prices?

    The problem is the comma as the decimal separator and dots for thousands separation. You’ll need to fix that before the calculation, so you should switch to using a PHP function to do the math instead: https://www.wpallimport.com/documentation/developers/custom-code/inline-php/.

    Some examples:

    [array_sum(array(str_replace(",",".",str_replace(".","",{price[1]})),"2"))]

    And…

    [array_product(array(str_replace(",",".",str_replace(".","",{price[1]})),"1"))]

    Thread Starter berbercito

    (@berbercito)

    Hi,

    Thanks a lot for your quick reply. I have just tested both short codes and they work perfectly to perform corect price calculation.

    I have modified the function this way and it works correctly as well:

    <?php
    function final_price( $price = null, $extra = 0 ) {
    
    if ( !empty( $price ) ) {
    
    // strip any extra characters from price
    $price = preg_replace("/[^0-9,.]/", "", $price);
    
    //price ommas and dots fix
    $price = str_replace(",",".",str_replace(".","",$price));
    
    // perform calculations
    return ($price + $extra); 
    }
    }
    ?>

    Millions of thanks!!

    I would definitely go for the pro version.

    onclesam89

    (@onclesam89)

    I’m also on WP all import and love this very effective plugin.

    The rounding function that you offer on your wpallimport.com site works very well.

    function round_price( $price = null, $multiplier = 1, $nearest = .01, $minus = 0 ) {
    if ( !empty( $price ) ) {
    // strip any extra characters from price
    $price = preg_replace("/[^0-9,.]/", "", $price);
    // perform calculations
    return ( round ( ( $price * $multiplier ) / $nearest ) * $nearest ) - $minus; 
    }
    }

    As I live in Switzerland, I just have to round the prices to 5 cents … I offer discounts of 15% so if the price is 25.20 CHF, it becomes 21.42 CHF. I have to find a solution to fall on CHF 21.40.

    Do you have an idea ?

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Price rounding problem’ is closed to new replies.