0

I have joined column qty to Magento\Catalog\Model\ResourceModel\Product\Collection But Sorting filter by qty not working and sorting too in upsell_product_listing

/app/code/Vendor/UpsellProducts/etc/adminhtml/di.xml

    <virtualType name="upsell_product_listing_data_source" type="Magento\Catalog\Model\ResourceModel\Product\CollectionFactory">
    <arguments>
        <argument name="instanceName" xsi:type="string">Vendor\UpsellProducts\Model\Grid\JoinProductCollection</argument>
    </arguments>
</virtualType>
<type name="Magento\Catalog\Ui\DataProvider\Product\Related\UpSellDataProvider">
    <arguments>
        <argument name="collectionFactory" xsi:type="object">upsell_product_listing_data_source</argument>
    </arguments>
</type>

/app/code/Vendor/UpsellProducts/Model/Grid/JoinProductCollection.php

namespace Vendor\UpsellProducts\Model\Grid;

use Magento\Catalog\Model\ResourceModel\Product\Collection as ProductCollection;

class JoinProductCollection extends ProductCollection
{


    /**
     * @return OriginalCollection|void
     */
    protected function _initSelect()
    {
        $this->addFilterToMap('qty', '_inv.qty');

        parent::_initSelect();
    }

    /**
     *
     */
    protected function _renderFiltersBefore()
    {

        $this->getSelect()->joinLeft(
            array('_inv' => $this->getResource()->getTable('cataloginventory_stock_item')),
            "e.entity_id = _inv.product_id",
            array('qty')
        );

        parent::_renderFiltersBefore();
    }
}

Vendor/UpsellProducts/view/adminhtml/ui_component/upsell_product_listing.xml

        <column name="qty" component="Magento_Ui/js/grid/columns/select">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="dataType" xsi:type="string">text</item>
                <item name="sortOrder" xsi:type="number">60</item>
                <item name="filter" xsi:type="string">textRange</item>
                <item name="label" translate="true" xsi:type="string">Qty</item>
            </item>
        </argument>
    </column>

error when trying how to filter by qty

    main.CRITICAL: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'at_qty.qty' in 'where clause', query was: SELECT DISTINCT  COUNT(DISTINCT e.entity_id) FROM `catalog_product_entity` AS `e` WHERE

Can`t understand why in error is column "at_qty". Maybe in need to join the column somewhere else?

Your Answer

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

Browse other questions tagged or ask your own question.