Welcome to the pages of our site.
Very often customers who use several currencies on their site ask themselves how to display the price of the goods in just a few currencies in the product card.
By default, opencart gives the opportunity to change the currency only in the header throughout the site, but very many users do not know how to select the desired currency and switch the site to it, so today we'll talk about how to visually display prices in several currencies immediately near the main price.
An example of what will eventually turn out you can look at the photo below:
As I wrote before, opencart by default does not provide the display of different currencies in the product card, but the convert function is present in the currency class:
1 |
public function convert($value, $from, $to) |
This function is used by the engine when you change the currency from the store's header, the conversion from the hryvnia to the dollar, for example, is as follows:
1 |
$this->currency->convert($product_info['price'], 'UAH', 'USD') |
Well, with the basic principles figured out now let's get down to practice and deduce different currencies near the price.
First of all I want to note that we will perform all actions on the default template, and by default we have the Hryvnia currency, and additional currencies are the ruble and the dollar, so we will show them.
1. We display different currencies in the product card:
Changes will be made in two files:
1 2 |
catalog/view/theme/default/template/product/product.tpl catalog/controller/product/product.php |
And so open the product.tpl file and look for the code that is responsible for displaying the price, namely:
1 |
<?php echo $price; ?> |
And after it we add the code:
1 2 3 4 |
//For the dollar: <span id="price_2"><?php echo $price_2; ?></span> //For the Ruble: <span id="price_3"><?php echo $price_3; ?></span> |
If you need to display just one thing then just specify the required drain.
Thus, we added a price output for the standard price in which there are no shares.
Now we need to add the output for the prices in which the auction price is indicated, in the same file we find the code:
1 |
<span class="price-old"><?php echo $price; ?></span> <span class="price-new"><?php echo $special; ?></span> |
And after it we add:
1 2 3 4 |
//For the dollar: <span class="price-old"><?php echo $price_2; ?></span> <span class="price-new"><?php echo $special_2; ?></span> //For the ruble: <span class="price-old"><?php echo $price_3; ?></span> <span class="price-new"><?php echo $special_3; ?></span> |
We finished with the product.tpl file, thus we added the output of the price in two additional currencies.
Now we need to edit the product.php file.
And so open it and look for the code:
1 |
$this->data['price'] = $this->currency->format($this->tax->calculate($product_info['price'], $product_info['tax_class_id'], $this->config->get('config_tax'))); |
Which is responsible for our price by default and after it we add:
1 2 |
$this->data['price_2'] = "($".round($this->currency->convert($product_info['price'], 'UAH', 'USD'), '2').")"; $this->data['price_3'] = "(".round($this->currency->convert($product_info['price'], 'UAH', 'RUB'), '0')." ΡΡΠ±.)"; |
Thus, we indicated that we need to display an additional price, but this only again applies only to the price that is without a stock, and that additional currencies are shown also in the products with the share we find the code:
1 |
$this->data['special'] = $this->currency->format($this->tax->calculate($product_info['special'], $product_info['tax_class_id'], $this->config->get('config_tax'))); |
And after it we add:
1 2 |
$this->data['special_2'] = "($".round($this->currency->convert($product_info['special'], 'UAH', 'USD'), '2').")"; $this->data['special_3'] = "(".round($this->currency->convert($product_info['special'], 'UAH', 'RUB'), '2')." ΡΡΠ±.)"; |
In these lines of code, the round (...) function allows you to round prices to 2, you can also specify 0 if you do not want to display a penny after the comma.
That's basically all, now we have in the product card will display the value of the goods both in the default price (hryvnia) and in the additional currencies the dollar and ruble are an example in the photo above.
And so with the card of the goods we have finished, but we have many other places where the price is displayed, let's consider other places of output of the currency of prices.
Let's display several of our currencies in modules, for example, novelties, recommended, sales hits, etc. ... for all of them the principle is the same, for this we take as a basis the module of the novelty, for all other actions the same.
2. We show different currencies prices in modules (Latest)
Changes will be made in two files:
1 2 |
catalog/view/theme/default/template/module/latest.tpl catalog/controller/module/latest.php |
First of all we open the latest.tpl file and in it we indicate the place where we will display our price, we find the first code:
1 |
<?php echo $product['price']; ?> |
And after it we add the following:
1 2 |
<span id="price_2"><?php echo $price_2; ?></span> <span id="price_3"><?php echo $price_3; ?></span> |
Thus, you have displayed a display of the additional price of the currency in the place of the module we need, for a price that does not have a stock, now we find the piece of code that is responsible for the auction price:
1 |
<span class="price-old"><?php echo $product['price']; ?></span> <span class="price-new"><?php echo $product['special']; ?></span> |
And after it we add the currencies we need:
1 2 |
<span id="price_2"><?php echo $price_2_special; ?></span> <span id="price_3"><?php echo $price_3_special; ?></span> |
All now you can proceed to the controller itself, that is, open the latest.php file, and in it we search for the output of the price, after which we also need to add a piece of the code we need:
And so we find:
1 2 |
if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) { $price = $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax'))); |
And then we add the code:
1 2 |
$this->data['price_2'] = "($".round($this->currency->convert($result['price'], 'UAH', 'USD'), '2').")"; $this->data['price_3'] = "(".round($this->currency->convert($result['price'], 'UAH', 'RUB'), '2')."ΡΡΠ±.)"; |
Now we need to do everything the same for the auction price, we find the code:
1 2 |
if ((float)$result['special']) { $special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax'))); |
And after it we add:
1 2 |
$this->data['price_2_special'] = "($".round($this->currency->convert($product_info['special'], 'UAH', 'USD'), '2').")"; $this->data['price_3_special'] = "(".round($this->currency->convert($product_info['special'], 'UAH', 'RUB'), '2')."ΡΡΠ±.)"; |
Well, in principle, that's all, now you have a module in the novelty will display the price in different currencies, other modules do by analogy.
IMPORTANT: In some other modules, use $product_info instead of $result all the rest by analogy.
Now we need to make it possible to display currencies in categories, search, share page, manufacturers, etc ...
For example, we take the page of product categories, the rest do the same.
And so let's make a display of the currency in the category of goods.
3. We show different currencies prices in the category of goods
In the category we will use a slightly different code to display the additional currency, but there's nothing too complicated.
Changes will be made in two files, namely:
1 2 |
catalog/view/theme/default/template/product/category.tpl catalog/controller/product/category.php |
Well, let's begin.
Open the category.tpl file and look for the output of the product price without stock:
1 |
<?php echo $product['price']; ?> |
After this code we add:
1 2 |
<br>( <span id="price_2"><?php echo $product['price_2']; ?></span> / <span id="price_3"><?php echo $product['price_3']; ?></span> ) |
To display currency to commodities with shares, look for the code:
1 |
<span class="price-old"><?php echo $product['price']; ?></span> <span class="price-new"><?php echo $product['special']; ?></span> |
And after it we add:
1 2 |
<br>( <span id="price_2"><?php echo $product['price_2_special']; ?></span> / <span id="price_3"><?php echo $product['price_3_special']; ?></span> ) |
All with the category.tpl file is finished, now proceed to the controller, and so open the category.php file and look for the code:
1 |
$price = $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax'))); |
And after it we add:
1 2 3 |
// Additional currency $price_2 = $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax')), 'USD'); $price_3 = $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax')), 'RUB'); |
There is a little lower after the code:
1 |
$price = false; |
Adding:
1 2 |
$price_2 = false; $price_3 = false; |
Now all the same we need to add for the product with promotional price, we are looking for the code below:
1 |
$special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax'))); |
And after it we add:
1 2 3 |
// Additional currency for shares $price_2_special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax')), 'USD'); $price_3_special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax')), 'RUB'); |
And a little lower after the code:
1 |
$special = false; |
Add the following:
1 2 |
$price_2_special = false; $price_3_special = false; |
Further we fall below and we search for the code:
1 |
'price' => $price, |
And right after it we add:
1 2 3 4 5 |
// Additional currency 'price_2' => $price_2, 'price_3' => $price_3, 'price_2_special' => $price_2_special, 'price_3_special' => $price_3_special, |
Well, in the prince and everything, now we have in the category in the list of goods next to the price will be displayed an additional currency, with search pages, producers, shares, etc. ... we do everything by analogy.
Try common, test and everything will work out, and if that does not work ask in the comments.
No Comment
You can post first response comment.