2

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.

2 Answers 2

3

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.

3
  • I added this code in API and need to get data which pass in body text. How to get that value? Commented Dec 8, 2019 at 12:47
  • check my updated answer. Commented Dec 8, 2019 at 12:58
  • Thanks !! It's working.. Commented Dec 8, 2019 at 13:01
2

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>'; 
2
  • How to update also? Commented Dec 7, 2019 at 9:43
  • 1
    I checked in "cataloginventory_stock_item" table for product id 1, and there are backorders field value is "1" But, still it returns 0 Commented Dec 7, 2019 at 9:45

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.