Post by patrickbozic » Fri Oct 05, 2018 8:06 pm

Working with OpenCart 2.0.1.1. and the Journal 2 template, I have several products with bulk-order discounts. When displaying them in the product grid (category.tpl) I would like to display the lowest discount price instead of the regular price (if a product has a discount). I've understood the basics of OpenCart's MVC system, but I don't know the best way to access the discount data in the category.tpl file.

I've tried to access discounts data in category.php via:

Code: Select all

$discounts_data = $this->model_catalog_product->getProductDiscounts($result['product_id']);

$discounts = array();

foreach ($discounts_data as $discount) {
	$discounts[] = array(
		'quantity' => $discount['quantity'],
		'price'    => $this->currency->format($this->tax->calculate($discount['price'], $result['tax_class_id'], $this->config->get('config_tax')))
	);
}
and then passing $discounts into $data['products'] by adding 'discount' => $discounts

Then in category.tpl I've tried to access that data via $product['discount'], but it seems that no values get passed over from category.php. Does anyone have an idea how to do this?

Thanks a lot :)
Last edited by patrickbozic on Sat Nov 17, 2018 10:13 pm, edited 2 times in total.

Newbie

Posts

Joined
Mon Jun 25, 2018 7:32 pm
Location - Germany

Post by straightlight » Sat Oct 06, 2018 8:17 pm

patrickbozic wrote:
Fri Oct 05, 2018 8:06 pm
Working with OpenCart 2.0.1.1. and the Journal 2 template, I have several products with bulk-order discounts. When displaying them in the product grid (category.tpl) I would like to display the lowest discount price instead of the regular price (if a product has a discount). I've understood the basics of OpenCart's MVC system, but I don't know the best way to access the discount data in the category.tpl file.

I've tried to access discounts data in category.php via:

Code: Select all

$discounts_data = $this->model_catalog_product->getProductDiscounts($result['product_id']);

$discounts = array();

foreach ($discounts_data as $discount) {
	$discounts[] = array(
		'quantity' => $discount['quantity'],
		'price'    => $this->currency->format($this->tax->calculate($discount['price'], $result['tax_class_id'], $this->config->get('config_tax')))
	);
}
and then passing $discounts into $data['products'] by adding 'discount' => $discounts

Then in category.tpl I've tried to access that data via $product['discount'], but it seems that no values get passed over from category.php. Does anyone have an idea how to do this?

Thanks a lot :)
The issue is once you created the array from the controller, you then need to extract the created $product['discount'] array in your TPL file. As an alternate solution, you can modify your $discounts array from the controller from:

Code: Select all

$discounts[]
to read:

Code: Select all

$discounts[$discount['product_id']]
Then, replace:

Code: Select all

'discount'    => $discounts
with:

Code: Select all

'discount' => (!empty($discounts[$result['product_id']]) ? $discounts[$result['product_id']] : array())
Then, you can use the: $product['discount']['quantity'] and $product['discount']['price'] from your TPL file with an IF statement.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by patrickbozic » Mon Oct 08, 2018 8:19 pm

straightlight wrote:
Sat Oct 06, 2018 8:17 pm

The issue is once you created the array from the controller, you then need to extract the created $product['discount'] array in your TPL file. As an alternate solution, you can modify your $discounts array from the controller from:

Code: Select all

$discounts[]
to read:

Code: Select all

$discounts[$discount['product_id']]
Then, replace:

Code: Select all

'discount'    => $discounts
with:

Code: Select all

'discount' => (!empty($discounts[$result['product_id']]) ? $discounts[$result['product_id']] : array())
Then, you can use the: $product['discount']['quantity'] and $product['discount']['price'] from your TPL file with an IF statement.
Thank you for the quick response. I tried to use your alternate solution, which I find very elegant, but it doesn't work yet. First I used the $product['discount']['price'] in category.tpl without an IF statement - didn't work.
After tweaking around a bit, I commented all my changes in the controller out to have a look at the original $product array in the TPL file. I did so, by using print_r ( $product ) and saw that it still has a key called discount with an empty value. So rightnow I'm wondering where this is coming from, since I undid my modifications to category.php. Any ideas about that? EDIT: I also tried to pass some static text into $data['products'] via 'discount' => "text", but it seems that, whatever I do in the controller, it gets overwritten by an empty value after somehow.

Newbie

Posts

Joined
Mon Jun 25, 2018 7:32 pm
Location - Germany

Post by straightlight » Mon Oct 08, 2018 9:42 pm

If you're adding a static text into the discount key and it's still not showing in the array, then it either means you are using an OCMod extension prioritizing over the category.php file or you are encountering server cache issues. FAQ: viewtopic.php?f=176&p=736120#p731004 . It may also be about misconfigured stored OC cache path as well.

Also ensure to edit the right category.php file - in accordance of the source and route path you are using.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON
Who is online

Users browsing this forum: No registered users and 148 guests