pass variale through hook

I am using hook named “orders:items_list_row” in “customer\views\orders\details.tpl”



in this hook, I have created new variale $sum_prices_exc_vat

{cycle values=",class=\"table-row\"" name="class_cycle" assign="_class"}
{assign var='sum_prices_exc_vat' value=`$sum_prices_exc_vat+$product.base_price`}


Now I need to pass calculated $sum_prices_exc_vat variable to details.tpl to display its value





Thanks in davance

Check smarty manual I think capture might be what your looking for.

Hi,



I think that calculation should be made in the controller php file rather than in the tpl one, and then passed to the tpl file with the following method :


$view->assign('your_smarty_variable', $your_php_variable)

stephR is correct. The capture tag is just a way to make something a global variable. It does not create good programming structure.

Thanks for all of you.



I have ended with this code



in controllers\admin\orders.php after

} elseif ($mode == 'details') {



added:


foreach ($order_info['items'] as $k => $v) {
if (!empty($v['base_price']) && !empty($v['amount'])) {
$subtotal_exc_vat = $v['base_price']*$v['amount'];
$order_info['items'][$k]['subtotal_exc_vat'] = $subtotal_exc_vat;
$total_exc_vat = $total_exc_vat + $subtotal_exc_vat;
}
};
$order_info['total_exc_vat'] = $total_exc_vat;






Thanks

Actually you can do this without modifying any standard code.



Make your contorller addons/my_changes/orders.post.php and then add in your “if($mode == 'details')”; Youll probably need to extract your order_info array and save it back by:


$order_info = $view->get_var('order_info);


then after you've updated the data structure put it back with


$view->assign('order_info', $order_info);






This will ensure your changes don't get clobbered in the next upgrade you do.

It works. Just in case anyone is interested:



Tax calculation method based on Unit price.



create file: addons/my_changes/controllers/customer/orders.post.php (not in skins\basic\customer\addons.…)

with content:

if ($mode == 'details' || $mode == 'print_invoice') {
$order_info = $view->get_var('order_info');
foreach ($order_info['items'] as $k => $v) {
if (!empty($v['base_price']) && !empty($v['amount'])) {
$subtotal_exc_vat = $v['base_price']*$v['amount'];
$order_info['items'][$k]['subtotal_exc_vat'] = $subtotal_exc_vat;
$total_exc_vat = $total_exc_vat + $subtotal_exc_vat;
}
};
$order_info['total_exc_vat'] = $total_exc_vat;
$view->assign('order_info', $order_info);
}
?>




Thanks tbirnseth!

With this edit I assume the subtotal price will be shown excl. tax?



Thanks, this does make the end calculation more clear.

[quote name='Flow' timestamp='1330254532' post='132107']

With this edit I assume the subtotal price will be shown excl. tax?



Thanks, this does make the end calculation more clear.

[/quote]



Not actually. This code creates two additional smarty variables: subtotal_exc_vat and $total_exc_vat. It does not effect any templates by default.

You can access these variables in your tpl's when using orders controller to view or print orders.



Here is example:



[Product] [Price inc VAT(20%)] [Amount] [Total]

[product1] [120] [2] [240]

[product2] [60] [1] [60]



product1 subtotal_exc_vat will have value: 100(120-20%) x 2 = 200

product2 subtotal_exc_vat will have value: 50(60-20%) x 1 = 50

$total_exc_vat will have 200+50=250



BTW, its for 2.2.4, i suspect you are using earlier version?

If so, send me your “controllers\admin\orders.php” file and I will take a look for compatibility for you

Ah, i understand. didn't really look at it… just thought that would be it cause I know you're working on it.



But that's pretty handy… I might need that for another store i'm building, so thanks!

I have strange problem with 'print_invoice' mode.

Its look like, my “orders.post.php” does not execute at all when “dispatch=orders.print_invoice” is used.



Just for testing purpose I have changed content of “orders.post.php” to

$order_info['test1'] = "works";
$view->assign('order_info', $order_info)




and there is no 'test1' variable at all in “print_invoice.tpl” and “invoice.tpl”.



I suspect:

  1. my orders.post.php does not execute at all
  2. 'order_info' variable array reassigned somewhere



    Any ideas?

[quote name='tbirnseth' timestamp='1330201599' post='132086']

Actually you can do this without modifying any standard code.



Make your contorller addons/my_changes/orders.post.php and then add in your “if($mode == 'details')”; Youll probably need to extract your order_info array and save it back by:


$order_info = $view->get_var('order_info);


then after you've updated the data structure put it back with


$view->assign('order_info', $order_info);






This will ensure your changes don't get clobbered in the next upgrade you do.

[/quote]



Thanks I was wondering where the php hooks went.

@kemeris - I'd guess that when the invoice is printed that a redirect is being done before your controller is run. Hence, you might want to use a 'pre' controller versus a 'post' controller which should ensure that your controller is run BEFORE the standard controller for 'orders'.

[quote name=‘tbirnseth’ timestamp=‘1330731800’ post=‘132471’]

@kemeris - I’d guess that when the invoice is printed that a redirect is being done before your controller is run. Hence, you might want to use a ‘pre’ controller versus a ‘post’ controller which should ensure that your controller is run BEFORE the standard controller for ‘orders’.

[/quote]



thanks for suggestion, but this does not work.

In this case I need to execute

    $order_info = fn_get_order_info($_REQUEST['order_id']);
```<br />
in my "orders.pre.php" and remove this function call in "orders.php"<br />
In this case this is not hook based solution anymore <img src="upload://nMBtKsE7kuDHGvTX96IWpBt1rTb.gif" class="bbc_emoticon" alt=":-("><br />
<br />
I just looked at "fn_get_order_info" function located in fn.cart.php and found this statement:<br />
```php
fn_set_hook('get_order_info', $order, $additional_data);
```<br />
<br />
cs-cart docs describes this as "code hook" but there is no example how to use it and where to put my hook file.<br />
<br />
Can I make my calculations in this function's hook somehow?<br />
<br />
Thanks

Yes, you can do it there.

Add


fn_register_hooks('get_order_info');


to your addons/my_changes/init.php file.

Then add a function called fn_my_changes_get_order_info(&$order_info, &$added_info)

to your addons/my_changes/func.php file.



You can adjust the $order_info data to anything you want.



There's a whole tutorial on using PHP hooks in the developers section that I wrote quite a while ago. I think there are 6 or so separate threads. It should tell you everything you need to know about using PHP/code hooks.

[quote name='tbirnseth' timestamp='1330806239' post='132500']

There's a whole tutorial on using PHP hooks in the developers section that I wrote quite a while ago. I think there are 6 or so separate threads. It should tell you everything you need to know about using PHP/code hooks.

[/quote]



Thank you, just found your “PHP Controllers” posts, reading them right now. These are invaluable, really!

So, thanks to tbirnseth, here is latest version of this mod.

Please ignore all my previous posts with code, they are not needed.


  1. Create “addons/my_changes/init.php” with content:

fn_register_hooks('get_order_info');
?>




2. Create “addons/my_changes/func.php” with content:

```php

function fn_my_changes_get_order_info(&$order, &$additional_data) {
if( !defined('AREA') ) die('Access denied');

foreach ($order['items'] as $k => $v) {
if (!empty($v['base_price']) && !empty($v['amount'])) {
$subtotal_exc_vat = $v['base_price']*$v['amount'];
$order['items'][$k]['subtotal_exc_vat'] = $subtotal_exc_vat;
$total_exc_vat += $subtotal_exc_vat;
}
};
$order['total_exc_vat'] = $total_exc_vat;
}
?>

```





That's it, now variales “subtotal_exc_vat” and “total_exc_vat” will be available in every mode (details, print_invoice) of orders controller

You should have an ampersand (&) ahead of your variable names in your hook so that the data is passed by reference rather than by value. I.e.


fn_my_changes_get_order_info($order, $additional_data) {



should be


fn_my_changes_get_order_info(&$order, &$additional_data) {

[quote name='tbirnseth' timestamp='1330899073' post='132568']

You should have an ampersand (&) ahead of your variable names in your hook so that the data is passed by reference rather than by value. I.e.


fn_my_changes_get_order_info($order, $additional_data) {



should be


fn_my_changes_get_order_info(&$order, &$additional_data) {



[/quote]



Thanks!

great input tbirn