{"id":2702,"date":"2024-12-31T20:26:01","date_gmt":"2024-12-31T20:26:01","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/how-to-add-newsletter-subscription-column-to-customer-grid-in-magento-2\/"},"modified":"2025-03-17T06:19:54","modified_gmt":"2025-03-17T06:19:54","slug":"add-newsletter-subscription-column-to-customer-grid-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/add-newsletter-subscription-column-to-customer-grid-in-magento-2\/","title":{"rendered":"How to Add Newsletter Subscription Column to Customer Grid in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Looking for a method to&nbsp;<em><strong>add newsletter subscription column to customer grid in Magento 2<\/strong><\/em>? Find the complete solution here.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Customers subscribed to the newsletter are generally a step closer to being your brand advocates \u2014 or say they\u2019re more loyal customers. Keeping track of your newsletter subscribers can help you evaluate your branding effort and performance.&nbsp;<a href=\"https:\/\/business.adobe.com\/products\/magento\/magento-commerce.html\" target=\"_blank\" rel=\"noreferrer noopener\">Magento 2<\/a>&nbsp;offers a different backend grid to&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/manage-newsletter-subscriptions-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">manage newsletter subscriptions<\/a>&nbsp;by default. If you want a bigger picture, this separate backend grid may not be enough for you.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Add Newsletter Subscription Column to Customer Grid in Magento 2<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In order to add the&nbsp;<strong>Newsletter Subscription&nbsp;<\/strong>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:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em><strong>Steps to do that:<\/strong><\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 1:&nbsp;<\/strong>Register a new module by creating a&nbsp;<strong>registration.php<\/strong>&nbsp;file in&nbsp;<em><strong>app\/code\/Meetanshi\/&nbsp;<\/strong><\/em>directory with the following code:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n    \\Magento\\Framework\\Component\\ComponentRegistrar::register(\n        \\Magento\\Framework\\Component\\ComponentRegistrar::MODULE,\n        'Meetanshi_CustomerSubscriptionStatus',\n        __DIR__\n    );<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 2:&nbsp;<\/strong>Now, create a&nbsp;<em><strong>module.xml<\/strong><\/em>&nbsp;file in the&nbsp;<em><strong>app\/code\/Meetanshi\/CustomerSubscriptionStatus\/etc\/&nbsp;<\/strong><\/em>directory with the following code:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Module\/etc\/module.xsd\">\n    &lt;module name=\"Meetanshi_CustomerSubscriptionStatus\" setup_version=\"1.0.0\"\/>\n&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 3:&nbsp;<\/strong>In the same directory, create a&nbsp;<em><strong>di.xml&nbsp;<\/strong><\/em>file to define the plugin with the following code:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n        xsi:noNamespaceSchemaLocation=\"urn:magento:framework:ObjectManager\/etc\/config.xsd\">\n    &lt;type name=\"Magento\\Framework\\View\\Element\\UiComponent\\DataProvider\\CollectionFactory\">\n        &lt;plugin name=\"mt_subscriber_status\"\n                type=\"Meetanshi\\CustomerSubscriptionStatus\\Model\\Customer\\Grid\\CollectionPlugin\" sortOrder=\"5\"\/>\n    &lt;\/type>\n&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 4:&nbsp;<\/strong>Now, create a plugin file named&nbsp;<em><strong>CollectionPlugin.php,<\/strong><\/em>&nbsp;as we defined in the earlier file, at the&nbsp;<strong>app\/code\/Meetanshi\/CustomerSubscriptionStatus\/Model\/Customer\/Grid\/&nbsp;<\/strong>directory with the following code:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\nnamespace Meetanshi\\CustomerSubscriptionStatus\\Model\\Customer\\Grid;\nuse Magento\\Framework\\View\\Element\\UiComponent\\DataProvider\\CollectionFactory;\nclass CollectionPlugin\n{\n    public function afterGetReport(\n        CollectionFactory $subject,\n        $collection,\n        $requestName\n    ){\n\n        if ($requestName == 'customer_listing_data_source') {\n            $select = $collection->getSelect();\n            $select->joinLeft(\n                [\"secondTable\" => $collection->getTable(\"newsletter_subscriber\")],\n                'main_table.entity_id = secondTable.customer_id',\n                ['subscriber_status']\n            );\n            $collection->addFilterToMap('main_table.subscriber_status', 'secondTable.subscriber_status');\n        }\n        return $collection;\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 5:&nbsp;<\/strong>Create a&nbsp;<em><strong>customer_listing.xml&nbsp;<\/strong><\/em>file at the&nbsp;<strong>app\/code\/Meetanshi\/CustomerSubscriptionStatus\/view\/adminhtml\/ui_component\/<\/strong>&nbsp;directory with the following code:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?>\n&lt;listing xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:module:Magento_Ui:etc\/ui_configuration.xsd\">\n    &lt;listingToolbar name=\"listing_top\"\/>\n    &lt;columns name=\"customer_columns\" class=\"Magento\\Customer\\Ui\\Component\\Listing\\Columns\">\n        &lt;column name=\"subscriber_status\" sortOrder=\"30\"\n            class=\"Meetanshi\\CustomerSubscriptionStatus\\Ui\\Component\\Listing\\Columns\\Statuses\">\n            &lt;settings>\n                &lt;dataType>select&lt;\/dataType>\n                &lt;options class=\"Meetanshi\\CustomerSubscriptionStatus\\Model\\Subscription\\Statuses\"\/>\n                &lt;filter>select&lt;\/filter>\n                &lt;label translate=\"true\">Subscriber Status&lt;\/label>\n            &lt;\/settings>\n        &lt;\/column>\n    &lt;\/columns>\n&lt;\/listing><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 6:&nbsp;<\/strong>Create&nbsp;<em><strong>Statuses.php&nbsp;<\/strong><\/em>file at the&nbsp;<strong>app\/code\/Meetanshi\/CustomerSubscriptionStatus\/Model\/Subscription\/&nbsp;<\/strong>directory with the following code:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nnamespace Meetanshi\\CustomerSubscriptionStatus\\Model\\Subscription;\n\nuse Magento\\Framework\\Option\\ArrayInterface;\n\nclass Statuses implements ArrayInterface\n{\n    \/**\n     * Retrieve options array.\n     *\n     * @return array\n     *\/\n    public function toOptionArray()\n    {\n        $result = [\n            ['value' => 1, 'label' => __(\"Subscribed\")],\n            ['value' => 2, 'label' => __(\"Not Activate\")],\n            ['value' => 3, 'label' => __(\"Not Subscribed\")],\n            ['value' => 4, 'label' => __(\"Not Confirmed\")],\n        ];\n\n        return $result;\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This will convert the newsletter subscription statuses into labels, which will be displayed in the customer grid.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 7:&nbsp;<\/strong>Lastly, create&nbsp;<em><strong>Statuses.php&nbsp;<\/strong><\/em>file in the&nbsp;<strong>CustomerSubscriptionStatus\\CustomerSubscriptionStatus\\Ui\\Component\\Listing\\Columns<\/strong>&nbsp;directory with the following code:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nnamespace Meetanshi\\CustomerSubscriptionStatus\\Ui\\Component\\Listing\\Columns;\nuse Magento\\Framework\\View\\Element\\UiComponent\\ContextInterface;\nuse Magento\\Framework\\View\\Element\\UiComponentFactory;\nuse Magento\\Ui\\Component\\Listing\\Columns\\Column;\n\nclass Statuses extends Column\n{\n    public function construct(\n        ContextInterface $context, UiComponentFactory $uiComponentFactory, array $components = [], array $data = []\n    ) {\n        parent::construct($context, $uiComponentFactory, $components, $data);\n    }\n\n    public function prepareDataSource(array $dataSource)\n    {\n        if (isset($dataSource['data']['items'])) {\n            foreach ($dataSource['data']['items'] as &amp; $item) {\n                $status = $item['subscriber_status'];\n                if ($status == 1)\n                    $name = __('Subscribed');\n                elseif ($status == 3)\n                    $name = __('Not Subscribed');\n                else\n                    $name = __('');\n                $item[$this->getData('name')] = $name;\n            }\n        }\n\n        return $dataSource;\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">&amp; that\u2019s it!<br><strong>Related Read:<\/strong>&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/enable-ajax-newsletter-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Enable Ajax Newsletter in Magento 2<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It will add newsletter subscription column to customer grid in Magento 2. If you get an error of&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/magento-newsletter-unsubscribe-link-not-working\/\">unsubscribe link for newsletter in Magento 2 not working<\/a>&nbsp;solve it as soon as possible to stop your customers from getting frustrated.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Did you find this Magento solution helpful? Let me know in the comments below.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Share this post with your friends and spread the Magento knowledge.  <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thanks for reading\u2026!  <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Looking for a method to&nbsp;add newsletter subscription column to customer grid in Magento 2? Find the complete solution here. An email newsletter is an important&#8230;<\/p>\n","protected":false},"author":38,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-2702","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/2702","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/38"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=2702"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/2702\/revisions"}],"predecessor-version":[{"id":9370,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/2702\/revisions\/9370"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=2702"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=2702"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=2702"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}