0

I can't get the correct quantity from my products and i'm not able to see what I'm doing wrong.

use Magento\Catalog\Model\ProductRepository;
use Magento\CatalogInventory\Model\Stock\StockItemRepository;

/**
 * @var ProductRepository
 */
private $productRepository;

/**
 * @var StockItemRepository
 */
private $stockItemRepository;

$product = $this->productRepository->getById($productId);
$productStock = $this->stockItemRepository->get($productId);

var_dump($productId);                  // -----> string 51
var_dump($productStock->getQty());     // -----> float 30

Problem is now if I check the quantity from the database.

I' going to cataloginventory_stock_item the qty should be 0

Qty picture

The quantity is also apearing correctly as 0 in the backend...so as I have no idea where this 30 values is coming from.

qty-2 qty-3

3
  • 1
    try to run var_dump($productStock->getData()) then you'll get all data n check that if qty exists or not
    – Asad Khan
    Aug 9 '21 at 9:44
  • else try this $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $productStockObj = $objectManager->get('Magento\CatalogInventory\Api\StockRegistryInterface')->getStockItem($productId); print_r($productStockObj->getData());
    – Asad Khan
    Aug 9 '21 at 9:46
  • @AsadKhan Good catch printing getData revealed me that even having "51" as product id; then stock product id related is "49" which really has 30 as a quantity...this makes no sense...but I have something to work with now with that clue.
    – Claims
    Aug 9 '21 at 9:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.