Jump to content

Change Product Flag Discount Text from Percentage to Amount


Recommended Posts

Hi,

I'd like to change the product flag discount text from a percentage to an amount.

Is this possible?

So in this example, it would say "Save £3.60" rather than "-30%"

Can someone point me to the right file to do this?

Thanks

screenshot-www.spidawebs.co.uk-2020.03.13-18_30_04.png

Share this post


Link to post
Share on other sites

I've finally worked this out incase anyone else needs to know.

I changed the following in /src/Adapter/Presenter/Product/ProductLazyArray.php

from

        if ($show_price && $this->product['reduction']) {
            if ($this->product['discount_type'] === 'percentage') {
                $flags['discount'] = array(
                    'type' => 'discount',
                    'label' => $this->product['discount_percentage'],

to

        if ($show_price && $this->product['reduction']) {
            if ($this->product['discount_type'] === 'percentage') {
                $flags['discount'] = array(
                    'type' => 'discount',
                    'label' => $this->product['discount_amount_to_display'],

 

Ideally I would like to remove the minus sign infront of the price and add the word "SAVE" before it. Does anyone know how I can do this please?

screenshot-www.spidawebs.co.uk-2020.03.17-17_01_03.png

Share this post


Link to post
Share on other sites

Thanks, but I wanted to keep that as a percentage on the backend, because it is a lot easier to set prices that way. If I want to do a 30% sale, I can just set all products to 30% off, rather than having to adjust every price. But on the front end I prefer to show the amount saved.

Now I just need to find out how to remove the minus sign on the flag. Does anyone know?

Share this post


Link to post
Share on other sites
  • 2 months later...

Hi I'd like to do this the other way around. I would like to convert amount to percentage only. I configured my products to be amount discounted but would like to display percent. However when I tried to change the code:

if ($show_price && $this->product['reduction']) {
            if ($this->product['discount_type'] === 'percentage') {
                $flags['discount'] = array(
                    'type' => 'discount',
                    'label' => $this->product['discount_percentage'],
                );
            } elseif ($this->product['discount_type'] === 'amount') {
                $flags['discount'] = array(
                    'type' => 'discount',
                    'label' => $this->product['discount_percentage'],
                );
            } else {
                $flags['discount'] = array(
                    'type' => 'discount',
                    'label' => $this->translator->trans('Reduced price', array(), 'Shop.Theme.Catalog'),
                );
            }
        }

 

It gives me this:

image.png.74c93617cec248c3cad13b475a6cb9b5.png

Share this post


Link to post
Share on other sites
1 hour ago, Travel the World said:

Hi I'd like to do this the other way around. I would like to convert amount to percentage only. I configured my products to be amount discounted but would like to display percent. However when I tried to change the code:

if ($show_price && $this->product['reduction']) {
            if ($this->product['discount_type'] === 'percentage') {
                $flags['discount'] = array(
                    'type' => 'discount',
                    'label' => $this->product['discount_percentage'],
                );
            } elseif ($this->product['discount_type'] === 'amount') {
                $flags['discount'] = array(
                    'type' => 'discount',
                    'label' => $this->product['discount_percentage'],
                );
            } else {
                $flags['discount'] = array(
                    'type' => 'discount',
                    'label' => $this->translator->trans('Reduced price', array(), 'Shop.Theme.Catalog'),
                );
            }
        }

 

It gives me this:

image.png.74c93617cec248c3cad13b475a6cb9b5.png

That's because Prestashop WON'T do the math to switch from ₽1,720 to the actual discount percentage. It uses the input value and outputs formatting it with a % symbol.

You should try making the math yourself, then passing the variable to the array. Just use the correct variables. You need numeric variables, it will probably crash if you use string type variables, but give it a try. (can't try this one myself right now, and will probably not work)
 

            } elseif ($this->product['discount_type'] === 'amount') {
                $productDiscountperc = $this->product['discount_amount_to_display']/$this->product['price']*100;
                $flags['discount'] = array(
                    'type' => 'discount',
                    'label' => '-'.$productDiscountperc.'%',
                );
            } else {

 

  • Like 1

Share this post


Link to post
Share on other sites

I think I did it! 

5 hours ago, Luis C said:

That's because Prestashop WON'T do the math to switch from ₽1,720 to the actual discount percentage. It uses the input value and outputs formatting it with a % symbol.

You should try making the math yourself, then passing the variable to the array. Just use the correct variables. You need numeric variables, it will probably crash if you use string type variables, but give it a try. (can't try this one myself right now, and will probably not work)
 


            } elseif ($this->product['discount_type'] === 'amount') {
                $productDiscountperc = $this->product['discount_amount_to_display']/$this->product['price']*100;
                $flags['discount'] = array(
                    'type' => 'discount',
                    'label' => '-'.$productDiscountperc.'%',
                );
            } else {

 

Thank you! I managed with this:

elseif ($this->product['discount_type'] === 'amount') {

                $productDiscountperc = round($this->product['reduction']/$this->product['price_without_reduction']*100, 0);
                $flags['discount'] = array(
                    'type' => 'discount',
                    'label' => '-'.$productDiscountperc.'%',
                );
            } else {
                $flags

 

 

Share this post


Link to post
Share on other sites
1 minute ago, Travel the World said:

I think I did it! 

Thank you! I managed with this:

elseif ($this->product['discount_type'] === 'amount') {

                $productDiscountperc = round($this->product['reduction']/$this->product['price_without_reduction']*100, 0);
                $flags['discount'] = array(
                    'type' => 'discount',
                    'label' => '-'.$productDiscountperc.'%',
                );
            } else {
                $flags

 

 

Awesome! I'm glad I wasn't "too" wrong 😜

  • Like 1

Share this post


Link to post
Share on other sites
24 minutes ago, cricket-hockey said:

I'd still like to be able to remove the minus sign from before the discounted percent or amount. Does anyone know how to do that? So it shows "save £50" instead of "save -£50" etc

What you want to do, as I stated previously, can be done via template (and I'd say it's the best practice since it doesn't involve changing core or overriding anything)

 

You need to locate in your template file (product template or product miniature template depending on where you want to apply the change) the code where you output your discount. AFAIK, flags should be output using {$flag.label} so if yours got the text "save -XX" you should be able to find something like {l s='save'.........} {$flag.label}. If that's the case you can just do the following:

{if $flag.type == 'discount'}{$flag.label|substr:1}{else}{$flag.label}{/if}

However, if your string is formed elsewhere, you should do the substr there.

Share this post


Link to post
Share on other sites
  • 7 months later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More