3

I've one grid similar to product grid.

<?php
namespace LazyCoder\WarehouseListing\Block\Adminhtml\Grid;
use Magento\Backend\Block\Widget\Grid\Extended;
use Magento\Backend\Block\Template\Context;
use Magento\Backend\Helper\Data;
use Magento\Framework\Registry;
use Magento\Framework\ObjectManagerInterface;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as DemoCollection;
use Magento\InventoryApi\Api\GetSourceItemsBySkuInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;

// also you can use Magento Default CollectionFactory
class Grid extends Extended
{
    protected $registry;
    protected $_objectManager = null;
    protected $demoFactory;
    public function __construct(
        Context $context,
        Data $backendHelper,
        Registry $registry,
        ObjectManagerInterface $objectManager,
        DemoCollection $demoFactory,
         \Magento\Catalog\Model\ProductFactory $productFactory,
         GetSourceItemsBySkuInterface $sourceItemsBySku,
         ProductRepositoryInterface $productRepo,
         \Magento\Backend\Model\Session $session,
        array $data = []
    ) {
        $this->_objectManager = $objectManager;
        $this->registry = $registry;
        $this->demoFactory = $demoFactory;
        $this->productFactory = $productFactory;
        $this->getSourceItemsBySku = $sourceItemsBySku;
        $this->productRepository  = $productRepo;
        $this->session = $session;
        parent::__construct($context, $backendHelper, $data);
    }
    protected function _construct()
    {
        parent::_construct();
        $this->setId('index');
        $this->setDefaultSort('created_at');
        $this->setDefaultDir('DESC');
        $this->setSaveParametersInSession(true);
    }
    protected function _prepareCollection()
    {

        $demo = $this->demoFactory->create()
            ->addFieldToSelect('*');
        $demo->addFieldToFilter('entity_id', array('neq' => ''));

        $this->setCollection($demo);
        return parent::_prepareCollection();
    }
    protected function _prepareColumns()
    {
        $this->addColumn(
            'entity_id',
            [
                'header_css_class' => 'a-center',
                'type' => 'checkbox',
                'name' => 'entity_id',
                'align' => 'center',
                'index' => 'entity_id',
            ]
        );
        $this->addColumn(
            'entity_id',
            [
                'header' => __('ID'),
                'type' => 'number',
                'index' => 'entity_id',
                'header_css_class' => 'col-id',
                'column_css_class' => 'col-id',
            ]
        );
        $this->addColumn(
            'name',
            [
                'header' => __('Name'),
                'type' => 'text',
                'index' => 'name',
                'header_css_class' => 'col-id',
                'column_css_class' => 'col-id',
            ]
        );
        $this->addColumn(
            'sku',
            [
                'header' => __('SKU'),
                'index' => 'sku',
                'class' => '',
                'width' => '125px',
            ]
        );
        $this->addColumn(
            'color',
            [
                'header' => __('Color'),
                'index' => 'color',
                'class' => '',
                'width' => '125px',
                'renderer'  => 'LazyCoder\WarehouseListing\Block\Adminhtml\Listing\Edit\Tab\Renderer\Color','filter_condition_callback' => [$this, '_filterCollectionByColor'],
            ]
        );

       $this->addColumn('size', 
    [
        'header' => __('Size'),
        'index' => 'size',
        'renderer'  =>'LazyCoder\WarehouseListing\Block\Adminhtml\Listing\Edit\Tab\Renderer\Size','filter_condition_callback' => [$this, '_filterCollection'],
    ]);


       $this->addColumn(
            'warehouse',
            [
                'header' => __('Warehouse'),
                'index' => 'warehouse',
                'class' => '',
                'width' => '125px',
                'renderer'  => 'LazyCoder\WarehouseListing\Block\Adminhtml\Listing\Edit\Tab\Renderer\Warehouse','filter_condition_callback' => [$this, '_filterCollectionByWarehouse'],
            ]
        );

       return parent::_prepareColumns();
    }

    public function getGridUrl()
    {
        return $this->getUrl('*/*/index', ['_current' => true]);
    }


    protected function _filterCollection($collection, $column)
    {

        $value = trim($column->getFilter()->getValue());

        $_product = $this->productFactory->create();
        $isAttributeExist = $_product->getResource()->getAttribute('size');
        $optionId = '';
        if ($isAttributeExist && $isAttributeExist->usesSource()) {
            $optionId = $isAttributeExist->getSource()->getOptionId($value);
        }

        $this->getCollection()->addAttributeToFilter('size', array('eq' => $optionId));


        return $this;
    }

    protected function _filterCollectionByColor($collection, $column)
    {

        $value = trim($column->getFilter()->getValue());

        $_product = $this->productFactory->create();
        $isAttributeExist = $_product->getResource()->getAttribute('color');
        $optionId = '';
        if ($isAttributeExist && $isAttributeExist->usesSource()) {
            $optionId = $isAttributeExist->getSource()->getOptionId($value);
        }

        $this->getCollection()->addAttributeToFilter('color', array('eq' => $optionId));


        return $this;
    }

    protected function _filterCollectionByWarehouse($collection, $column)
    {

        $value = trim($column->getFilter()->getValue());

        //$this->session->setMyValue($value);

        $select = $this->getCollection()->getSelect()->joinLeft(
                ['isi' => 'inventory_source_item'],
                'e.sku = isi.sku',
                ['source_code' => new \Zend_Db_Expr('group_concat(`isi`.source_code)'),]
            )->where('isi.source_code = ?', $value)->group('e.sku'); 

        return $this;
    }

}

I can able to filter all other columns expect warehouse column. Here's how I get warehouse column value. I used renderer.

$sourceItems = $this->getSourceItemsBySku->execute($row->getSku());


        foreach($sourceItems as $sourceItem){
            $value[] = $sourceItem->getSourceCode().':'.$sourceItem->getQuantity();
        }


        if(empty($value)){
            return '';
        }else{
            return implode('<br>', $value);
        }

If you check below image, I took one SKU as an example, I've joined 2 tables data to get warehouse values.

Note : I've used default multi source inventory here term warehouse means source.

If I enter china to warehouse column and hit enter then it worked as I expected but after filter cant able to navigate to other pages. Take a look at below.

https://streamable.com/u1yom

In join main table is catalog_product_entity and joined with inventory_source_item. Take look at below.

enter image description here enter image description here

If you can see other table having warehouse information, but its duplicated raw per sku, Like if SKU has one more warehouse assigned then it there'll be 4 entries in inventory_source_item table.

Does any one have idea what's wrong here ? I'm sure there's very small mistake but cant get that. Any help would be appreciated.

0

I think you should remove ->group('e.sku') or use ->group('e.entity_id') and check. Might your issue will fix.

| improve this answer | |
  • I did try same thing but same output no pagination after filter. – anonymous Oct 10 '19 at 4:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.