Post by talentive » Fri Sep 15, 2023 10:59 pm

Hi,
I am using OC version 3.0.3.8 and trying to display discount on product page by subtracting special from price. Here are the modifications that I have made:

In catalog\controller\product\product.php

After this line:

Code: Select all

$special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
I have added:

Code: Select all

$disPercentage = ((($result['price']-$result['special'])/$result['price']) * 100);
$disPrice = $result['price']-$result['special'];
After this line:

Code: Select all

$special = false;
I have added:

Code: Select all

$disPercentage = false;
$disPrice = false;
After this line:

Code: Select all

$data['special'] = $this->currency->format($this->tax->calculate($product_info['special'], $product_info['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
I have added:

Code: Select all

$disPercentage = ((($product_info['price']-$product_info['special'])/$product_info['price']) * 100);
$disPrice = $product_info['price']-$product_info['special'];

And finally, after this line:

Code: Select all

'special'     => $special,
I have added:

Code: Select all

'disPercentage' => $disPercentage,
'disPrice' => $disPrice,
Now, In catalog\view\theme\default\template\product\product.twig
After this line:

Code: Select all

<li><a class="thumbnail" href="{{ popup }}" title="{{ heading_title }}"><img src="{{ thumb }}" title="{{ heading_title }}" alt="{{ heading_title }}" /></a></li>
I add this:

Code: Select all

{% if special %}
                <div style="position: absolute;
    top: 10px;
    left: 10px;
    background-color: red;
    font-weight: 600;
    font-size: 14px;
    color: white;
    padding: 5px 10px;
    border-radius: 0px 20px 20px 0px;
    /* transform: rotate(-45deg); */
    z-index: 99;">
                
              $ {{ disPrice }} off
            </div>
        {% endif %}
Now, on product, label just Shows $ off
disPrice is not getting value from controller to twig.

I have tried clearing cache from modification page, from blue gear in dashboard, from system/storage but to no avail. I am still not getting anything. I have tried hardcoding disPrice in controller to eliminate if there is any error in calculation, still noting. If I replace disPrice with special in twig, I get special price string.

I have applied same modification on category.php and category.twig and it works.

Please help
Last edited by talentive on Tue Sep 19, 2023 8:55 pm, edited 1 time in total.

New member

Posts

Joined
Tue Nov 10, 2020 2:04 am

Post by ADD Creative » Fri Sep 15, 2023 11:09 pm

You need to be setting $data['disPrice'] for the template. It looks like you are modifying the code for the related products and not the main product.

www.add-creative.co.uk


Guru Member

Posts

Joined
Sat Jan 14, 2012 1:02 am
Location - United Kingdom

Post by talentive » Fri Sep 15, 2023 11:13 pm

I am trying to display discount on product's own page. I have not made any change in model files. Where do I set the variable you mentioned

New member

Posts

Joined
Tue Nov 10, 2020 2:04 am

Post by talentive » Fri Sep 15, 2023 11:20 pm

Right, I got it, instead of this:

Code: Select all

$disPercentage = ((($product_info['price']-$product_info['special'])/$product_info['price']) * 100);
$disPrice = $product_info['price']-$product_info['special'];
I used this:

Code: Select all

$data['disPercentage'] = ((($product_info['price']-$product_info['special'])/$product_info['price']) * 100);
$data['disPrice'] = $product_info['price']-$product_info['special'];
And now it works beautifully. Thanks for guidance. For my understanding, can you please explain why I had to do that? Because underneath this is

Code: Select all

$tax_price = (float)$product_info['special'];
and it is working fine without addition of

Code: Select all

$data['']

New member

Posts

Joined
Tue Nov 10, 2020 2:04 am

Post by ADD Creative » Fri Sep 15, 2023 11:25 pm

Somewhere in or just after the following block of code. https://github.com/opencart/opencart/bl ... #L294-L312

See how all the variables used by the template are set as $data['price'] =, $data['special'] =, etc.

www.add-creative.co.uk


Guru Member

Posts

Joined
Sat Jan 14, 2012 1:02 am
Location - United Kingdom

Post by Huiu » Sat Sep 16, 2023 2:37 am

You solved?

Dezvoltator Web Full Stack - Specializat în Dezvoltare și Suport OpenCart în România
Contactează-mă pentru Lucrări Personalizate sau Suport Rapid.


User avatar
Newbie

Posts

Joined
Mon Mar 16, 2020 1:52 am
Location - Romania

Post by talentive » Tue Sep 19, 2023 1:01 am

Huiu wrote:
Sat Sep 16, 2023 2:37 am
You solved?
Yes, thanks

New member

Posts

Joined
Tue Nov 10, 2020 2:04 am

Post by paulfeakins » Tue Sep 19, 2023 7:21 pm

talentive wrote:
Tue Sep 19, 2023 1:01 am
Huiu wrote:
Sat Sep 16, 2023 2:37 am
You solved?
Yes, thanks
So please add [SOLVED] to the start of the post title.

UK OpenCart Hosting | OpenCart Audits | OpenCart Support - please email info@antropy.co.uk


User avatar
Legendary Member
Online

Posts

Joined
Mon Aug 22, 2011 11:01 pm
Location - London Gatwick, United Kingdom

Post by talentive » Tue Sep 19, 2023 8:55 pm

paulfeakins wrote:
Tue Sep 19, 2023 7:21 pm
talentive wrote:
Tue Sep 19, 2023 1:01 am
Huiu wrote:
Sat Sep 16, 2023 2:37 am
You solved?
Yes, thanks
So please add [SOLVED] to the start of the post title.
Done

New member

Posts

Joined
Tue Nov 10, 2020 2:04 am

Post by talentive » Tue Sep 19, 2023 8:55 pm

paulfeakins wrote:
Tue Sep 19, 2023 7:21 pm
talentive wrote:
Tue Sep 19, 2023 1:01 am
Huiu wrote:
Sat Sep 16, 2023 2:37 am
You solved?
Yes, thanks
So please add [SOLVED] to the start of the post title.
Done

New member

Posts

Joined
Tue Nov 10, 2020 2:04 am
Who is online

Users browsing this forum: No registered users and 24 guests