Adding New Variables To Variable List In Document

Hello, I'm trying to add variables to add variables to variable list in Invoice document.

I've read this part of manual, but I just don't understand how the variables are pulled from the database and linked to the variables that are available to choose from in document creation page.

In Tygh/Template/Document/Order/Variables/OrderVariable.php there is used $context->getOrder(); to get the data to the variables that can be used in the document. I can see in Tygh/Template/Document/Order/Context.php file that GetOrder() just returns the data of the order, but what actually is "order"? What information does it carry? How can I add more information to it from one of the tables in the database?

You can use the Suppliers add-on as an example. This add-on adds some new variables to the invoice editor.

According to the documentation, I'm supposed to be able to add a variable from the schema only by using the GenericVariable class.

I created the following schema in app/addons/coch/schemas/documents/order.post.tpl (I know it's being seen because I saw and corrected a sytnax error.)

However, there is no 'coch' variable showing up in the document editor.

What am I doing wrong. Or is the documentation wrong and I have to implement a couple of hundred lines of code just to make a single variable visible?

yes, the cache was cleared.

use Tygh\Registry;

$schema[‘order’][‘coch’] = array(
‘class’ => ‘\Tygh\Template\Document\Variables\GenericVariable’,
// ‘alias’ => ‘c’,
‘data’ => function (\Tygh\Template\Document\Order\Context $context) {
$order = $context->getOrder();
if( !empty($order[‘coch_amount’]) )
return array(‘coch_amount’=>$order[‘coch_amount’],
‘entity’ => $order[‘entity’],
‘coch_label’ => $order[‘coch_label’]);
return coch_get_order_data($order[‘order_id’]);
},
‘attributes’ => array(‘coch_amount’, ‘entity’, ‘coch_label’)
);
return $schema;

According to the documentation, I'm supposed to be able to add a variable from the schema only by using the GenericVariable class.

I created the following schema in app/addons/coch/schemas/documents/order.post.tpl (I know it's being seen because I saw and corrected a sytnax error.)

However, there is no 'coch' variable showing up in the document editor.

What am I doing wrong. Or is the documentation wrong and I have to implement a couple of hundred lines of code just to make a single variable visible?

yes, the cache was cleared.

use Tygh\Registry;

$schema[‘order’][‘coch’] = array(
‘class’ => ‘\Tygh\Template\Document\Variables\GenericVariable’,
// ‘alias’ => ‘c’,
‘data’ => function (\Tygh\Template\Document\Order\Context $context) {
$order = $context->getOrder();
if( !empty($order[‘coch_amount’]) )
return array(‘coch_amount’=>$order[‘coch_amount’],
‘entity’ => $order[‘entity’],
‘coch_label’ => $order[‘coch_label’]);
return coch_get_order_data($order[‘order_id’]);
},
‘attributes’ => array(‘coch_amount’, ‘entity’, ‘coch_label’)
);
return $schema;

I am not sure but I think it is impossible to have it in a 2d array. You must do something like $schema['var'] = array () (not $schema['var']['var']) After this you can add the alias 'order.c' to access the data as a 'subarray' in twig.

You're correct. I was going to test that (and got distracted). So I removed the 'orders' element so that it won't be under the 'o' data and it shows. Now all I have to do is figure out how to update a customer's document with this data during the addon install. The new editor is not very addon friendly for adding elements to the emails like lines in the 'totals' area of an order without potentially stomping on anything the customer might already have. Would certainly be nice if there were hooks that could be used for twig.

I see the variable and I see its elements in the variables list.

However, there's no data associated with them. I put diagnostic output in the anonymous function in the schema (like to log the order_id) but it's never being called when printing an invoice. I would expect to see the die() message when I go to print an invoice and it tries to resolve the data.... If I inject a syntax error then I see that failure, but the die() is never executed..... Perplexing.

And if I add 'XX' and 'YY' to the table cells, that data is never shown either.

If I try to 'preview' the document, it generates a fatal error like below, but only on preview, not at runtime in creating the invoice.:

PHP Fatal error: Uncaught Error: Cannot use object of type Tygh\Template\Document\Order\Order as array in

Grr....
$schema['coch'] = array(
  'class' => '\Tygh\Template\Document\Variables\GenericVariable',
  'data' => function (\Tygh\Template\Document\Order\Context $context) {
  $order = $context->getOrder();
die("Schema: order[order_id]=$order[order_id]");
  if( !empty($order['coch_amount']) ) {
return array('coch_amount'=>$order['coch_amount'],
      'entity' => $order['entity'],
      'coch_label' => $order['coch_label']);
    }
return coch_get_order_data($order['order_id']);
},
'attributes' => array('coch_amount', 'entity', 'coch_label')
);
return $schema;

I see the variable and I see its elements in the variables list.

However, there's no data associated with them. I put diagnostic output in the anonymous function in the schema (like to log the order_id) but it's never being called when printing an invoice. I would expect to see the die() message when I go to print an invoice and it tries to resolve the data.... If I inject a syntax error then I see that failure, but the die() is never executed..... Perplexing.

And if I add 'XX' and 'YY' to the table cells, that data is never shown either.

If I try to 'preview' the document, it generates a fatal error like below, but only on preview, not at runtime in creating the invoice.:

Grr....
$schema['coch'] = array(
  'class' => '\Tygh\Template\Document\Variables\GenericVariable',
  'data' => function (\Tygh\Template\Document\Order\Context $context) {
  $order = $context->getOrder();
die("Schema: order[order_id]=$order[order_id]");
  if( !empty($order['coch_amount']) ) {
return array('coch_amount'=>$order['coch_amount'],
      'entity' => $order['entity'],
      'coch_label' => $order['coch_label']);
    }
return coch_get_order_data($order['order_id']);
},
'attributes' => array('coch_amount', 'entity', 'coch_label')
);
return $schema;

I assume you cleared the cache? Please share your Twig code as well.

You're correct. I was going to test that (and got distracted). So I removed the 'orders' element so that it won't be under the 'o' data and it shows. Now all I have to do is figure out how to update a customer's document with this data during the addon install. The new editor is not very addon friendly for adding elements to the emails like lines in the 'totals' area of an order without potentially stomping on anything the customer might already have. Would certainly be nice if there were hooks that could be used for twig.

By the way, I think you can just include your own hook with ease. It uses the 'collection' class so just adding a hook including that should allow you to easily edit the data for twig. Perhaps you can post it in the hook request forum.

In one of our module our developers added new class which extends the OrderVariable one and add required variables to order. You can use this solution as well

In one of our module our developers added new class which extends the OrderVariable one and add required variables to order. You can use this solution as well

I was trying to avoid having to write 200 lines of code to make a variable available by using the documented GenericVariable class.

I assume you cleared the cache? Please share your Twig code as well.

Yes I cleared the cache. And give a syntax error occurs (whern present) when I select "Print invoice" it seems that it's seeing it.

However, I question whether the actual document with the changes is being used. I even added literal values outside my conditions and they don't show.

1 table row with two cells.

Cell 1:

XX{% if coch.coch_amount and coch.coch_label %}{{coch.coch.label}}{% endif %}

Cell 2

YY{% if coch.coch_amount %}
{{coch.coch_amount}}
{%endif%}

Note that XX and YY never appear and any logging (or die()) I put in the anonymous function that returns the values is never executed, but syntax errors are seen and no syntax errors from Twig are shown.

By the way, I think you can just include your own hook with ease. It uses the 'collection' class so just adding a hook including that should allow you to easily edit the data for twig. Perhaps you can post it in the hook request forum.

I've asked for hooks for things like the list of order variables that are available and was turned down.

I.e. it limits the list from cscart_orders to a small number of available fields (not in schema but embedded in the class). Hence if someone extends the table, there's no easy way to include that new field in the order data.

I find this new editor to be extremely frustrating to try to work with and very unfriendly to addons being able to insert info as needed.

In this specific case, the order is recalculated with an additional fee. That fee needs to be on the invoice so the subtotal + stuff == total. Without it, that equation will be off by the amount of the fee. And it has to be split across vendors too. In the templates version of email this is very easy and takes maybe 5 lines of html code and the addition of one file using hooks/orders/totals.post.tpl or hooks/orders/invoice_shipping.post.tpl

I've asked for hooks for things like the list of order variables that are available and was turned down.

I.e. it limits the list from cscart_orders to a small number of available fields. Hence if someone extends the table, there's no easy way to include that new field in the order data.

I find this new editor to be extremely frustrating to try to work with and very unfriendly to addons being able to insert info as needed.

In this specific case, the order is recalculated with an additional fee. That fee needs to be on the invoice so the subtotal + stuff == total. Without it, that equation will be off by the amount of the fee. And it has to be split across vendors too. In the templates version of email this is very easy and takes maybe 5 lines of html code and the addition of one file using hooks/orders/totals.post.tpl or hooks/orders/invoice_shipping.post.tpl

Agreed, in my opinion the mailer should have also been created in Smarty as this is just a giant mess. The best thing would have been if they extended or re-used the block manager so people could have actually build their email templates (and keeping the same structure / workflow for the developers).

Yes I cleared the cache. And give a syntax error occurs (whern present) when I select "Print invoice" it seems that it's seeing it.

However, I question whether the actual document with the changes is being used. I even added literal values outside my conditions and they don't show.

1 table row with two cells.

Cell 1:

XX{% if coch.coch_amount and coch.coch_label %}{{coch.coch.label}}{% endif %}

Cell 2

YY{% if coch.coch_amount %}
{{coch.coch_amount}}
{%endif%}

Note that XX and YY never appear and any logging (or die()) I put in the anonymous function that returns the values is never executed, but syntax errors are seen and no syntax errors from Twig are shown.

Well, now I am out of ideas as well. Perhaps you can send mschekotov a message, he is the most responsive one (slack).

I posed the question on the doc page via Disqus but no response. In the past they'be been responsive to Disqus comments.

So my current "guess" (since there are no tools) is that the new "document" is NOT being picked up by the "Print invoice" menu item.

So I tried to just send the email. I got the following error:

Fatal error: Uncaught Error: Cannot use object of type Tygh\Template\Document\Order\Order as array in /home/ezms/public_html_subs/mve4/app/addons/coch/schemas/documents/order.post.php:16 Stack trace: #0 /home/ezms/public_html_subs/mve4/app/Tygh/Template/Document/Variables/GenericVariable.php(41): {closure}(Object(Tygh\Template\Document\Order\Context)) #1 [internal function]: Tygh\Template\Document\Variables\GenericVariable->__construct(Object(Tygh\Template\Document\Order\Context), Array) #2 /home/ezms/public_html_subs/mve4/app/Tygh/Template/ObjectFactory.php(84): ReflectionClass->newInstanceArgs(Array) #3 /home/ezms/public_html_subs/mve4/app/Tygh/Template/VariableProxy.php(86): Tygh\Template\ObjectFactory->create('\\Tygh\\Template\\...', Array, Array) #4 /home/ezms/public_html_subs/mve4/app/Tygh/Template/VariableProxy.php(102): Tygh\Template\VariableProxy->initVariable() #5 /home/ezms/public_html_subs/mve4/app/Tygh/Template/VariableProxy.php(186): Tygh\Template\VariableProxy->getAttribute('coch_amount') #6 /home/ezms/publ in /home/ezms/public_html_subs/mve4/app/addons/coch/schemas/documents/order.post.php on line 16

So i'm assuming that even though it accepted the variable in the editor and has its attributes that when run at runtime, it won't handle an array()!

Grr...

I posed the question on the doc page via Disqus but no response. In the past they'be been responsive to Disqus comments.

So my current "guess" (since there are no tools) is that the new "document" is NOT being picked up by the "Print invoice" menu item.

So I tried to just send the email. I got the following error:

Fatal error: Uncaught Error: Cannot use object of type Tygh\Template\Document\Order\Order as array in /home/ezms/public_html_subs/mve4/app/addons/coch/schemas/documents/order.post.php:16 Stack trace: #0 /home/ezms/public_html_subs/mve4/app/Tygh/Template/Document/Variables/GenericVariable.php(41): {closure}(Object(Tygh\Template\Document\Order\Context)) #1 [internal function]: Tygh\Template\Document\Variables\GenericVariable->__construct(Object(Tygh\Template\Document\Order\Context), Array) #2 /home/ezms/public_html_subs/mve4/app/Tygh/Template/ObjectFactory.php(84): ReflectionClass->newInstanceArgs(Array) #3 /home/ezms/public_html_subs/mve4/app/Tygh/Template/VariableProxy.php(86): Tygh\Template\ObjectFactory->create('\\Tygh\\Template\\...', Array, Array) #4 /home/ezms/public_html_subs/mve4/app/Tygh/Template/VariableProxy.php(102): Tygh\Template\VariableProxy->initVariable() #5 /home/ezms/public_html_subs/mve4/app/Tygh/Template/VariableProxy.php(186): Tygh\Template\VariableProxy->getAttribute('coch_amount') #6 /home/ezms/publ in /home/ezms/public_html_subs/mve4/app/addons/coch/schemas/documents/order.post.php on line 16

So i'm assuming that even though it accepted the variable in the editor and has its attributes that when run at runtime, it won't handle an array()!

Grr...

Try adding (array) in front of it. At this point you are not 'converting it', an object can not be approached as an array since you would access things like this $item->var and not like $item['var']

This all gets worse the further along I go....

It seems that the "Print invoice" menu function doesn't use the current document for order summary. So not sure how/where to change that. So I just sent the email and it generated an error of:

 Cannot use object of type Tygh\Template\Document\Order\Order as array in /home/ezms/public_html_subs/mve4/app/lib/vendor/twig/twig/lib/Twig/Environment.php(403) : eval()'d code on line 0

when doing a preview.

So I flattened the variables and made them two separate variables (not an array of 3 elements) and adjusted the document to use the new names and it still generates a similar error of:

XX
Fatal error: Method Tygh\Template\VariableProxy::__toString() must not throw an exception, caught Error: Cannot use object of type Tygh\Template\Document\Order\Order as array in /home/ezms/public_html_subs/mve4/var/cache/templates/twig/5f/5fd5ec2265abd9a9c77ce61af86ae72cf14bf89f9caa52a3925cd9829b96b7f5.phpon line 0

I even changed the order.post.php schema to return literal values and still get the same error. The order.post.php schema file now looks like:

$schema['charity_label'] = array(
    'class' => '\Tygh\Template\Document\Variables\GenericVariable',
    'data' => function (\Tygh\Template\Document\Order\Context $context) {
    $order = $context->getOrder();
    $vals = coch_get_order_data($order['order_id']);
    return "this is a test";
    return empty($vals['coch_label']) ? '' : $vals['coch_label'];
  },
  'attributes' => array()
);
$schema['charity_amount'] = array(
  'class' => '\Tygh\Template\Document\Variables\GenericVariable',
  'data' => function (\Tygh\Template\Document\Order\Context $context) {
    $order = $context->getOrder();
    $vals = coch_get_order_data($order['order_id']);
    return "0.01";
    $formatter = Tygh::$app['formatter'];
    return $formatter->asPrice(empty($vals['coch_amount']) ? 0.00 : $vals['coch_amount']);
  },
  'attributes' => array()
);

Okay, the GenericVariable class is buggy. I gave up and used the specific class to provide the variable and it all works as it should.

There's still the issue that the menu item for Print invoice does not use the current order document.... I'll report both bugs.

Still annoying that one has to implement a whole class to make a variable available and there is not way for an addon developer to incorporate the change to the document but instead have to rely on the merchant to do so....

I didn’t realize it, but order notifications and invoices use separate documents. It’s there a place to set what documents are used for what actions?

I didn't realize it, but order notifications and invoices use separate documents. It's there a place to set what documents are used for what actions?

I think so, I recall being able to define them in the addon.xml file.