2

I have got salable salable quantity of product in magento 2.3 using object manager.

But we need to update quantity by programmatic or admin.
$getSalableQuantityDataBySku = $this->objectManager->create('\Magento\InventorySalesAdminUi\Model\GetSalableQuantityDataBySku');

$salable = $getSalableQuantityDataBySku->execute($sku);
                print_r($salable);

Output like in below :

    Array
    (
        [0] => Array
            (
                [stock_name] => Default Stock
                [qty] => 0
                [manage_stock] => 1
            )

)
1
  • have u update salable quantity of products? Sep 28, 2020 at 9:27

1 Answer 1

1

This is verified in Magento 2.3.2

public function __construct(
    \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry
)
{
    $this->stockRegistry = $stockRegistry;
    parent::__construct();
}

public function yourMethod() {
    $sku = 'ABC123';
    $qty = 10;
    $stockItem = $this->stockRegistry->getStockItemBySku($sku);
    $stockItem->setQty($qty);
    $stockItem->setIsInStock((bool)$qty); // this line
    $this->stockRegistry->updateStockItemBySku($sku, $stockItem);
}

Update Stock using ObjectManager

Create a sandbox script PHP file in magento root dir and add below code

require __DIR__ . '/app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();

$sku = 'demo';
$qty = 10;
$_sourceItemsSaveInterface = $objectManager->get('\Magento\InventoryApi\Api\SourceItemsSaveInterface');
$_sourceItemFactory = $objectManager->get('Magento\InventoryApi\Api\Data\SourceItemInterfaceFactory');
$sourceItem = $_sourceItemFactory->create();
$sourceItem->setSourceCode('default'); // default : stock source
$sourceItem->setSku($sku);
$sourceItem->setQuantity($qty);
$sourceItem->setStatus(1);
$_sourceItemsSaveInterface->execute([$sourceItem]);
9
  • What is the issue here? Dec 13, 2019 at 13:31
  • Error : Class Magento\CatalogInventory\Api\StockRegistryInterfac does not exist Dec 13, 2019 at 13:31
  • run php bin/magento setup:di:compile Dec 13, 2019 at 13:34
  • After compile same error Dec 13, 2019 at 13:47
  • @BaharuniAsif, Check my updated answer. Let me know Dec 13, 2019 at 14:25

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.