Magento 2:从自定义来源获取库存数量
Magento 2: get Stock quantity from custom source
我使用此代码按产品 ID 检索库存数量。
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$StockState = $objectManager->get('\Magento\CatalogInventory\Api\StockStateInterface');
$qty = $StockState->getStockQty($product->getId(), $product->getStore()->getWebsiteId());
此代码段 returns 只有“默认来源”数量。
我的目标是检索“mycustom source”数量,例如:
可能吗?
当然可以。请参阅以下代码:
<?php
namespace YourNameSpace;
use Magento\InventoryApi\Api\SourceItemRepositoryInterface;
use Magento\Framework\Api\SearchCriteriaBuilder;
class YourClass
{
/**
* @var SourceItemRepositoryInterface
*/
protected SourceItemRepositoryInterface $sourceItems;
/**
* @var SearchCriteriaBuilder
*/
protected SearchCriteriaBuilder $searchCriteriaBuilder;
/**
* @param SourceItemRepositoryInterface $sourceItems
* @param SearchCriteriaBuilder $searchCriteriaBuilder
*/
public function __construct(
SourceItemRepositoryInterface $sourceItems,
SearchCriteriaBuilder $searchCriteriaBuilder
){
$this->sourceItems = $sourceItems;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
}
public function yourFunction() {
//Set source
$sku = $product->getSku();
$searchCriteria = $this->searchCriteriaBuilder
->addFilter('sku', $sku)
->create();
$sourceItemData = $this->sourceItems->getList($searchCriteria);
foreach ($sourceItemData->getItems() as $sourceItem){
//Get source qty
$sourceItem->getQuantity()
//Get source code
$sourceItem->getSourceCode();
}
}
}
}
您只需要提供SKU,然后您将从MyCustom Source获得8个,从Default Source获得47个。我将代码简化为更易读的形式,请在使用时修改它。
我使用此代码按产品 ID 检索库存数量。
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$StockState = $objectManager->get('\Magento\CatalogInventory\Api\StockStateInterface');
$qty = $StockState->getStockQty($product->getId(), $product->getStore()->getWebsiteId());
此代码段 returns 只有“默认来源”数量。 我的目标是检索“mycustom source”数量,例如:
可能吗?
当然可以。请参阅以下代码:
<?php
namespace YourNameSpace;
use Magento\InventoryApi\Api\SourceItemRepositoryInterface;
use Magento\Framework\Api\SearchCriteriaBuilder;
class YourClass
{
/**
* @var SourceItemRepositoryInterface
*/
protected SourceItemRepositoryInterface $sourceItems;
/**
* @var SearchCriteriaBuilder
*/
protected SearchCriteriaBuilder $searchCriteriaBuilder;
/**
* @param SourceItemRepositoryInterface $sourceItems
* @param SearchCriteriaBuilder $searchCriteriaBuilder
*/
public function __construct(
SourceItemRepositoryInterface $sourceItems,
SearchCriteriaBuilder $searchCriteriaBuilder
){
$this->sourceItems = $sourceItems;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
}
public function yourFunction() {
//Set source
$sku = $product->getSku();
$searchCriteria = $this->searchCriteriaBuilder
->addFilter('sku', $sku)
->create();
$sourceItemData = $this->sourceItems->getList($searchCriteria);
foreach ($sourceItemData->getItems() as $sourceItem){
//Get source qty
$sourceItem->getQuantity()
//Get source code
$sourceItem->getSourceCode();
}
}
}
}
您只需要提供SKU,然后您将从MyCustom Source获得8个,从Default Source获得47个。我将代码简化为更易读的形式,请在使用时修改它。