TransWikia.com

Magento 2: How to Show different quantity for specific product type in product grid?

Magento Asked on February 20, 2021

I have a custom table for specific product type to store the product qty, the query to select the qty is like this for example:

SELECT a.qty FROM custom_product_table WHERE product_id = 1;

i noticed the qty column is rendered in this file

vendor/magento/module-catalog-inventory/Ui/DataProvider/Product/AddQuantityFieldToCollection.php

public function addField(Collection $collection, $field, $alias = null)
{
    $collection->joinField(
        'qty',
        'cataloginventory_stock_item',
        'qty',
        'product_id=entity_id',
        '{{table}}.stock_id=1',
        'left'
    );
}

How can i assign my custom query as qty column only when the product type id is my custom product type id, for example type_id = 5 ?

IF(e.type_id = 4) 
      (SELECT a.qty FROM custom_product_table WHERE product_id = e.entity_id) AS qty
ELSE
   $collection->joinField(
        'qty',
        'cataloginventory_stock_item',
        'qty',
        'product_id=entity_id',
        '{{table}}.stock_id=1',
        'left'
    ) AS qty

One Answer

Try the below code I am not sure but when try to use the Magento formatted SQL ideas we can get any solutions. Please try to get the type ID from the $collections.

IF(e.type_id = 4) 
      $collection->joinField(
        'qty',
        'custom_product_table',
        'qty',
        'product_id=entity_id',
        '{{table}}.stock_id=1',
        'left'
    ) AS qty
ELSE
   $collection->joinField(
        'qty',
        'cataloginventory_stock_item',
        'qty',
        'product_id=entity_id',
        '{{table}}.stock_id=1',
        'left'
    ) AS qty

Answered by Sabareesh on February 20, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP