最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Display product price in two currencies at the same time? - Stack Overflow

matteradmin5PV0评论

How do I modify PrestaShop 1.5 to display product prices in two currencies at the same time (ie. base currenct and visitor's currency on products listed in product & categories pages):

I think I should be modifying ProductController.php and product.tpl. Is this correct?

Below is one solution for the product page that I find on a forum, but it is for PrestaShop 1.4x:

  1. Override ProductController.php in /controllers/ProductController.php

    <?php
    class ProductController extends ProductControllerCore{
        public function displayContent() {
            global $currency;
            $second_currency = 'USD';
            $productPriceWithTax = Product::getPriceStatic($this->product->id, true, NULL, 6);
            if (Product::$_taxCalculationMethod == PS_TAX_INC) {
                $productPriceWithTax = Tools::ps_round($productPriceWithTax, 2);
            }
            $productPriceWithoutEcoTax = (float)($productPriceWithTax - $this->product->ecotax);
    
            $current_currency = $currency->iso_code;
            $default_currency = Currency::getDefaultCurrency()->iso_code;
            $currency_array   = Currency::getCurrencies($object = false, $active = 1);
    
            if ($current_currency == $default_currency) {
                foreach ($currency_array as $arr) {
                    if ((string)$arr['iso_code'] == $second_currency) {
                        $second_currency_price = Tools::ps_round($productPriceWithoutEcoTax * (float)$arr['conversion_rate'], 2);
                    }
                }
            }
    
            self::$smarty->assign('second_currency_price', $second_currency_price . ' ' . $second_currency);
            parent::displayContent();
        }
    }
    
  2. Modify product.tpl:

    {if $priceDisplay >= 0 && $priceDisplay <= 2}
        <span id="our_price_display">{convertPrice price=$productPrice}</span>
    

    to

    {if $priceDisplay >= 0 && $priceDisplay <= 2}
        {$second_currency_price} /
        <span id="our_price_display">{convertPrice price=$productPrice}</span>
    

In above example USD is the second currency ($second_currency='USD'). I was wondering if it would be possible to modify this code for PrestaShop 1.5, which has changed significantly since 1.4x.

How do I modify PrestaShop 1.5 to display product prices in two currencies at the same time (ie. base currenct and visitor's currency on products listed in product & categories pages):

I think I should be modifying ProductController.php and product.tpl. Is this correct?

Below is one solution for the product page that I find on a forum, but it is for PrestaShop 1.4x:

  1. Override ProductController.php in /controllers/ProductController.php

    <?php
    class ProductController extends ProductControllerCore{
        public function displayContent() {
            global $currency;
            $second_currency = 'USD';
            $productPriceWithTax = Product::getPriceStatic($this->product->id, true, NULL, 6);
            if (Product::$_taxCalculationMethod == PS_TAX_INC) {
                $productPriceWithTax = Tools::ps_round($productPriceWithTax, 2);
            }
            $productPriceWithoutEcoTax = (float)($productPriceWithTax - $this->product->ecotax);
    
            $current_currency = $currency->iso_code;
            $default_currency = Currency::getDefaultCurrency()->iso_code;
            $currency_array   = Currency::getCurrencies($object = false, $active = 1);
    
            if ($current_currency == $default_currency) {
                foreach ($currency_array as $arr) {
                    if ((string)$arr['iso_code'] == $second_currency) {
                        $second_currency_price = Tools::ps_round($productPriceWithoutEcoTax * (float)$arr['conversion_rate'], 2);
                    }
                }
            }
    
            self::$smarty->assign('second_currency_price', $second_currency_price . ' ' . $second_currency);
            parent::displayContent();
        }
    }
    
  2. Modify product.tpl:

    {if $priceDisplay >= 0 && $priceDisplay <= 2}
        <span id="our_price_display">{convertPrice price=$productPrice}</span>
    

    to

    {if $priceDisplay >= 0 && $priceDisplay <= 2}
        {$second_currency_price} /
        <span id="our_price_display">{convertPrice price=$productPrice}</span>
    

In above example USD is the second currency ($second_currency='USD'). I was wondering if it would be possible to modify this code for PrestaShop 1.5, which has changed significantly since 1.4x.

Share Improve this question edited Jan 30, 2014 at 16:28 cubitouch 1,94715 silver badges28 bronze badges asked Jun 4, 2013 at 11:56 user2427839user2427839
Add a ment  | 

1 Answer 1

Reset to default 2

You have to loop this array which contains all the currencies you manage: {$currencies}

{foreach from=$currencies item=c}{$c.name}{/foreach}

The default currency is in: {$id_currency_cookie}

If I remember, you have to write this in product.tpl.

I don't know how to display the correct price for your currency. Tell us if you find.

Post a comment

comment list (0)

  1. No comments so far