سؤال

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.

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

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>';
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى magento.stackexchange
scroll top