Post by BrandEvo » Fri Nov 16, 2018 5:22 am

When I edit a .twig file and insert PHP.. I get an error.

Here is a snippet of PHP code from my old cart that I am trying to use on my 3.0.2.0 cart
/catalog/view/theme/default/template/product/product.tpl

Code: Select all

 <?php if(BONUS_CARD_ENABLE){ ?>
      <div id="bonus_card_info"></div>
      <i>$20 Bonus Card for every $100 gift card purchased will display on your final receipt after check out !</i>
      <?php } ?>
      </div>
      <?php } ?>
I want to add it to product.twig - located at /catalog/view/theme/default/template/product/product.twig

Here is the code I am inserting into product.twig

Code: Select all

 {% ifBONUS_CARD_ENABLE()%}
      <div id="bonus_card_info"></div>
      <i>$20 Bonus Card for every $100 gift card purchased will display on your final receipt after check out !</i>
      {% endif %}
      </div>
Obviously I need to add something more, I am not familiar with TWIG yet.


Fatal error: Uncaught exception 'Twig_Error_Syntax' with message 'Unknown "ifBONUS_CARD_ENABLE" tag in "default/template/product/product.twig" at line 45.' in /home/wpdemosystem/public_html/system/library/template/Twig/Parser.php:178 Stack trace: #0 /home/wpdemosystem/public_html/system/library/template/Twig/Parser.php(100): Twig_Parser->subparse(NULL, false) #1 /home/wpdemosystem/public_html/system/library/template/Twig/Environment.php(619): Twig_Parser->parse(Object(Twig_TokenStream)) #2 /home/wpdemosystem/public_html/system/library/template/Twig/Environment.php(671): Twig_Environment->parse(Object(Twig_TokenStream)) #3 /home/wpdemosystem/public_html/system/library/template/Twig/Environment.php(396): Twig_Environment->compileSource('{{ header }}\r\n<...', 'default/templat...') #4 /home/wpdemosystem/public_html/system/library/template/twig.php(33): Twig_Environment->loadTemplate('default/templat...') #5 /home/wpdemosystem/public_html/system/library/template.php(51): Template\Twig->render('default/templat...', '0') #6 /ho in /home/wpdemosystem/public_html/system/library/template/Twig/Parser.php on line 178

Newbie

Posts

Joined
Sat Dec 03, 2011 1:07 pm

Post by Johnathan » Fri Nov 16, 2018 6:03 am

You need a space between "if" and your custom constant BONUS_CARD_ENABLE.

Also, I'm not sure if you can use constants that way in Twig. If it doesn't work, then use a normal variable instead. You'd do that by adding a line like this in the /catalog/controller/product/product.php file:

Code: Select all

$data['bonus_card_enable'] = ...

filling in the data for that variable on the right-half of that equation. Then, you can use it in the .twig file like this:

Code: Select all

{% if bonus_card_enable %}

Image Image Image Image Image


User avatar
Administrator

Posts

Joined
Fri Dec 18, 2009 3:08 am


Post by ocmta » Fri Nov 16, 2018 4:59 pm

You can use constants in twig, but it should be like this:

Code: Select all

{% if constant('BONUS_CARD_ENABLE') %}

Extensions for affiliates (openCart 1, 2, 3):
Advanced Multi Level Affiliate System
Customer and Affiliate Accounts Combined into one Account
Affiliate Tracking with Coupons
Discount for Referred Customers - Order Total
Type Tracking Code
Mass Pay
Affiliate Transactions for openCart 3
Affiliate Pack X - all modules with 40% discount


Active Member

Posts

Joined
Mon Mar 12, 2012 11:21 am

Who is online

Users browsing this forum: mantolis, openalvin, pprmkr and 512 guests