Skip to content

Filter Product Grid Column Based on SQL calculation

I have a custom column within the Product Grid that states whether a product is In Stock or Out of Stock, based on whether or not the Salably Quantity is greater than zero.

While I have managed to get sorting for this column to work, I cannot get filtering to work, and have been getting the below error:

 SQLSTATE[42S22]: Column not found: 1054 Unknown column 'salable_quantity_status' in 'having clause', query was: SELECT COUNT(DISTINCT e.entity_id) FROM `catalog_product_entity` AS `e` HAVING (salable_quantity_status <= 0) at /home/proling/public_html/vendor/magento/framework/DB/Statement/Pdo/Mysql.php:110, PDOException(code: 42S22): SQLSTATE[42S22]: Column not found: 1054 Unknown column 'salable_quantity_status' in 'having clause' at /home/proling/public_html/vendor/magento/framework/DB/Statement/Pdo/Mysql.php:91)"} []

MyCustomModulesProductGridSortUiDataProviderProductProductDataProvider

<?php

namespace MyCustomModulesProductGridSortUiDataProviderProduct;

use MagentoFrameworkDBSelect;


class ProductDataProvider extends MagentoCatalogUiDataProviderProductProductDataProvider {

    public function addOrder($field, $direction) {
        // Check if the field is the custom column
        // Join tables to calculate salable_quantity_status
        $this->getCollection()
             ->getSelect()
             ->joinLeft(['isi' => $this->getCollection()
                                       ->getTable('inventory_source_item')], 'e.sku = isi.sku', [])
             ->joinLeft(['ir' => $this->getCollection()
                                      ->getTable('inventory_reservation')], 'isi.sku = ir.sku', [])
             ->columns(['salable_quantity_status' => new Zend_Db_Expr('(SUM(DISTINCT isi.quantity) + COALESCE(SUM(ir.quantity), 0))')])
             ->group('e.sku');

        // Check if the field is the custom column
        if ($field === 'salable_quantity_status') {
            // Add order to the main select
            $this->getCollection()->getSelect()->reset(Select::ORDER)->order('salable_quantity_status ' .
                                                                             strtoupper($direction));
        } else {
            // For other fields, use the default behavior
            parent::addOrder($field, $direction);
        }

        return $this;
    }

    public function addFilter(MagentoFrameworkApiFilter $filter) {
        if ($filter->getField() == 'salable_quantity_status') {
            if ($filter->getValue()[0] == 1) {
                $this->getCollection()->getSelect()->having('salable_quantity_status > ?', 0);
            } else {
                $this->getCollection()->getSelect()->having('salable_quantity_status <= ?', 0);
            }
        } else {
            parent::addFilter($filter);
        }
    }
}

It would appear that this issue is due to salable_quantity_status being an alias, and not a column within the database. Moreover, the error references COUNT(DISTINCT e.entity_id), which is not within my custom queries.

After several hours in, I am at a complete loss.