1

I want to get back orders by specific product id and update product's back orders status by specific product.

Any help would be appreciated.

Thanks.

1

You need to inject \Magento\Catalog\Api\ProductRepositoryInterface in your construct for get backorders details by specific product Id. Add this below code in your construct :

protected $productRepository;

public function __construct(
    \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
) {
    $this->productRepository = $productRepository;
}

Now, You need to add this below code in your function.

Get backorders by specific product :

$productId = 1;
$product = $this->productRepository->getById($productId);
$productStock = $product->getExtensionAttributes()->getStockItem();
echo $productStock['backorders'];

Update backorders by specifc product :

$productId = 1;
$product = $this->productRepository->getById($productId);
$stockData = ['backorders' => 1, 'use_config_backorders' => 1];
$product->setStockData($stockData);
$this->productRepository->save($product);

UPDATE:

You can body value which you pass in API get this by this below code :

protected $request;

public function __construct(
\Magento\Framework\Webapi\Rest\Request $request
){
$this->request = $request;  
}

$postBodyData = $this->request->getBodyParams();

Hope, It will helpful for you.

  • I added this code in API and need to get data which pass in body text. How to get that value? – Ankita Patel Dec 8 '19 at 12:47
  • check my updated answer. – Rohan Hapani Dec 8 '19 at 12:58
  • Thanks !! It's working.. – Ankita Patel Dec 8 '19 at 13:01
1

Try below code

$obj = \Magento\Framework\App\ObjectManager::getInstance();  
$stockRegistry = $obj->get('Magento\CatalogInventory\Api\StockRegistryInterface');
$stockitem = $stockRegistry->getStockItem($_product->getId(),$_product->getStore()->getWebsiteId());
echo "Backorder: "; echo $stockitem->getBackorders(); echo '<br>';
  • How to update also? – Ankita Patel Dec 7 '19 at 9:43
  • I checked in "cataloginventory_stock_item" table for product id 1, and there are backorders field value is "1" But, still it returns 0 – Ankita Patel Dec 7 '19 at 9:45

Your Answer

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

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