cancel
Showing results for 
Search instead for 
Did you mean: 

Add Customer Group to Order Email Template

Add Customer Group to Order Email Template

I was shown how to create an observer to grab the customer groupID and create a variable that can be added to my email template like this... 

<?php

namespace Vendor\Module\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class AddCustomerGroupToOrderEmail implements ObserverInterface
{
    protected $_order;

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

    public function execute(Observer $observer)
    {
        $order = $observer->getEvent()->getOrder();
        $customerGroupId = $order->getCustomerGroupId();

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

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

And in my email template, I can then include {{var order.customer_group_id}} to display the customer group ID.

 

I would like to know I can make the Variable show the customer group NAME instead of the ID number so my employees don't all need to remember what every number means.  So for example, if customerGroupId = 0 then it would say Not Logged In, if customerGroupId = 1, then it would say General, etc. 

I don't know php very well and have just started using Magento so if anyone can help, I'd really appreciate it.

Thanks.

5 REPLIES 5

Re: Add Customer Group to Order Email Template

You can modify your observer to load the customer group name. Update your observer code and add `Magento\Customer\Model\Group` class. Here's a simple version of the updated code:

 

namespace Vendor\Module\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();

// Set the customer group name to the order data
$order->setData('customer_group_name', $customerGroupName);
}
}

 

Now, in your email template, you can use `{{var order.customer_group_name}}` to display the customer group name instead of the ID. This way, your employees will see the actual names like Not Logged In or General. Hope this helps Smiley Happy

Re: Add Customer Group to Order Email Template

@keystoneke3419 

 

Hey,

 

You can add the condition in the email template and display the group name use condition like below in the email template,

 {{if order.customer_group_id == 1}} <p>Customer Group 1 Name</p> {{/if}}

Thank You!

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

Re: Add Customer Group to Order Email Template

For some reason it still isn't working right. This is what my Observer file looks like now...

<?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);
    }
}
And this is what I have tried in the email template
 
            <p>
               Group Name <span style="font-weight: 400;">{{var order.customer_group_name}} {{var order.customer_group_id}} {{if order.customer_group_id == 1}} General{{/if}}
{{if order.customer_group_id == 2}} Wholesale{{/if}}
</span>
            </p>
Which is a combination of your 2 answers to see which one works but the output is "Group Name 2" (I'm logged in as a wholesale account).  So the actual Group Name variable isn't set right somewhere.  Can you see what is wrong?
Thanks.
 

Re: Add Customer Group to Order Email Template

Can anyone see what is wrong with my code and why the Group Name isn't showing up on my emails?  The Group ID shows up fine, I just can't figure out how to convert it to the Group Name.

 

Thanks.

Re: Add Customer Group to Order Email Template

I still haven't gotten the group name to display in my order email templates correctly yet.  The CustomerGroupID shows up fine though.  Here is the code in my observer file.

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

// Set the customer group name to the order data
$order->setData('customer_group_name', $customerGroupName);
}
}

and code in my email template is this

Group Name {{var order.customer_group_name}} {{var order.customer_group_id}} 

my order emails still come through like this though with just ID number, no name.

 

Screenshot 2024-06-12 143613.png

It almost seems like my observer file isn't updating when I make an edit.  I've run setup:upgrade, setup:di:compile: setup:static-content:deploy -f, cache:clean and flush

Can someone tell me what I'm missing?

Thanks.