Post by skip » Fri Jun 21, 2019 8:30 pm

How to convert this line from product tpl to product twig file ?

Code: Select all

<div class="price2" style="font-size:17px;"> </br> <?php   $number = preg_replace("/[^0-9\.]/", '', $price);   
$total= $number*1.2; echo "List price: "."$".number_format($total, 2);  ?> </div> 
 <h2><?php echo "Your price ". $price; ?></h2>
thanks

Active Member

Posts

Joined
Mon May 09, 2011 9:57 pm

Post by straightlight » Fri Jun 21, 2019 8:43 pm

No OC version posted and incomplete code statement since the $price variable has not been indicated where it originates from due to variable replacement.

However,

Code: Select all

<div class="price2" style="font-size:17px;"><br>
{% if number %}
	{{ number }}
{% endif %}
Then, in your catalog/controller/product/product.php file, in the index() method, you could add below $price variable IF condition:

Code: Select all

$data['number'] = $this->currency->format($price, $this->config->get('config_currency'), false, false);
instead of:

Code: Select all

$number = preg_replace("/[^0-9\.]/", '', $price);
Then,

Code: Select all

{% set total = number * 1.2 %}
List price: {{ total|number_format(2, '.', '') }}</div>
<h2>Your price: {{ price }}</h2>

The most generated errors being found on Opencart forum originates from contributed programming. The increased post counters are caused by redundancies of the same solutions that were already provided prior.


Regards,
Straightlight
Opencart.com Administrator / Quality Assurance Analyst / Programmer


Legendary Member

Posts

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

Post by skip » Fri Jun 21, 2019 11:22 pm

$number and $total is variables defined directly in view tpl file (there is no code in controller at all) Version of OC is 2302 , and Im trying convert code for 3032

Code: Select all

<div class="price2" style="font-size:17px;"> </br> <?php   
$number = preg_replace("/[^0-9\.]/", '', $price);   
$total= $number*1.2; echo "List price: "."$".number_format($total, 2);  ?> </div>

Active Member

Posts

Joined
Mon May 09, 2011 9:57 pm

Post by straightlight » Fri Jun 21, 2019 11:45 pm

You'd still need to define your PHP variables into your controllers or, as an alternative, to use a custom function in order to use preg_replace in TWIG files. Another approach you could use is the TPL Engine from the Marketplace in order to use TPL files into OC v3.0.3.2 rather than using TWIG files.

The most generated errors being found on Opencart forum originates from contributed programming. The increased post counters are caused by redundancies of the same solutions that were already provided prior.


Regards,
Straightlight
Opencart.com Administrator / Quality Assurance Analyst / Programmer


Legendary Member

Posts

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

Post by letxobnav » Sat Jun 22, 2019 11:02 am

you should never put data logic in a tpl or twig file, always in the controller.

“Data security is paramount at [...], and we are committed to protecting the privacy of anyone who is associated with our [...]. We’ve made a lot of improvements and will continue to make them.”
When you know your life savings are gone.


Active Member

Posts

Joined
Fri Aug 18, 2017 4:35 pm
Location - Taiwan

Post by straightlight » Sat Jun 22, 2019 8:17 pm

You'd still need to define your PHP variables into your controllers
letxobnav wrote:
Sat Jun 22, 2019 11:02 am
you should never put data logic in a tpl or twig file, always in the controller.
Redundancy. However, the data array don't seem to be used according to the examples the OP has posted, so far, fortunately.

The most generated errors being found on Opencart forum originates from contributed programming. The increased post counters are caused by redundancies of the same solutions that were already provided prior.


Regards,
Straightlight
Opencart.com Administrator / Quality Assurance Analyst / Programmer


Legendary Member

Posts

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

Post by thekrotek » Sat Jun 22, 2019 8:33 pm

skip wrote:
Fri Jun 21, 2019 11:22 pm
$number and $total is variables defined directly in view tpl file (there is no code in controller at all) Version of OC is 2302 , and Im trying convert code for 3032

Code: Select all

<div class="price2" style="font-size:17px;"> </br> <?php   
$number = preg_replace("/[^0-9\.]/", '', $price);   
$total= $number*1.2; echo "List price: "."$".number_format($total, 2);  ?> </div>
TPL is basically PHP, so you can use any PHP functions in it. TWIG is a stupid template engine with its own syntax and limited coding options. In most cases you will have to move any kind of calculations from TPL to controller, like in example above.

Professional OpenCart extensions, support and custom work.
Contact me via email or Skype by support@thekrotek.com


User avatar
Expert Member
Online

Posts

Joined
Sun Jul 03, 2016 12:24 am


Post by skip » Mon Jun 24, 2019 10:56 pm

Ok understand , please give me example how to define one basic variable in controller, for example

Code: Select all

$number=2* $price ;        
and on which place in controller I can set it for 3032 version - index() funkction is 500 rows long.
Thanks

Active Member

Posts

Joined
Mon May 09, 2011 9:57 pm

Post by straightlight » Mon Jun 24, 2019 11:27 pm

skip wrote:
Mon Jun 24, 2019 10:56 pm
Ok understand , please give me example how to define one basic variable in controller, for example

Code: Select all

$number=2* $price ;        
and on which place in controller I can set it for 3032 version - index() funkction is 500 rows long.
Thanks
Below the $price variable IF statement in the controller, you could add:

Code: Select all

$data['number'] = $price * 2;
Then, use the {{ number }} in your product.twig file.

The most generated errors being found on Opencart forum originates from contributed programming. The increased post counters are caused by redundancies of the same solutions that were already provided prior.


Regards,
Straightlight
Opencart.com Administrator / Quality Assurance Analyst / Programmer


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 4 guests