I am new to Opencart and I am trying to display the quantity discount in the featured products - without success. I am using Opencart 2.3. Basically, what I would like to achieve is to pass the variables quantity
and price
from the controller file featured.php
to the view file featured.tpl
.
Here is what I have tried:
1) In the file /catalog/controller/extension/module/featured.php
after the $product_info = $this->model_catalog_product->getProduct($product_id);
I added the following code:
$discounts = $this->model_catalog_product->getProductDiscounts($product_id);
$data['discounts'][] = array();
foreach ($discounts as $discount) {
$data['discounts'][] = array(
'quantity' => $discount['quantity'],
'price' => $discount['price']
);
}
2) In the file /catalog/view/theme/default/template/extension/module/featured.tpl
I added the following code:
<?php foreach ($discounts as $discount) { ?>
<span>
<?php echo sprintf($text_discount, $discount['quantity'], $discount['price']); ?>
</span><br>
<?php } ?>
Any help would be very much appreciated!
I have tried several other variations of code (eg the solution provided here ), but no success.
Go to catalog\\language\\en-gb\\extension\\module\\featured.php Add following code:
$_['text_discount'] = ' or more ';
Go to catalog\\controller\\extension\\module\\featured.php Find following lines of code:
$data['heading_title'] = $this->language->get('heading_title');
Add following lines of code:
$data['text_discount'] = $this->language->get('text_discount');
Find following lines of code:
foreach ($products as $product_id) {
$product_info = $this->model_catalog_product->getProduct($product_id);
Add following lines of code:
$discounts = $this->model_catalog_product->getProductDiscounts($product_id);
$product_info['discounts'] = array();
foreach ($discounts as $discount) {
$product_info['discounts'][] = array(
'quantity' => $discount['quantity'],
'price' => $this->currency->format($this->tax->calculate($discount['price'], $product_info['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency'])
);
}
Find following lines of code:
$data['products'][] = array(
'product_id' => $product_info['product_id'],
Just below it Add following lines of code:
'discounts'=>$product_info['discounts'],
Now go to catalog\\view\\theme\\YOUR_ACTIVE_THEME\\template\\extension\\module\\featured.tpl
Find following lines of code:
<div class="button-group">
<button type="button" onclick="cart.add('<?php echo $product['product_id']; ?>');">
Add above it or where ever you want to show but need to inside the product array:
<?php if ($product['discounts']) { ?>
<ul>
<hr>
<?php foreach ($product['discounts'] as $discount) { ?>
<li><?php echo $discount['quantity']; ?><?php echo $text_discount; ?><?php echo $discount['price']; ?></li>
<?php } ?>
</ul>
<?php } ?>
You can download the ocmod at Show Discounts at featured module Ocmod OpenCart 2.3.02
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.