0

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
1
  • Can you please specify in some more detail, what you are looking for? Nov 20, 2020 at 14:19

1 Answer 1

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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