• Resolved enatadesign

    (@enatadesign)


    the following code display the product price twice

    add_filter( ‘woocommerce_get_price_html’, ‘euros_custom_price’, 100, 2 );

    function euros_custom_price( $price, $product ){
    $price = ‘XPF ‘ . $price . ‘ (EUROS ‘ . $price . ‘)’;
    return apply_filters( ‘woocommerce_get_price’, $price );
    }

    I would like to have the price in euros calculate on the $price with 2 decimals

    $euros = $price * 0.008352
    so for example is $price = 1000 the $euros should be 8.35

    I don’t know how to integrate the multiplication into the following code

    add_filter( ‘woocommerce_get_price_html’, ‘euros_custom_price’, 100, 2 );

    function euros_custom_price( $price, $product ){
    $price = ‘XPF ‘ . $price . ‘ (EUROS ‘ . $euros . ‘)’;
    return apply_filters( ‘woocommerce_get_price’, $price );
    }

Viewing 9 replies - 1 through 9 (of 9 total)
  • Have you seen the number_format() function:
    https://www.w3schools.com/php/func_string_number_format.asp

    So:

    add_filter( 'woocommerce_get_price_html', 'euros_custom_price', 100 );
    function euros_custom_price( $price ) {
      $euros = $price * 0.008352;
      $euros = number_format($euros, 2);
      $price = 'XPF ' . $price . ' (EUROS ' . $euros . ')';
      return $price;
    }
    

    There’s no need to apply_filters on the return statement.

    Thread Starter enatadesign

    (@enatadesign)

    Thank you Lorro for your help.

    I tried the code you gave me, but the calculation doesn’t work.
    if I put the price at 1000, the result I get is

    XPF 1,000 (EUROS 0.00)

    Instead of

    XPF 1,000 (EUROS 8.35)

    Upon investigation, it seems $price isn’t a number, its a string which includes a currency symbol, so that’s why we can’t use it for arithmetic. Instead, try:

    add_filter( 'woocommerce_get_price_html', 'euros_custom_price', 100, 2 );
    function euros_custom_price( $price, $product ) {
      $my_price = $product->get_price();
      $euros = $my_price * 0.008352;
      $euros = number_format($euros, 2);
      $price = 'XPF ' . $price . ' (EUROS ' . $euros . ')';
      return $price;
    }
    
    Thread Starter enatadesign

    (@enatadesign)

    Thank you Lorro, it works perfectly. I really appreciate your help on this.

    I f you have a Paypal, give me your email, I would like to give a donation for your help.

    Thread Starter enatadesign

    (@enatadesign)

    The code you gave me works great for a single product, but when I used a. variable product for example with a price range from CFP 1000 to 2000 , I get this

    XPF 1,000 – 2,000 (EUROS 8.35)

    It only shoe the lowest price in EUROS but not the price range.

    <?php
      add_filter( 'woocommerce_get_price_html', 'euros_custom_price', 100, 2 );
      function euros_custom_price( $price, $product ) {
        $factor = 0.008352; // Euro conversion factor
        if ( $product->is_type('simple') ) {
          $my_price = $product->get_price();
          $euros = $my_price * $factor;
          $euros = number_format($euros, 2);
          $price = 'XPF ' . $price . ' (EUROS ' . $euros . ')';
        }
        if ( $product->is_type('variable') ) {
          $min_price = $product->get_variation_price('min');
          $min_euros = $min_price * $factor;
          $min_euros = number_format($min_euros, 2);
          $max_price = $product->get_variation_price('max');
          $max_euros = $max_price * $factor;
          $max_euros = number_format($max_euros, 2);
          $price = 'XPF '.$min_price.' to '.$max_price.' (EUROS '.$min_euros.' to '.$max_euros.')';
        }
        return $price;
      }
    
    Thread Starter enatadesign

    (@enatadesign)

    Thank you

    Thread Starter enatadesign

    (@enatadesign)

    Hello Loro,

    In the same Manner, I would like to display the 2 prices in XPF and EUROS on
    – the “cart” page “Total”
    – the “checkout” page “Total”

    Thank you

    Sorry that’s a bit much development work for a forum answer. You might be lucky. If not you can find a developer here:
    https://jobs.wordpress.net/
    Proposals are likely to be competitive and you are not obliged to accept any.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Display 2 price in woocommerce’ is closed to new replies.