Skip to content

Magento2 create custom product Grid

I have created below files but after input filter I’m getting the error :

appcodeMageplazaProductGridBlockAdminhtmlProductGrid.php

<?php

namespace MageplazaProductGridBlockAdminhtmlProduct;

use MagentoCatalogModelProductAttributeSourceStatus;
use MagentoCatalogModelProductVisibility;

class Grid extends MagentoBackendBlockWidgetGridExtended
{
    const DISPLAYCOLUMNS = 'mageplaza_productgrid/product/productgridcolumns';

    /**
     * Sales config
     *
     * @var MagentoSalesModelConfig
     */
    protected $_salesConfig;

    /**
     * Session quote
     *
     * @var MagentoBackendModelSessionQuote
     */
    protected $_sessionQuote;

    /**
     * Catalog config
     *
     * @var MagentoCatalogModelConfig
     */
    protected $_catalogConfig;

    protected $_collectionFactory;

    protected $scopeConfig;

    protected $date;

    protected $resourceConnection;


    /**
     * @param MagentoBackendBlockTemplateContext $context
     * @param MagentoBackendHelperData $backendHelper
     * @param MagentoCatalogModelProductFactory $productFactory
     * @param MagentoCatalogModelConfig $catalogConfig
     * @param MagentoBackendModelSessionQuote $sessionQuote
     * @param MagentoSalesModelConfig $salesConfig
     * @param array $data
     */
    public function __construct(
        MagentoBackendBlockTemplateContext $context,
        MagentoBackendHelperData $backendHelper,
        MagentoCatalogModelResourceModelProductCollectionFactory $collectionFactory,
        MagentoCatalogModelConfig $catalogConfig,
        MagentoBackendModelSessionQuote $sessionQuote,
        MagentoSalesModelConfig $salesConfig,
        MagentoFrameworkStdlibDateTimeDateTime $date,
        MagentoFrameworkAppResourceConnection $resourceConnection,
        MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig,
        array $data = []
    ) {
        $this->_collectionFactory = $collectionFactory;
        $this->_catalogConfig = $catalogConfig;
        $this->_sessionQuote = $sessionQuote;
        $this->_salesConfig = $salesConfig;
        $this->scopeConfig = $scopeConfig;
        $this->date = $date;
        $this->resourceConnection = $resourceConnection;
        parent::__construct($context, $backendHelper, $data);
    }

    protected function _construct()
    {
        parent::_construct();
        $this->setId('mageplaza_productgrid_product_grid');
        $this->setDefaultSort('entity_id');
        $this->setUseAjax(true);
    }

    protected function _addColumnFilterToCollection($column)
    {
        // Set custom filter for in product flag
        if ($column->getId() == 'in_products') {
            $productIds = $this->_getSelectedProducts();
            if (empty($productIds)) {
                $productIds = 0;
            }
            if ($column->getFilter()->getValue()) {
                $this->getCollection()->addFieldToFilter('entity_id', ['in' => $productIds]);
            } else {
                if ($productIds) {
                    $this->getCollection()->addFieldToFilter('entity_id', ['nin' => $productIds]);
                }
            }
        } else {
            parent::_addColumnFilterToCollection($column);
        }
        return $this;
    }

    /**
     * Retrieve quote store object
     *
     * @return MagentoStoreModelStore
     */
    public function getStore()
    {
        return $this->_sessionQuote->getStore();
    }

    /**
     * Retrieve quote object
     *
     * @return MagentoQuoteModelQuote
     */
    public function getQuote()
    {
        return $this->_sessionQuote->getQuote();
    }

    protected function _prepareCollection()
    {
        /* @var $collection MagentoCatalogModelResourceModelProductCollection */
        $collection = $this->_collectionFactory->create();
        $collection->setStore(
            $this->getStore()
        )->addAttributeToSelect(
            array('entity_id', 'name', 'sku')
        );

        $this->setCollection($collection);
        return parent::_prepareCollection();
    }

    protected function _prepareColumns()
    {

        $this->addColumn(
            'entity_id',
            [
                'header' => __('ID'),
                'sortable' => true,
                'header_css_class' => 'col-id',
                'column_css_class' => 'col-id',
                'index' => 'entity_id'
            ]
        );
        $this->addColumn(
            'name',
            [
                'header' => __('Name'),
                'index' => 'name',

            ]
        );

        $this->addColumn(
            'sku',
            [
                'header' => __('SKU'),
                'index' => 'sku'
            ]
        );

        return parent::_prepareColumns();
    }

    protected function _getSelectedProducts()
    {
        $products = $this->getRequest()->getPost('products', null);
        return $products;
    }

    public function getRowUrl($item)
    {
        return '';
    }
    public function getAbsoluteGridUrl($params = [])
    {
        return $this->getUrl('mageplaza_productgrid/product/listproducts');
    }
}

appcodeMageplazaProductGridviewadminhtmllayoutmageplaza_productgrid_product_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_con    figuration.xsd">
    <body>
        <referenceContainer name="content">
            <block class="MageplazaProductGridBlockAdminhtmlProduct" template="Mageplaza_ProductGrid::products.phtml" name="mageplaza.productgrid.product">
                <block class="MageplazaProductGridBlockAdminhtmlProductGrid" name="mageplaza.productgrid.product.grid" />
            </block>
        </referenceContainer>
    </body>
</page>

appcodeMageplazaProductGridControllerAdminhtmlProductIndex.php

<?php

namespace MageplazaProductGridControllerAdminhtmlProduct;

use MagentoBackendAppAction;
use MagentoBackendAppActionContext;
use MagentoFrameworkViewResultPageFactory;

class Index extends Action
{
    protected $resultPageFactory;
    public function __construct(
        Context $context,
        PageFactory $resultPageFactory
    ) {
        parent::__construct($context);
        $this->resultPageFactory = $resultPageFactory;
    }
    public function execute()
    {
        $resultPage = $this->resultPageFactory->create();
        $resultPage->getConfig()->getTitle()->prepend(__("Items"));
        $resultPage->setActiveMenu("Mageplaza_ProductGrid::mageplaza_productgrid_product");
        return $resultPage;
    }
}

appcodeMageplazaProductGridviewadminhtmllayoutmageplaza_productgrid_product_listproducts.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_con  figuration.xsd">
    <body>
        <referenceContainer name="content">
            <block class="MageplazaProductGridBlockAdminhtmlProduct" template="Mageplaza_ProductGrid::products.phtml" name="mageplaza.productgrid.product">
                <block class="MageplazaProductGridBlockAdminhtmlProductGrid" name="mageplaza.productgrid.product.grid" as="grid"/>
            </block>
        </referenceContainer>
    </body>
</page>

appcodeMageplazaProductGridControllerAdminhtmlProductListProducts.php

<?php

namespace MageplazaProductGridControllerAdminhtmlProduct;

use MagentoBackendAppAction;
use MagentoBackendAppActionContext;
use MagentoFrameworkViewResultPageFactory;

class ListProducts extends MagentoCatalogControllerAdminhtmlProductIndex
{
    protected $resultPageFactory;
    public function __construct(
        PageFactory $resultPageFactory
    ) {
        $this->resultPageFactory = $resultPageFactory;
    }
    public function execute()
    {
        $resultPage = $this->resultPageFactory->create();
        $result = $resultPage->getLayout()->renderElement('content');
        return $this->resultRawFactory->create()->setContents($result);
    }
}

appcodeMageplazaProductGridBlockAdminhtmlProduct.php

<?php

namespace MageplazaProductGridBlockAdminhtml;

class Product extends MagentoSalesBlockAdminhtmlOrderCreateAbstractCreate
{
    protected function _construct()
    {
        parent::_construct();
        $this->setId('mageplaza_productgrid_product');
    }
}

enter image description here

Here is the error screenshot. I am trying to learn about the product grid creation.