Displaying Customer Group Information in Magento
As a Magento developer, I often receive queries from clients about displaying customer group information within the admin panel or customer account area. In this post, we’ll explore how to show which group a specific customer belongs to.
Understanding Customer Groups in Magento 2
Magento provides built-in functionality for managing customer groups, which allows you to segment customers and apply different pricing rules, tax classes, and other attributes based on their membership. By default, the system includes two groups: General and Not Logged In.
To create a new group, navigate to Stores > Attributes > Customer Groups in your Magento admin panel. Here, you can add new groups like “Purchase Order Enabled” or any other custom group that suits your business requirements.
Displaying Group Information for a Specific Customer
To display the customer’s group information within their account area, we’ll need to create a custom block and template. Here are the steps:
-
Create a Custom Block: In your Magento theme directory (e.g.,
app/design/frontend/Vendor/theme/Magento_Customer/templates), create a new XML file namedcustomer_group_info.phtml. -
Add Customer Group Information: Open the newly created
customer_group_info.phtmlfile and add the following PHP code to fetch the customer’s group information:
<?php if ($block->getGroupId()): ?>
<div class="box box-information">
<h4 class="heading"><?= __('Customer Group') ?></h4>
<p><?= $block->escapeHtml(__('Your current customer group: %1', $block->getGroupName())) ?></p>
</div>
<?php endif; ?>
-
Create a Custom Block File: In your theme’s
etcdirectory, create a new XML file nameddi.xml. -
Configure the Custom Block: Open the
di.xmlfile and add the following code to configure the custom block:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Customer\Block\Account\Dashboard">
<arguments>
<argument name="blocks" xsi:type="array">
<item name="customer_group_info_block" xsi:type="string">Vendor_MagentoCustomerGroup::groupInfo</item>
</argument>
</arguments>
</type>
</config>
Replace Vendor with your actual vendor name.
-
Create the Custom Group Information Block: In your theme’s
app/code/Vendor/MagentoCustomerGroup/Blockdirectory, create a new file namedGroupInfo.php. -
Implement the Block Logic: Open the
GroupInfo.phpfile and add the following code to implement the custom block:
namespace Vendor\MagentoCustomerGroup\Block;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Magento\Customer\Model\Session;
use Magento\Customer\Api\Data\GroupInterfaceFactory;
class GroupInfo extends Template
{
protected $_session;
protected $_groupFactory;
public function __construct(
Context $context,
Session $customerSession,
GroupInterfaceFactory $groupFactory
) {
parent::__construct($context);
$this->_session = $customerSession;
$this->_groupFactory = $groupFactory;
}
public function getGroupId()
{
if ($this->_session->isLoggedIn()) {
return $this->_session->getCustomer()->getGroupId();
}
return null;
}
public function getGroupName()
{
if ($this->getGroupId()) {
$group = $this->_groupFactory->create()->load($this->getGroupId());
return $group->getName();
}
return null;
}
}
Conclusion
Following these steps, you should now be able to display the customer’s group information within their account area in Magento 2. This customization allows you to provide additional context to your customers about their membership status.
Remember that Magento development often involves creating custom modules and extending existing functionality. For more advanced customizations and best practices, consider exploring the official Magento documentation or engaging with experienced professionals at Laravel Company.
This approach ensures a clean separation of concerns while keeping your codebase maintainable and performance-optimized.