You can use \Magento\InventoryApi\Api\SourceItemRepositoryInterface class with \Magento\Framework\Api\SearchCriteriaBuilder to get source item data by source code and product SKU.
Here are the sample model class
<?php
namespace MageExpert\Testing\Model;
class SourceItemModel
{
private $searchCriteriaBuilder;
private $sourceItemRepository;
public function __construct(
...
\Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder,
\Magento\InventoryApi\Api\SourceItemRepositoryInterface $sourceItemRepository
...
) {
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->sourceRepository = $sourceRepository;
}
public function getSourcesItems($souceCode, $sku)
{
$searchCriteria = $this->searchCriteriaBuilder
->addFilter('source_code', $souceCode)
->addFilter('sku', $sku)
->create();
$sourceItemData = $this->sourceItemRepository->getList($searchCriteria);
return $sourceItemData->getItems();
}
}
Now you can use getSourcesItems() function to get all source items by sources code
$sourceCode = 'your_store_code';
$sku = 'Product_1'
$sourceItems = $this->getSourcesItems($sourceCode);
foreach ($sourceItems as $sourceItem) {
print_r($sourceItem->getData());
}
OUTPUT:
Array
(
[source_item_id] => 96
[source_code] => your_store_code
[sku] => Product_1
[quantity] => 100.0000
[status] => 1
)
0 comments:
Post a Comment