1

I am unable to get simple product's quantity Can anyone help out please.

I have tried:

$product = $block->getAllowProducts();

foreach ($product  as $child) {
    print_r($child->getQty());
    print_r($child);
}

$child->getQty() returns nothing and it doesn't seem to be in $child array either

Array
(
  [entity_id] => 1
  [attribute_set_id] => 4
  [type_id] => simple
  [sku] => t-shirt-red
  [has_options] => 0
  [required_options] => 0
  [created_at] => 2017-10-24 10:05:40
  [updated_at] => 2017-10-24 10:05:40
  [parent_id] => 4
  [catalog_rule_price] => 
  [name] => t-shirt-red
  [url_key] => t-shirt-red
  [price] => 99.9900
  [weight] => 100.0000
  [color] => 4
  [status] => 1
  [tax_class_id] => 2
  [is_featured] => 1
  [media_gallery] => Array
    (
      [images] => Array
        (
        )

      [values] => Array
         (
         )

    )

[tier_price] => Array
    (
    )

[salable] => 1
)

Any help please, appreciate. Thank you!

1

2 Answers 2

1

You cant get directly product quantity. You need to call StockStateInterface to get qty of product.

For get qty based on product id,

public function __construct(\Magento\CatalogInventory\Api\StockStateInterface $stockState)
{        
    $this->stockState = $stockState;
}

public function getOptionsQty($currentProduct, $allowedProducts)
{
    $product = $block->getAllowProducts();

    foreach ($product  as $child) {
        $productId = $child->getId();
        $qty = $this->stockState->getStockQty($productId,1);          
        echo $qty;
        print_r($child);
    }
}

Using directly objectmanager,

$product = $block->getAllowProducts();

    foreach ($product  as $child) {
        $productId = $child->getId();
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $stockState = $objectManager->get('\Magento\CatalogInventory\Api\StockStateInterface');
        $qty = $stockState->getStockQty($productId,1);
        echo $qty;
        print_r($child);
    }
1
  • @Rakesk It works But I wasn't planing on using ObjectManager! I was more looking into something like this: $child->getExtensionAttributes()->getStockItem()->getQty() but it returns NULL Commented Oct 25, 2017 at 8:17
1

You can use this to get the qty.

$products = Mage::getModel('catalog/product')
     ->getCollection()
     //->addAttributeToSelect('*')
     ->addAttributeToSelect(array('name', 'thumbnail', 'weight' ,'price','description'))
     ->joinField('qty',
                 'cataloginventory/stock_item',
                 'qty',
                 'product_id=entity_id',
                 '{{table}}.stock_id=1',
                 'left');


foreach ($products as $product) {
  $p['products'][] = array(
        'id'        => $product->getId(),
        'sku'       => $product->getSku(),
        'name'      => $product->getName(),
    'description'   => $product->getDescription(),
      'weight'      => $product->getWeight(),
    'created at'    => $product->getCreatedAt(),
        'pirce'     => Mage::helper('core')->currency($product->getPrice(), true, false), //." ".$currencyCode,

     //get qty
     'qty'          => $product->getQty(),
     );
 }
3
  • @ Vivek , Your solution is in Magento-1, but the required solution should be in Magento-2.
    – MGento
    Commented Oct 26, 2017 at 5:07
  • Oh yes, that is for magento1, but you can use it in magento2 by changing some of its functions which are changed in M2
    – Vivek
    Commented Nov 1, 2017 at 5:21
  • 1
    this worked for me in magento 1 Commented Jun 13, 2018 at 11:11

Your Answer

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

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