How to display a percentage discount in an Opencart template?
How to display a percentage discount in an Opencart template?
Описание
In this article you will learn how to display a percentage discount in a CMS Opencart template.
For some reason, there are very few working solutions to this problem on the Internet. Basically, all modules and articles are written for the administrative part, so that it is there that prices can be changed as a percentage.
But we will make discounts and promotions in % specifically for clients, because a percentage discount has a better effect on sales than the “old price” in numbers.
How to add a percentage discount for categories and modules?
For the first time, you can take any file of a module, category, or discounted product page.
In this article we will use the files:
- "/catalog/controller/module/special.php"
- "/catalog/view/theme/default/template/module/special.tpl"
Step #1. We make adjustments to the controller - (/catalog/controller/module/special.php)
We find:
if ((float)$result['special']) { $special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax'))); } else { $special = false; }if ((float)$result['special']) { $special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax'))); } else { $special = false; }if ((float)$result['special']) { $special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax'))); } else { $special = false; }if ((float)$result['special']) { $special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax'))); } else { $special = false; }if ((float)$result['special']) { $special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax'))); } else { $special = false; }And replace it with:
if ((float)$result['special']) { $special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax'))); $skidka = round(100-($result['special']/($result['price']/100))); } else { $skidka = false; $special = false; }if ((float)$result['special']) { $special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax'))); $skidka = round(100-($result['special']/($result['price']/100))); } else { $skidka = false; $special = false; }if ((float)$result['special']) { $special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax'))); $skidka = round(100-($result['special']/($result['price']/100))); } else { $skidka = false; $special = false; }if ((float)$result['special']) { $special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax'))); $skidka = round(100-($result['special']/($result['price']/100))); } else { $skidka = false; $special = false; }if ((float)$result['special']) { $special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax'))); $skidka = round(100-($result['special']/($result['price']/100))); } else { $skidka = false; $special = false; }if ((float)$result['special']) { $special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax'))); $skidka = round(100-($result['special']/($result['price']/100))); } else { $skidka = false; $special = false; }if ((float)$result['special']) { $special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax'))); $skidka = round(100-($result['special']/($result['price']/100))); } else { $skidka = false; $special = false; }Then we find:
$data['products'][] = array(Below the line:
'special' => $special,Insert:
'skidka' => $skidka,Attention! Some modules use $product_info instead of $result, in which case we change the line:
$skidka = round(100-($result['special']/($result['price']/100)));on
$skidka = 100-($product_info['special']/($product_info['price']/100));Step #2. We make changes to the template (/catalog/view/theme/default/template/module/special.tpl)
Are looking for:
<span class="price-old"><?php echo $product['price']; ?></span> <span class="price-new"><?php echo $product['special']; ?></span><span class="price-old"><?php echo $product['price']; ?></span> <span class="price-new"><?php echo $product['special']; ?></span>Add:
<span class="procent"><?php echo $product['skidka']; ?>%</span>How to add a percentage discount for a product card?
Step #1. We change the code in the controller - (/catalog/controller/product/product.php)
We find:
if ((float)$product_info['special']) { $this->data['special'] = $this->currency->format($this->tax->calculate($product_info['special'], $product_info['tax_class_id'], $this->config->get('config_tax'))); } else { $this->data['special'] = false; }if ((float)$product_info['special']) { $this->data['special'] = $this->currency->format($this->tax->calculate($product_info['special'], $product_info['tax_class_id'], $this->config->get('config_tax'))); } else { $this->data['special'] = false; }if ((float)$product_info['special']) { $this->data['special'] = $this->currency->format($this->tax->calculate($product_info['special'], $product_info['tax_class_id'], $this->config->get('config_tax'))); } else { $this->data['special'] = false; }if ((float)$product_info['special']) { $this->data['special'] = $this->currency->format($this->tax->calculate($product_info['special'], $product_info['tax_class_id'], $this->config->get('config_tax'))); } else { $this->data['special'] = false; }if ((float)$product_info['special']) { $this->data['special'] = $this->currency->format($this->tax->calculate($product_info['special'], $product_info['tax_class_id'], $this->config->get('config_tax'))); } else { $this->data['special'] = false; }And replace it with:
if ((float)$product_info['special']) { $this->data['special'] = $this->currency->format($this->tax->calculate($product_info['special'], $product_info['tax_class_id'], $this->config->get('config_tax'))); $this->data['skidka'] = round(100-($product_info['special']/($product_info['price']/100))); } else { $this->data['special'] = false; $this->data['skidka'] = false; }if ((float)$product_info['special']) { $this->data['special'] = $this->currency->format($this->tax->calculate($product_info['special'], $product_info['tax_class_id'], $this->config->get('config_tax'))); $this->data['skidka'] = round(100-($product_info['special']/($product_info['price']/100))); } else { $this->data['special'] = false; $this->data['skidka'] = false; }if ((float)$product_info['special']) { $this->data['special'] = $this->currency->format($this->tax->calculate($product_info['special'], $product_info['tax_class_id'], $this->config->get('config_tax'))); $this->data['skidka'] = round(100-($product_info['special']/($product_info['price']/100))); } else { $this->data['special'] = false; $this->data['skidka'] = false; }if ((float)$product_info['special']) { $this->data['special'] = $this->currency->format($this->tax->calculate($product_info['special'], $product_info['tax_class_id'], $this->config->get('config_tax'))); $this->data['skidka'] = round(100-($product_info['special']/($product_info['price']/100))); } else { $this->data['special'] = false; $this->data['skidka'] = false; }if ((float)$product_info['special']) { $this->data['special'] = $this->currency->format($this->tax->calculate($product_info['special'], $product_info['tax_class_id'], $this->config->get('config_tax'))); $this->data['skidka'] = round(100-($product_info['special']/($product_info['price']/100))); } else { $this->data['special'] = false; $this->data['skidka'] = false; }if ((float)$product_info['special']) { $this->data['special'] = $this->currency->format($this->tax->calculate($product_info['special'], $product_info['tax_class_id'], $this->config->get('config_tax'))); $this->data['skidka'] = round(100-($product_info['special']/($product_info['price']/100))); } else { $this->data['special'] = false; $this->data['skidka'] = false; }if ((float)$product_info['special']) { $this->data['special'] = $this->currency->format($this->tax->calculate($product_info['special'], $product_info['tax_class_id'], $this->config->get('config_tax'))); $this->data['skidka'] = round(100-($product_info['special']/($product_info['price']/100))); } else { $this->data['special'] = false; $this->data['skidka'] = false; }Attention! For Opencart 2.x, change the line:
$this->data['skidka'] = round(100-($product_info['special']/($product_info['price']/100)));on the:
$data['skidka'] = 100-($product_info['special']/($product_info['price']/100));Step #2. Editing the product card template (/catalog/view/theme/default/template/product/product.tpl)
We find:
<span class="price-old"><?php echo $product['price']; ?></span> <span class="price-new"><?php echo $product['special']; ?></span><span class="price-old"><?php echo $product['price']; ?></span> <span class="price-new"><?php echo $product['special']; ?></span>And add below or above:
<span class="procent"><?php echo $product['skidka']; ?>%</span>After which you can safely configure the remaining modules and pages that display the product.
Styling the percentage display in CSS is your homework. Just kidding, we think that if you can get into PHP + HTML code even at such a basic level, then stylizing a % discount will not be difficult :)
You can leave questions and suggestions in the comments!