Jump to content

  • You cannot start a new topic
  • You cannot reply to this topic

Can't Execute Function In My_Changes Func.php - Unknown Modifier Rate Topic   - - - - -

 
  • invosis
  • Junior Member
  • Members
  • Join Date: 04-Apr 11
  • 26 posts

Posted 07 March 2018 - 08:53 PM #1

I'm trying to do something which appears to be really simple according to the documentation but I just can't see to get it working. 

 

All I want to do is create a php function in the my_changes addon which I can use in a my_changes template override.

 

 

Here is what I have:

 

\addons\my_changes\func.php

<?php

if (!defined('BOOTSTRAP')) { die('Access denied'); }

function fn_is_special_edition($product_id){
    return 'yay, this worked!'; 
}

?>

\addons\my_changes\hooks\product_data.post.tpl

...

{$is_special_edition = "1234"|fn_is_special_edition}

...

Returns error

Smarty Compiler: Syntax error in template

unknown modifier "fn_is_special_edition"  

I have the addon enabled (I've used TPL overrides for years) and I've cleared all caches.

 

I also looked at examples online and also already installed add-ons but just can't seem to work it out.

 

HELP!

 



 
  • tbirnseth
  • CS Cart Expert
  • Authorized Reseller
  • Join Date: 08-Nov 08
  • 10667 posts

Posted 08 March 2018 - 09:01 PM #2

Is the My Changes addon "Active" or "Disabled"?  Needs to be "Active".


EZ Merchant Solutions: Custom (USA based) B2B Development, Consulting, Development and Special Projects (get a quote here).
Commercial addons, payment methods and modifications to meet your business and operations needs.


 
  • invosis
  • Junior Member
  • Members
  • Join Date: 04-Apr 11
  • 26 posts

Posted 08 March 2018 - 09:04 PM #3

Is the My Changes addon "Active" or "Disabled"? Needs to be "Active".


Definitely active. I use a load of template overrides and they’re all working.

 
  • tbirnseth
  • CS Cart Expert
  • Authorized Reseller
  • Join Date: 08-Nov 08
  • 10667 posts

Posted 08 March 2018 - 09:33 PM #4

Have you tried older smarty syntax like:

{assign var="is_special_edition" value="1234"|fn_is_special_edition}

Have you tried just calling your function from a my_changes.php admin controller to ensure you don't have any typos, filename issues or other not-so-easily-seen issues?  I.e. ?dispatch=my_changes.test_my_function


EZ Merchant Solutions: Custom (USA based) B2B Development, Consulting, Development and Special Projects (get a quote here).
Commercial addons, payment methods and modifications to meet your business and operations needs.


 
  • oleg.gorshkov
  • CS-Cart Expert
  • Authorized Reseller
  • Join Date: 06-Dec 13
  • 2813 posts

Posted 09 March 2018 - 05:53 AM #5

Definitely active. I use a load of template overrides and they’re all working.

 

Hello!

 

It seems that you created post template incorrectly, it should be addons/my_changes/hooks/products/product_data.post.tpl

 Please check.

 

What about your function, you can use the 

fn_set_hook('get_product_data_post', $product_data, $auth, $preview, $lang_code);

hook to define is_special_edition variable in $product_data array and use it as $product.is_special_edition in the template.


Simtech Development | sales@simtechdev.com | www.simtechdev.com
CERTIFIED CS-CART PARTNER | LICENSES | DEVELOPMENT | ADD-ONS | DESIGN | UPGRADE

Mega SEO Package - All you need for your SEO


 
  • invosis
  • Junior Member
  • Members
  • Join Date: 04-Apr 11
  • 26 posts

Posted 12 March 2018 - 09:55 PM #6

Hello!

 

It seems that you created post template incorrectly, it should be addons/my_changes/hooks/products/product_data.post.tpl

 Please check.

 

What about your function, you can use the 

fn_set_hook('get_product_data_post', $product_data, $auth, $preview, $lang_code);

hook to define is_special_edition variable in $product_data array and use it as $product.is_special_edition in the template.

 

Hi Oleg,

 

Yes sorry, the template override is in products folder, sorry that was a typo. It's definitely being called.

 

Your solution does sound more elegant. I've given it a go but it's doesn't appear to be working. Am I missing something?

 

\addons\my_changes\init.php

<?php 

fn_register_hooks(
    'get_product_data_post'
 );

\addons\my_changes\func.php

<?php

if (!defined('BOOTSTRAP')) { die('Access denied'); }

function fn_my_changes_get_product_data_post(&$product_data, &$auth, &$preview, &$lang_code)
{
    $product_data['is_special_edition'] = 'yay';
}


\addons\my_changes\hooks\products\product_data.post.tpl

{if $product_data['is_special_edition'] == 'yay'}
	Special
{else}
	Nope - {$product_data['is_special_edition']} 
{/if}


 
  • tbirnseth
  • CS Cart Expert
  • Authorized Reseller
  • Join Date: 08-Nov 08
  • 10667 posts

Posted 13 March 2018 - 12:31 AM #7

Get rid of the quotes in your smarty code.  I.e. change it to:

{if $product_data[is_special_edition] == 'yay'}
  Special
{else}
  Nope - $product_data[is_special_edition]
{/if}

Also it's easier to use 'dot notation' like:

{if $product_data.is_special_edition == 'yay'}

Have fun!


EZ Merchant Solutions: Custom (USA based) B2B Development, Consulting, Development and Special Projects (get a quote here).
Commercial addons, payment methods and modifications to meet your business and operations needs.


 
  • oleg.gorshkov
  • CS-Cart Expert
  • Authorized Reseller
  • Join Date: 06-Dec 13
  • 2813 posts

Posted 13 March 2018 - 05:20 AM #8

 

Hi Oleg,

 

Yes sorry, the template override is in products folder, sorry that was a typo. It's definitely being called.

 

Your solution does sound more elegant. I've given it a go but it's doesn't appear to be working. Am I missing something?

 

\addons\my_changes\init.php

<?php 

fn_register_hooks(
    'get_product_data_post'
 );

\addons\my_changes\func.php

<?php

if (!defined('BOOTSTRAP')) { die('Access denied'); }

function fn_my_changes_get_product_data_post(&$product_data, &$auth, &$preview, &$lang_code)
{
    $product_data['is_special_edition'] = 'yay';
}


\addons\my_changes\hooks\products\product_data.post.tpl

{if $product_data['is_special_edition'] == 'yay'}
	Special
{else}
	Nope - {$product_data['is_special_edition']} 
{/if}

 

The function is correct. Just use $product_data.is_special_edition in the template as tbirnseth wrote.


Simtech Development | sales@simtechdev.com | www.simtechdev.com
CERTIFIED CS-CART PARTNER | LICENSES | DEVELOPMENT | ADD-ONS | DESIGN | UPGRADE

Mega SEO Package - All you need for your SEO


 
  • invosis
  • Junior Member
  • Members
  • Join Date: 04-Apr 11
  • 26 posts

Posted 22 March 2018 - 07:11 PM #9

Hi guys, thanks for the help. I adjusted smarty template to use $product_data.is_special_edition and it's still empty  :( (but at least it doesn't error)

 

Could it be that the PHP hook get_product_data_post is triggered after my template hook product_data.post.tpl? So basically $product_data['is_special_edition'] is not set until after it's called in product template?

 

Or I'm initialising the PHP hook wrong? The template hook is definitely working as I'm able to output to the product detail page.



 
  • tbirnseth
  • CS Cart Expert
  • Authorized Reseller
  • Join Date: 08-Nov 08
  • 10667 posts

Posted 22 March 2018 - 10:37 PM #10

A PHP hook is always called before a template hook (unless a template variable modifier is using fn_get_prduct_data).

If you have added your 'is_special_edition' column to the database in the cscart_products table, then if you look at the $product_data in your get_product_data_post hook, you should see it there.

 

You should then see it in a template variable of {$product_data.is_special_edition}

 

The flow is PHP code/hooks, then template code/hooks.

 

in your product_data.post.tpl file you should be able to do:

<!-- My Product data: {$product_data|print_r:true} -->

and then go look for that comment in the resulting page.


EZ Merchant Solutions: Custom (USA based) B2B Development, Consulting, Development and Special Projects (get a quote here).
Commercial addons, payment methods and modifications to meet your business and operations needs.


 
  • eComLabs
  • CS-Cart Expert
  • Authorized Reseller
  • Join Date: 27-Jan 14
  • 17599 posts

Posted 23 March 2018 - 03:48 PM #11

Hi guys, thanks for the help. I adjusted smarty template to use $product_data.is_special_edition and it's still empty  :( (but at least it doesn't error)

 

Could it be that the PHP hook get_product_data_post is triggered after my template hook product_data.post.tpl? So basically $product_data['is_special_edition'] is not set until after it's called in product template?

 

Or I'm initialising the PHP hook wrong? The template hook is definitely working as I'm able to output to the product detail page.

 

Did you copied and pasted suggested code? Try to temporary add the following line to the fn_my_changes_get_product_data_post function and check

fn_print_r(123123);

GET A FREE QUOTE | CS-Cart Add-ons | CS-Cart Licenses | CS-Cart Development | CS-Cart Design | Server Configuration | UniTheme and YOUPI
CS-Cart                USD 345     Multi-Vendor              USD 1250    CS-Cart RU                         24500 руб.
CS-Cart Ultimate  USD 775     CS-Cart + YOUPI      USD 545      CS-Cart RU + UniTheme    36000 руб.


 
  • straygecko
  • Advanced Member
  • Members
  • Join Date: 01-May 13
  • 92 posts

Posted 03 April 2018 - 02:55 PM #12

Hmmm.... Both your original test and the modified one based on input here seem to behave the same - func.php is not getting picked up, in the new test perhaps both init.php and func.php aren't getting picked up. You're not spelling out the complete path to files so lets start with basics to be sure you have init.php and func.php in the right place as you show both php and tpl hook code in just \addons. init.php and func.php go in \app\addons\my_changes\ Template overrides go in \design\themes\yourtheme\templates\addons\my_changes\

 
  • tbirnseth
  • CS Cart Expert
  • Authorized Reseller
  • Join Date: 08-Nov 08
  • 10667 posts

Posted 03 April 2018 - 06:32 PM #13

And the backend path for template files is design/backend/templates/addons/my_changes/.....


EZ Merchant Solutions: Custom (USA based) B2B Development, Consulting, Development and Special Projects (get a quote here).
Commercial addons, payment methods and modifications to meet your business and operations needs.


 
  • invosis
  • Junior Member
  • Members
  • Join Date: 04-Apr 11
  • 26 posts

Posted 03 April 2018 - 06:34 PM #14

Hmmm.... Both your original test and the modified one based on input here seem to behave the same - func.php is not getting picked up, in the new test perhaps both init.php and func.php aren't getting picked up. You're not spelling out the complete path to files so lets start with basics to be sure you have init.php and func.php in the right place as you show both php and tpl hook code in just \addons. init.php and func.php go in \app\addons\my_changes\ Template overrides go in \design\themes\yourtheme\templates\addons\my_changes\

 

Thank you! I finally have my custom function triggering. I really missed this somehow. It was confusing as there are definitely some init.php and func.php in other adds on under templates.

 

 

So my function runs, but adding a variable into product_data as suggested, does not work. Now that I can actually run and debug, I see that product_data in this example is actually a product id., not an array. Any ideas?



 
  • tbirnseth
  • CS Cart Expert
  • Authorized Reseller
  • Join Date: 08-Nov 08
  • 10667 posts

Posted 03 April 2018 - 06:37 PM #15

The template variable {$product_data} should ALWAYS be an array (even if it's empty).  My guess is that your php hook is clobbering the $product_data before it's being set as a template variable.


EZ Merchant Solutions: Custom (USA based) B2B Development, Consulting, Development and Special Projects (get a quote here).
Commercial addons, payment methods and modifications to meet your business and operations needs.


 
  • invosis
  • Junior Member
  • Members
  • Join Date: 04-Apr 11
  • 26 posts

Posted 03 April 2018 - 06:41 PM #16

The template variable {$product_data} should ALWAYS be an array (even if it's empty).  My guess is that your php hook is clobbering the $product_data before it's being set as a template variable.

 

Sorry I should have been more clear.

 

{$product_data} is populated, I can output it in my template and browse all the vars.

 

However, I don't get access to it in my function. As suggested in one of the earlier replies, I would be able to access $product_data in my PHP function and add a var to it. 

 

e.g 

function fn_my_changes_get_product_data_post(&$product_data, &$auth, &$preview, &$lang_code)
{
    $product_data['is_special_edition'] = 'yay';
}

But $product_data is actually the product id above. If I change parameter to product_id, product_data is still null.



 
  • invosis
  • Junior Member
  • Members
  • Join Date: 04-Apr 11
  • 26 posts

Posted 03 April 2018 - 07:20 PM #17

Sorry I should have been more clear.

 

{$product_data} is populated, I can output it in my template and browse all the vars.

 

However, I don't get access to it in my function. As suggested in one of the earlier replies, I would be able to access $product_data in my PHP function and add a var to it. 

 

e.g 

function fn_my_changes_get_product_data_post(&$product_data, &$auth, &$preview, &$lang_code)
{
    $product_data['is_special_edition'] = 'yay';
}

But $product_data is actually the product id above. If I change parameter to product_id, product_data is still null.

 

Sorry ignore me, I'd switched to pre when trying to resolve my issues. 

 

Thank you everybody.