{"id":135,"date":"2018-07-19T12:30:58","date_gmt":"2018-07-19T12:30:58","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2018\/07\/19\/create-product-grid-serialization-in-magento-2\/"},"modified":"2025-05-22T17:26:10","modified_gmt":"2025-05-22T11:56:10","slug":"create-product-grid-serialization-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/create-product-grid-serialization-in-magento-2\/","title":{"rendered":"How to Create Product Grid Serialization in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Working the smart way is crucial in business and in Magento 2, you require to be smarter to perform actions quickly!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Whenever you create a custom template and want to apply it to a number of products, you need not do it one by one. I have given the code here to implement&nbsp;<em><strong>product grid serialization in Magento 2.&nbsp;<\/strong><\/em>It enables a product grid from where you can select specific products at a time to apply a template or an option.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Steps to Create How to apply Product Grid Serialization in Magento 2<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">1. Create Tabs.php file<\/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=\"\">protected function _prepareLayout()\n{\n    $this->addTab(\n        'productgrid',\n        [\n            'label' => __('Select Product'),\n            'url' => $this->getUrl('*\/*\/actionname', ['_current' => true]),\n            'class' => 'ajax',\n        ]);\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2. Create your controller xml file.<\/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;layout xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n        xsi:noNamespaceSchemaLocation=\"urn:magento:framework:View\/Layout\/etc\/layout_generic.xsd\">\n    &lt;container name=\"root\" label=\"Root\">\n        &lt;block class=\"Vendor\\Extension\\Block\\Adminhtml\\Option\\Edit\\Tab\\Productgrid\"\n               name=\"company.module.edit.tab.productgrid\"\/>\n        &lt;block class=\"Magento\\Backend\\Block\\Widget\\Grid\\Serializer\" name=\"productgrid_grid_serializer\">\n            &lt;arguments>\n                &lt;argument name=\"input_names\" xsi:type=\"string\">products&lt;\/argument>\n                &lt;argument name=\"grid_block\" xsi:type=\"string\">company.module.edit.tab.productgrid&lt;\/argument>\n                &lt;argument name=\"callback\" xsi:type=\"string\">getSelectedProducts&lt;\/argument>\n                &lt;argument name=\"input_element_name\" xsi:type=\"string\">products&lt;\/argument>\n                &lt;argument name=\"reload_param_name\" xsi:type=\"string\">products&lt;\/argument>\n            &lt;\/arguments>\n        &lt;\/block>\n    &lt;\/container>\n&lt;\/layout><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">3. Controller php action<\/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=\"\">use Magento\\Backend\\App\\Action\\Context;\nuse Magento\\Catalog\\Controller\\Adminhtml\\Product\\Builder;\nuse Magento\\Framework\\View\\Result\\LayoutFactory;\nuse Magento\\Catalog\\Controller\\Adminhtml\\Product;\n\nclass Productgridextends Product\n{\n    protected $resultLayoutFactory;\n    public function __construct(\n        Context $context,\n        Builder $productBuilder,\n        LayoutFactory $resultLayoutFactory\n    )\n    {\n        parent::__construct($context, $productBuilder);\n        $this->resultLayoutFactory = $resultLayoutFactory;\n    }\n    public function execute()\n    {\n        $this->productBuilder->build($this->getRequest());\n        $resultLayout = $this->resultLayoutFactory->create();\n        $resultLayout->getLayout()->getBlock('company.module.edit.tab.productgrid')\n            ->setProducts($this->getRequest()->getPost('products', null));\n        return $resultLayout;\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">4. Create Productgrid.php<\/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\nuse Magento\\Backend\\Block\\Template\\Context;\nuse Magento\\Backend\\Helper\\Data;\nuse Magento\\Catalog\\Model\\ProductFactory;\nuse Vendor\\Extension\\Model\\OptionFactory;\nuse Magento\\Framework\\Registry;\n\nclass Productgridextends\\Magento\\Backend\\Block\\Widget\\Grid\\Extended{\n    protected $coreRegistry = null;\n    protected $productFactory;\n\n\n    public function __construct(\n        Context $context,\n        Data $backendHelper,\n        ProductFactory $productFactory,\n        OptionFactory $optionFactory,\n        Registry $coreRegistry,\n        array $data = []\n    )\n    {\n        $this->productFactory = $productFactory;\n        $this->optionFactory = $optionFactory;\n        $this->coreRegistry = $coreRegistry;\n        parent::__construct($context, $backendHelper, $data);\n    }\n    protected function _construct() {\n        parent::_construct();\n        $this->setId('product_grid');\n        $this->setDefaultSort('entity_id');\n        $this->setUseAjax(true);\n    }\n    protected function _addColumnFilterToCollection($column)\n    {\n        if ($column->getId() == 'products') {\n            $productIds = $this->_getSelectedProducts();\n            if (empty($productIds)) {\n                $productIds = 0;\n            }\n            if ($column->getFilter()->getValue()) {\n                $this->getCollection()->addFieldToFilter('entity_id', ['in' => $productIds]);\n            } else {\n                if ($productIds) {\n                    $this->getCollection()->addFieldToFilter('entity_id', ['nin' => $productIds]);\n                }\n            }\n        } else {\n            parent::_addColumnFilterToCollection($column);\n        }\n        return $this;\n    }\n    protected function _prepareCollection()    {\n        $collection = $this->productFactory->create()->getCollection()->addAttributeToSelect(\n            '*'\n        );\n        $this->setCollection($collection);\n       return parent::_prepareCollection();\n    }\n    protected function _prepareColumns()\n    {\n        $this->addColumn(\n            'products',\n            [\n                'type' => 'checkbox',\n                'field_name' => 'products_id[]',\n                'required' => true,\n                'values' => $this->_getSelectedProducts(),\n                'align' => 'center',\n                'index' => 'entity_id',\n                'header_css_class' => 'col-select',\n                'column_css_class' => 'col-select'\n            ]\n        );\n        $this->addColumn(\n            'name',\n            [\n                'header' => __('Name'),\n                'index' => 'name',\n                'header_css_class' => 'col-name',\n                'column_css_class' => 'col-name'\n            ]\n        );\n        $this->addColumn(\n            'sku',\n            [\n                'header' => __('SKU'),\n                'index' => 'sku',\n                'header_css_class' => 'col-sku',\n                'column_css_class' => 'col-sku'\n            ]\n        );\n        return parent::_prepareColumns();\n    }\n    public function getGridUrl(){\n        return $this->_getData('grid_url') ? $this->_getData('grid_url') : $this->getUrl('*\/*\/productgrid', ['_current' => true]);\n    }\n    protected function _getSelectedProducts(){\n        $products = array_keys($this->getSelectedProducts());\n        return $products;\n    }\n\n\n    public function getSelectedProducts(){\n        $tm_id = $this->getRequest()->getParam('id');\n        if (!isset($tm_id)) {\n            $tm_id = 0;\n        }\n        $collection = $this->optionFactory->create()->load($tm_id);\n        $data = $collection->getProducts();\n        $products = explode(',', $data);\n        $proIds = array();\n        foreach ($products as $product) {\n            $proIds[$product] = array('id' => $product);\n        }\n        return $proIds;\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Follow these steps and save yourself from the tedious tasks!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Happy Coding!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Working the smart way is crucial in business and in Magento 2, you require to be smarter to perform actions quickly! Whenever you create a&#8230;<\/p>\n","protected":false},"author":5,"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-135","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/135","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\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=135"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/135\/revisions"}],"predecessor-version":[{"id":15636,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/135\/revisions\/15636"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=135"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=135"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=135"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}