Post by print-n-tees » Tue Oct 24, 2017 10:30 am

Greetings.

Im trying to make the Discount Prices on the product page into a table... which I figured out.

So the first column I have the Discount Quantity.
Second Column I have the Discount Price.
In a 3rd column I would like to show what the price would be with my storewide discount.

So for Example lets say my store has 10% Storewide Discount when a coupon is used. I want to show what the price would be IF they used the discount coupon in a table like this:
24 - $2.00 - or $1.80 with 10% Discount use code: SAVE10
48 - $1.50 - or $1.35 with 10% Discount use code: SAVE10
96 - $1.00 - or $0.90 with 10% Discount use code: SAVE10

I got everything figured out except how to do the math.... showing the price with 10% off. I tried this:

Code: Select all

{% set Number = discount.price * 0.9 %}
or {{ Number }} with 10% Discount use code: SAVE10
when calling in the variable for the discount.price I get only a 0 (zero) as the number.

I think its because the discount.price has the USD Symbol as part of the variable. I tested this by changing the discount.price to discount.quantity and then it worked just fine... it took my quantity of 24 and multiplied by .90 and it worked great. But does not work with the discount.price variable... and im guessing its because of the "$" symbol.

Any ideas?

This is exactly what I am trying to achieve:

Code: Select all

{% set Number = {{ discount.price }} * .90 %}
Qty {{ discount.Quantity }}  -   {{ discount.price }}  -  or {{ Number }} with 10% Discount - Use Code: SAVE10
I cant figure out how to use the discount.price... or is there a way to remove the "$" symbol from the discount.price so it will work?

Angelina

User avatar
New member

Posts

Joined
Sun Feb 24, 2013 2:54 pm


Post by uksitebuilder » Tue Oct 24, 2017 4:43 pm

Your result of 0 will be due to the currency symbol.

Ideally, you should do your calculations in the controller.

However, you may be able to cheat and do it in the template, e.g.

Code: Select all

set Number = (discount.Price|trim('$', 'left') * 0.90)
Replace '$' with the currency symbol of your choosing)

User avatar
Guru Member

Posts

Joined
Thu Jun 09, 2011 11:37 pm
Location - United Kindgom

Post by print-n-tees » Wed Oct 25, 2017 12:31 am

Thank you UKSiteBuilder... However your line of code did not work... I got the following error using the code as you provided:

Warning: trim() expects at most 2 parameters, 3 given in /home/printingparties/public_html/system/library/template/Twig/Environment.php(403) : eval()'d code on line 1031 or 0 with 10% Discount - Use Code: SAVE10

User avatar
New member

Posts

Joined
Sun Feb 24, 2013 2:54 pm


Post by uksitebuilder » Wed Oct 25, 2017 12:48 am

That's strange.

Just need to play with the parenthesis or set another variable just for the initial discount price trimmed and then do the calculation afterwards

e.g.

Code: Select all

set numericDiscount = discount.Price|trim('$', 'left');
set Number = numericDiscount * 0.90;

User avatar
Guru Member

Posts

Joined
Thu Jun 09, 2011 11:37 pm
Location - United Kindgom

Post by print-n-tees » Wed Oct 25, 2017 12:52 am

OMG I was just thinking if the trim had to be done on its own. I tried just viewing the trim itself and got the same error:

Code: Select all

{{  discount.Price|trim('$', 'left') }}
But I will give what you just said a try.

User avatar
New member

Posts

Joined
Sun Feb 24, 2013 2:54 pm


Post by print-n-tees » Wed Oct 25, 2017 1:00 am

Ok tried it as:

{% set numericDiscount = discount.Price|trim('$', 'left') %}
{% set Number = numericDiscount * 0.90 %}
or {{ Number }} with discount

Fatal crashed will all sorts of errors.

User avatar
New member

Posts

Joined
Sun Feb 24, 2013 2:54 pm


Post by print-n-tees » Wed Oct 25, 2017 1:17 am

OKSiteBuilder... Could you do this in the controller with FTP Access for a fee? PM me. I tried to PM you but could not.

Angelina

User avatar
New member

Posts

Joined
Sun Feb 24, 2013 2:54 pm


Post by uksitebuilder » Wed Oct 25, 2017 2:23 am

catalog/controller/product/product.php

find:

Code: Select all

$data['discounts'] = array();

			foreach ($discounts as $discount) {
				$data['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'])
				);
			}
Change to:

Code: Select all

$data['discounts'] = array();

			foreach ($discounts as $discount) {
				$data['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']),
					'discountedprice'    => $this->currency->format($this->tax->calculate(($discount['price']*0.9), $product_info['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency'])
				);
			}
Then you can access the discount.discountedprice in your template

User avatar
Guru Member

Posts

Joined
Thu Jun 09, 2011 11:37 pm
Location - United Kindgom

Post by print-n-tees » Wed Oct 25, 2017 2:43 am

Thank you again. I made the suggested changes and calling the new variable... but it is not showing. Im thinking it may be a cache issue... I tried clearing the theme cache and the sass cache... but still the page loads the same. Do you see the new discount pricing show:

http://printingforparties.com/index.php ... uct_id=333

User avatar
New member

Posts

Joined
Sun Feb 24, 2013 2:54 pm


Post by uksitebuilder » Wed Oct 25, 2017 2:55 am

Image

Attachments

Screen Shot 2017-10-24 at 19.53.58.png

Screen Shot 2017-10-24 at 19.53.58.png (167.75 KiB) Viewed 4439 times


User avatar
Guru Member

Posts

Joined
Thu Jun 09, 2011 11:37 pm
Location - United Kindgom

Post by print-n-tees » Wed Oct 25, 2017 3:09 am

Yes... thats what I get so its still not working. there should be 2 prices...

24 - $1.09 or $0.99 with Discount

User avatar
New member

Posts

Joined
Sun Feb 24, 2013 2:54 pm


Post by print-n-tees » Wed Oct 25, 2017 3:12 am

In the controller products.php file I have the code you provided.

In the template I have:

Code: Select all

<table><TR> <TD colspan=3>THIS IS A TEST</TD></TR>
                {% for discount in discounts %}
                <TD>
                 {{ discount.quantity }}</TD><TD>{{ discount.price }}</TD><TD>
               
              	 or {{ discount.discountedprice }} with discount
                
              </TD></TR>
                {% endfor %}</TABLE>

User avatar
New member

Posts

Joined
Sun Feb 24, 2013 2:54 pm


Post by straightlight » Wed Oct 25, 2017 3:37 am

print-n-tees wrote:
Wed Oct 25, 2017 1:00 am
Ok tried it as:

{% set numericDiscount = discount.Price|trim('$', 'left') %}
{% set Number = numericDiscount * 0.90 %}
or {{ Number }} with discount

Fatal crashed will all sorts of errors.
What the error messages that you see when entering the discounted price key into the TWIG file?

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 print-n-tees » Wed Oct 25, 2017 3:56 am

When trying to use the trim tag... I got

Warning: trim() expects at most 2 parameters, 3 given in /home/printingparties/public_html/system/library/template/Twig/Environment.php(403) : eval()'d code on line 1031 or 0 with 10% Discount - Use Code: SAVE10

That was the error messages.. I would get one for each instance of a discount price.

However...

I also tried other things with that coding and trying to remove the $ symbol.... like just trying to show the price without the $ symbol.
All did not work.

User avatar
New member

Posts

Joined
Sun Feb 24, 2013 2:54 pm


Post by straightlight » Wed Oct 25, 2017 4:22 am

In catalog/controller/product/product.php file,

replace the previous step from the previous post with:

Code: Select all

'discounted_price'    => ($discounted_price = $this->currency->format($this->tax->calculate((trim($discount['price']) * 0.9), $product_info['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']) && !empty($discounted_price) ? preg_replace('/[^0-9\,\.]/', '$1', $discounted_price) : ''), 
when adding the discounted_price field in the array.

As for your catalog/view/theme/<your_theme>/template/product/product.twig file, the location was not mentioned specifically where you added the tags but it would rather be recommended to add like the following way:

Code: Select all

{% if discounted_price %}
{{ discounted_price }}
{% endif %}
along with your or text. In addition, I would also suggest to add your custom text in the language definition rather than from the template directly.

In your catalog/language/<your_language_code>/product/product.php file, you could add an array such as this example:

Code: Select all

$_['text_discounted_price'] = 'Discounted Price: ';
Then, in your TWIG file, you could use:

Code: Select all

{% if discounted_price %}
{{ text_discounted_price }} {{ discounted_price }}
{% endif %}
This should resolve the issue.
Last edited by straightlight on Wed Oct 25, 2017 5:23 am, edited 1 time in total.

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 print-n-tees » Wed Oct 25, 2017 5:08 am

Thanks I will give this a try when I get back home. Thank you!!

User avatar
New member

Posts

Joined
Sun Feb 24, 2013 2:54 pm


Post by print-n-tees » Wed Oct 25, 2017 7:08 am

OMG This is so friggen frustrating.

I go into: catalog/controller/product/product.php
Make the changes... and then save... and then make the changes for client side in:
catalog/view/theme/journal2/template/product/product.twig

And no matter what I change in the controller file... it takes but nothing happens. So I make the changes... save and close out the file... open the file up again and see the changes... but the changes have no effect. I even removed the entire section for the discounts... and save it... check to make sure the changes stick and they do. Go to the shop and the friggen discounts still show and they should not. So Im thinking... Maybe I should not be editing the catalog/controller/product/product.php so I rename it to product2.php just to see if the page still loads with the discounts and then I get an error. So its definitely the right page... but none of my changes are taking effect. Am I missing a cache setting somewhere?

I know the cog wheel has the theme and sass cache there and i do those and no effect.

User avatar
New member

Posts

Joined
Sun Feb 24, 2013 2:54 pm


Post by straightlight » Wed Oct 25, 2017 7:13 am

Am I missing a cache setting somewhere?
Ensure to clear both caches from your admin dashboard page as well for your extensions. Then, clear out the browser cache and also try with another browser to see if the issue persists. Also ensure from your admin products page that you are editing the right product by referring to the product search filter as editing the right product ID by referring to the store-front end on the URL where you see the discount on the page - also useful to check the product ID on the URL when not using SEO links in this particular case. Since Opencart does allow the possibility to copy a product over another category, it may be possible that you may have edited a duplicated product but without referring to the product ID if you do have copied products on your store. ;)

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 print-n-tees » Wed Oct 25, 2017 7:33 am

I cleared the cache a couple of times on the theme... sass and in journal there is a clear cache tool... and I turned off the chace system in journal2 Then I refreshed the modifications to clear cache on extensions. Cleared cache on my browser here on my mac. I even went in and asigned a SEO url for the product (only copy of it)... so everything is fresh. Load the page and still the discounted info does not show up correctly: http://printingforparties.com/test-001

You will see only the original 2 columns... not tot the discounted pricing.

I loaded it on my PC as well same result... and if you see the same... its definitely not a cache problem I would think but I dont know.

Here is what I have in the catalog/controller/product/product.php file

Code: Select all

$discounts = $this->model_catalog_product->getProductDiscounts($this->request->get['product_id']);

			$data['discounts'] = array();

			foreach ($discounts as $discount) {
				$data['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']),
					'discounted_price'    => ($discounted_price = $this->currency->format($this->tax->calculate((trim($discount['price']) * 0.9), $product_info['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']) && !empty($discounted_price) ? preg_replace('/[^0-9\,\.]/', '$1', $discounted_price) : ''), 
				);
			}
And what I have in catalog/view/theme/journal2/template/product/product.twig

Code: Select all

<table><TR> <TD colspan=3>THIS IS A TEST</TD></TR>
                {% for discount in discounts %}
                
                <TD>
                 {{ discount.quantity }}</TD><TD>{{ discount.price }}</TD><TD>
               
              	{% if discounted_price %}
              	{{ discounted_price }}
				{% endif %}
                
               
                
                </TD></TR>
                {% endfor %}</TABLE>
So I should have 3 columns when viewed on client side.

Again... even if I remove all references of the discounts from the controller page... I still get the same results when I should not.

User avatar
New member

Posts

Joined
Sun Feb 24, 2013 2:54 pm


Post by print-n-tees » Wed Oct 25, 2017 7:35 am

Even on my phone I get the same 2 columns. This is going to make me pull my friggen hair out.

User avatar
New member

Posts

Joined
Sun Feb 24, 2013 2:54 pm

Who is online

Users browsing this forum: No registered users and 21 guests