Jump to content

Change Product Flag Discount Text from Percentage to Amount


Recommended Posts

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

  • Like 1
  • Thanks 1
Link to comment
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?

Link to comment
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

Link to comment
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
Link to comment
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

 

 

Link to comment
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
Link to comment
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.

Link to comment
Share on other sites

  • 7 months later...
  • 11 months later...
Dnia 17.03.2020 o 6:03 PM, cricket-hockey napisał:

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

And how to make the cart the same as in the product card?

 

1.jpg

2.jpg

Link to comment
Share on other sites

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...