문제

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