Follow us on Twitter X-Cart on Facebook Wiki
Shopping cart software Solutions for online shops and malls

Custom e-mail variables

 
Reply
   X-Cart forums > X-Cart 5 > Dev Questions (X-Cart 5)
 
Thread Tools Search this Thread
  #1  
Old 10-09-2020, 04:40 PM
  The Knotty Celt's Avatar 
The Knotty Celt The Knotty Celt is offline
 

Advanced Member
  
Join Date: Jan 2020
Posts: 32
 

Default Custom e-mail variables

PREMISE
I have created a custom module which adds an order reference field to the Order class. The module displays this rather than the default order number. This was done to allow for customized format and annual counter reset. While I am able to correctly display this reference number to the customer and admin, emails are still referencing the default order number.


WHAT I'VE TRIED
I believe the email variables are defined in the XLite\Core\Mail\Order\AOrder class, specifically the __construct fucnction.


Code:
<?php // vim: set ts=4 sw=4 sts=4 et: namespace XLite\Module\LBS\OrderReference\Core\Mail\Order use XLite\Core\Cache\ExecuteCachedTrait; use XLite\Core\Config; use XLite\Module\LBS\OrderReference\Model\Order; use XLite\View\AView; abstract class AOrder extends \XLite\Core\Mail\Order\AOrder implements \XLite\Base\IDecorator { public function __construct(Order $order) { parent::__construct(); $this->appendData([ 'order' => $order; 'recipient_name' => $order->getProfile()->getName(), ]); $this->populateVariables([ 'order_number' => $order->getReference(), 'order_total' => AView::formatPrice($order->getTotal()), 'recipient_name' => $order->getProfile()->getName(), 'first_name' => $order->getProfile()->getName(true, true), 'shipping_method_name' => $order->getPaymentMethodName(), ]); $this->getAttachPdfInvoice(Config::getInstance()->NotificationAttachments->attach_pdf_invoice); if ($order->getPaymentStatus()) { $order->getPaymentStatus()->explicitlyLoadTranslations(); } if ($order->getShippingStatus() { $order->getShippingStatus()->explicitlyLoadTranslations(); } } }


I replaced the getOrderNumber() function call wtih a call to the getReference() function of my decorated Order class.


RESULTING BEHAVIOUR
Even after rebuilding cache, and attempting to send an order update e-mail, the subject line still uses the build-in order number rather than the customized reference number.


What am I missing? Did I not decorate the correct class? Did I not decorate it properly? How can I achieve my desired result?


Thank you in advance for any assistance you are able to provide.
__________________
X-Cart version 5.4.1.46
PHP version 7.4.33
MySQL version 15.1
Apache version 2.4.56
cURL version 7.74.0
Reply With Quote
  #2  
Old 11-04-2020, 09:24 AM
  The Knotty Celt's Avatar 
The Knotty Celt The Knotty Celt is offline
 

Advanced Member
  
Join Date: Jan 2020
Posts: 32
 

Default Re: Custom e-mail variables

I managed to solve this particular issue. I also opted to add a completely custom field, so that I could still access all the default ones. The issue stemmed from the following call:
Code:
parent::__construct();
In this case, the parent class is actually referencing the original AOrder class which this one is extending. Since the constructor of the original AOrder class expects an argument defining the order, it was a simple matter of passing this argument to the parent constructor.
Code:
parent::__construct($order);
With my new variable added, the __construct method of my decorated class becomes:
Code:
public function __construct(Order $order) { parent::__construct($order); $this->appendData([ 'order' => $order, 'recipient_name' => $order->getProfile()->getName(), ]); $this->populateVariables([ 'order_number' => $order->getOrderNumber(), 'order_total' => AView::formatPrice($order->getTotal()), 'recipient_name' => $order->getProfile()->getName(), 'first_name' => $order->getProfile()->getName(true, true), 'shipping_method_name' => $order->getShippingMethodName(), 'payment_method_name' => $order->getPaymentMethodName(), 'order_reference' => $order->getReference(), ]); $this->setAttachPdfInvoice(Config::getInstance()->NotificationAttachments->attac$ if ($order->getPaymentStatus()) { $order->getPaymentStatus()->explicitlyLoadTranslations(); } if ($order->getShippingStatus()) { $order->getShippingStatus()->explicitlyLoadTranslations(); } }

It is also important to note that I also added the following method to my decorated class in order to define and display the custom variable in the pop-up preview.
Code:
protected static function defineVariables() { $prefix = \XLite\Core\Config::getInstance()->LBS->OrderReference->prefix; $separator = \XLite\Core\Config::getInstance()->LBS->OrderReference->separator; return [ 'order_number' => '42', 'order_total' => AView::formatPrice(42), 'recipient_name' => static::t('John Doe'), 'first_name' => static::t('John'), 'shipping_method_name' => static::t('Shipping method'), 'payment_method_name' => static::t('Payment method'), 'order_reference' => $prefix.$separator.date('Y').$separator.'0042', ] + parent::defineVariables(); }
__________________
X-Cart version 5.4.1.46
PHP version 7.4.33
MySQL version 15.1
Apache version 2.4.56
cURL version 7.74.0

Last edited by The Knotty Celt : 11-04-2020 at 09:31 AM. Reason: Missed Step. Clarified abiguity.
Reply With Quote

The following user thanks The Knotty Celt for this useful post:
Ed B. (12-16-2020)
  #3  
Old 02-13-2021, 11:08 AM
  The Knotty Celt's Avatar 
The Knotty Celt The Knotty Celt is offline
 

Advanced Member
  
Join Date: Jan 2020
Posts: 32
 

Default Re: Custom e-mail variables

While the changes I described work for all the standard order notification e-mails, it does not do the same for notifications created by the Order Messages module or other Modules which access the Order class.
__________________
X-Cart version 5.4.1.46
PHP version 7.4.33
MySQL version 15.1
Apache version 2.4.56
cURL version 7.74.0
Reply With Quote
Reply
   X-Cart forums > X-Cart 5 > Dev Questions (X-Cart 5)


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -8. The time now is 03:29 AM.

   

 
X-Cart forums © 2001-2020