Jump to content

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

Adding New Variables To Variable List In Document Rate Topic   - - - - -

 
  • Jaari
  • Newbie
  • Trial users
  • Join Date: 15-Mar 18
  • 3 posts

Posted 27 March 2018 - 03:19 PM #1

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?



 

Posted 28 March 2018 - 07:22 AM #2

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


Sincerely yours, CS-Cart Support Team

 

User guide       |  Developer documentation  |  Core API documentation


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

Posted 24 April 2018 - 06:19 PM #3

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;

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.


 
  • poppedweb
  • Authorized Reseller
  • Members
  • Join Date: 02-Aug 16
  • 415 posts

Posted 24 April 2018 - 08:23 PM #4

 

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.


PoppedWeb | sales@poppedweb.com | https://poppedweb.com
TurnKey Website Design | Add-Ons | Performance Audits | Dedicated Server Management
24/7 Support | Response within an hour (during working hours).

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

Posted 25 April 2018 - 12:21 AM #5

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.


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.


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

Posted 25 April 2018 - 01:19 AM #6

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;

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.


 
  • poppedweb
  • Authorized Reseller
  • Members
  • Join Date: 02-Aug 16
  • 415 posts

Posted 25 April 2018 - 04:50 AM #7

 

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.


PoppedWeb | sales@poppedweb.com | https://poppedweb.com
TurnKey Website Design | Add-Ons | Performance Audits | Dedicated Server Management
24/7 Support | Response within an hour (during working hours).

 
  • poppedweb
  • Authorized Reseller
  • Members
  • Join Date: 02-Aug 16
  • 415 posts

Posted 25 April 2018 - 06:33 AM #8

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.


PoppedWeb | sales@poppedweb.com | https://poppedweb.com
TurnKey Website Design | Add-Ons | Performance Audits | Dedicated Server Management
24/7 Support | Response within an hour (during working hours).

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

Posted 25 April 2018 - 07:25 AM #9

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 


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 руб.


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

Posted 25 April 2018 - 05:06 PM #10

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.


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.


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

Posted 25 April 2018 - 05:10 PM #11

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.


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.


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

Posted 25 April 2018 - 05:16 PM #12

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


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.


 
  • poppedweb
  • Authorized Reseller
  • Members
  • Join Date: 02-Aug 16
  • 415 posts

Posted 25 April 2018 - 05:21 PM #13

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


PoppedWeb | sales@poppedweb.com | https://poppedweb.com
TurnKey Website Design | Add-Ons | Performance Audits | Dedicated Server Management
24/7 Support | Response within an hour (during working hours).

 
  • poppedweb
  • Authorized Reseller
  • Members
  • Join Date: 02-Aug 16
  • 415 posts

Posted 25 April 2018 - 05:22 PM #14

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


PoppedWeb | sales@poppedweb.com | https://poppedweb.com
TurnKey Website Design | Add-Ons | Performance Audits | Dedicated Server Management
24/7 Support | Response within an hour (during working hours).

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

Posted 25 April 2018 - 05:41 PM #15

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...


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.


 
  • poppedweb
  • Authorized Reseller
  • Members
  • Join Date: 02-Aug 16
  • 415 posts

Posted 25 April 2018 - 06:02 PM #16

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']


PoppedWeb | sales@poppedweb.com | https://poppedweb.com
TurnKey Website Design | Add-Ons | Performance Audits | Dedicated Server Management
24/7 Support | Response within an hour (during working hours).

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

Posted 25 April 2018 - 06:27 PM #17

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

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.


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

Posted 25 April 2018 - 08:53 PM #18

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....


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.


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

Posted 26 April 2018 - 05:00 PM #19

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?

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.


 
  • poppedweb
  • Authorized Reseller
  • Members
  • Join Date: 02-Aug 16
  • 415 posts

Posted 26 April 2018 - 05:07 PM #20

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.


PoppedWeb | sales@poppedweb.com | https://poppedweb.com
TurnKey Website Design | Add-Ons | Performance Audits | Dedicated Server Management
24/7 Support | Response within an hour (during working hours).