cancel
Showing results for 
Search instead for 
Did you mean: 

first attempt at a custom module doesn't seem to be having any effect

first attempt at a custom module doesn't seem to be having any effect

I've created my 1st module. The purpose is to add the customer group name to the order email template. I think that I didn't do something quite right and was hoping someone could look at my files to see where I may have messed up. The files are stored in magento/app/code/KeystoneCandle/AddGroupEmail/, /etc and /Observer

registration.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'KeytoneCandle_AddGroupEmail',
__DIR__
);

module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="KeystoneCandle_AddGroupEmail" setup_version="1.0.0">
<sequence>
<module name="Magento_Catalog"/>
</sequence>
</module>
</config>

Observer file AddGroupEmail.php

<?php

namespace KeystoneCandle\AddGroupEmail\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Customer\Model\Group;

class AddCustomerGroupToOrderEmail implements ObserverInterface
{
protected $_order;
protected $_group;

public function __construct(
\Magento\Sales\Model\Order $order,
Group $group
) {
$this->_order = $order;
$this->_group = $group;
}

public function execute(Observer $observer)
{
$order = $observer->getEvent()->getOrder();
$customerGroupId = $order->getCustomerGroupId();
$customerGroup = $this->_group->load($customerGroupId);
$customerGroupName = $customerGroup->getCustomerGroupCode();

// You can do additional processing with the customer group if needed.

$order->setData('customer_group_id', $customerGroupId);
$order->setData('customer_group_name', $customerGroupName);
}
}

Then in the email template I try to use these variables to show the customer group name and ID

{{var order.customer_group_name}} {{var order.customer_group_id}}

I've run setup:upgrade, setup:di:compile, setup:static-content:deploy -f, cache:flush/clean

The strange thing is that the

{{var order.customer_group_id}}

variable pulls the customer group id even after I disable my module (and run all the previous commands) so I'm not sure if that variable needed the observer or not but I can't get any reaction from the customergroupname. I'd appreciate any insights. Thanks.

 

1 REPLY 1

Re: first attempt at a custom module doesn't seem to be having any effect

@keystoneke3419 

 

Hey,

 

events.xml file is missing at etc folder code is like below in that file,

<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"> <event name="email_order_set_template_vars_before"> <observer name="order-add-customer-group" instance="KeystoneCandle\KeystoneCandle\Observer\AddCustomerGroupToOrderEmail"/> </event> </config>

Thank You!

Problem solved? Click Kudos and "Accept as Solution".
200+ Magento 2 Extensions for Enhanced Shopping Experience.