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

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

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!

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

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.

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

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.

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

fn_register_hooks(
‘get_product_data_post’
);

\addons\my_changes\func.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}

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!

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

fn_register_hooks(
‘get_product_data_post’
);

\addons\my_changes\func.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.

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.

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:


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

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);

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\

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

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?

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.

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.

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.