InsideDarkWeb.com

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

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

Magento Asked on February 20, 2021

1 Answers

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!

Related Questions

is creating block within template in magento 2 good practice

0  Asked on July 24, 2020 by herve-tribouilloy

       

Ask a Question

Get help from others!

© 2021 InsideDarkWeb.com. All rights reserved.