X-Cart: shopping cart software

X-Cart forums (https://forum.x-cart.com/index.php)
-   Dev Questions (X-Cart 5) (https://forum.x-cart.com/forumdisplay.php?f=56)
-   -   Custom e-mail variables (https://forum.x-cart.com/showthread.php?t=78021)

The Knotty Celt 10-09-2020 04:40 PM

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.

The Knotty Celt 11-04-2020 09:24 AM

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


The Knotty Celt 02-13-2021 11:08 AM

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.


All times are GMT -8. The time now is 09:24 AM.

Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.