How to Add Newsletter Subscription Column to Customer Grid in Magento 2
Looking for a method to add newsletter subscription column to customer grid in Magento 2? Find the complete solution here.
An email newsletter is an important channel of communication. From promoting new products and sending exclusive coupon codes to greeting customers on their anniversaries, newsletters are an excellent method to nourish brand-customer relations.
Customers subscribed to the newsletter are generally a step closer to being your brand advocates — or say they’re more loyal customers. Keeping track of your newsletter subscribers can help you evaluate your branding effort and performance. Magento 2 offers a different backend grid to manage newsletter subscriptions by default. If you want a bigger picture, this separate backend grid may not be enough for you.
I recently worked on one such requirement for a client who wanted to display the newsletter subscription status in the customer grid. In this blog post, I will share the step-wise method I used to add a newsletter subscription column to the customer grid in Magento 2.
Method to Add Newsletter Subscription Column to Customer Grid in Magento 2
In order to add the Newsletter Subscription column to the customer grid in Magento 2, we need to override the default customer grid. Here are the steps I followed to do that:
Steps to do that:
Step 1: Register a new module by creating a registration.php file in app/code/Meetanshi/ directory with the following code:
1 2 3 4 5 6 |
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Meetanshi_CustomerSubscriptionStatus', __DIR__ ); |
Step 2: Now, create a module.xml file in the app/code/Meetanshi/CustomerSubscriptionStatus/etc/ directory with the following code:
1 2 3 4 |
<?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="Meetanshi_CustomerSubscriptionStatus" setup_version="1.0.0"/> </config> |
Step 3: In the same directory, create a di.xml file to define the plugin with the following code:
1 2 3 4 5 6 7 8 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory"> <plugin name="mt_subscriber_status" type="Meetanshi\CustomerSubscriptionStatus\Model\Customer\Grid\CollectionPlugin" sortOrder="5"/> </type> </config> |
Step 4: Now, create a plugin file named CollectionPlugin.php, as we defined in the earlier file, at the app/code/Meetanshi/CustomerSubscriptionStatus/Model/Customer/Grid/ directory with the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?php namespace Meetanshi\CustomerSubscriptionStatus\Model\Customer\Grid; use Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory; class CollectionPlugin { public function afterGetReport( CollectionFactory $subject, $collection, $requestName ){ if ($requestName == 'customer_listing_data_source') { $select = $collection->getSelect(); $select->joinLeft( ["secondTable" => $collection->getTable("newsletter_subscriber")], 'main_table.entity_id = secondTable.customer_id', ['subscriber_status'] ); $collection->addFilterToMap('main_table.subscriber_status', 'secondTable.subscriber_status'); } return $collection; } } |
Step 5: Create a customer_listing.xml file at the app/code/Meetanshi/CustomerSubscriptionStatus/view/adminhtml/ui_component/ directory with the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?xml version="1.0" encoding="UTF-8"?> <listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd"> <listingToolbar name="listing_top"/> <columns name="customer_columns" class="Magento\Customer\Ui\Component\Listing\Columns"> <column name="subscriber_status" sortOrder="30" class="Meetanshi\CustomerSubscriptionStatus\Ui\Component\Listing\Columns\Statuses"> <settings> <dataType>select</dataType> <options class="Meetanshi\CustomerSubscriptionStatus\Model\Subscription\Statuses"/> <filter>select</filter> <label translate="true">Subscriber Status</label> </settings> </column> </columns> </listing> |
Step 6: Create Statuses.php file at the app/code/Meetanshi/CustomerSubscriptionStatus/Model/Subscription/ directory with the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?php namespace Meetanshi\CustomerSubscriptionStatus\Model\Subscription; use Magento\Framework\Option\ArrayInterface; class Statuses implements ArrayInterface { /** * Retrieve options array. * * @return array */ public function toOptionArray() { $result = [ ['value' => 1, 'label' => __("Subscribed")], ['value' => 2, 'label' => __("Not Activate")], ['value' => 3, 'label' => __("Not Subscribed")], ['value' => 4, 'label' => __("Not Confirmed")], ]; return $result; } } |
This will convert the newsletter subscription statuses into labels, which will be displayed in the customer grid.
Step 7: Lastly, create Statuses.php file in the CustomerSubscriptionStatus\CustomerSubscriptionStatus\Ui\Component\Listing\Columns directory with the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<?php namespace Meetanshi\CustomerSubscriptionStatus\Ui\Component\Listing\Columns; use Magento\Framework\View\Element\UiComponent\ContextInterface; use Magento\Framework\View\Element\UiComponentFactory; use Magento\Ui\Component\Listing\Columns\Column; class Statuses extends Column { public function construct( ContextInterface $context, UiComponentFactory $uiComponentFactory, array $components = [], array $data = [] ) { parent::construct($context, $uiComponentFactory, $components, $data); } public function prepareDataSource(array $dataSource) { if (isset($dataSource['data']['items'])) { foreach ($dataSource['data']['items'] as & $item) { $status = $item['subscriber_status']; if ($status == 1) $name = __('Subscribed'); elseif ($status == 3) $name = __('Not Subscribed'); else $name = __(''); $item[$this->getData('name')] = $name; } } return $dataSource; } } |
& that’s it!
Related Read: How to Enable Ajax Newsletter in Magento 2
It will add newsletter subscription column to customer grid in Magento 2. If you get an error of unsubscribe link for newsletter in Magento 2 not working solve it as soon as possible to stop your customers from getting frustrated.
Did you find this Magento solution helpful? Let me know in the comments below.
Share this post with your friends and spread the Magento knowledge. 😃
Thanks for reading…! 🍀
Siddharth Nandava
Siddharth Nandava is an enthusiastic Jr Magento developer at Meetanshi. Apart from the work, you can find him learning new things and spending quality time with his family.
Prev
Monthly Recap: Meetanshi Launches and Updates – February 2023
90+ Digital Marketing Statistics to Fuel Your Strategy [2024]
Next