本文整理汇总了PHP中Magento\CatalogInventory\Api\StockRegistryInterface::getStock方法的典型用法代码示例。如果您正苦于以下问题:PHP StockRegistryInterface::getStock方法的具体用法?PHP StockRegistryInterface::getStock怎么用?PHP StockRegistryInterface::getStock使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Magento\CatalogInventory\Api\StockRegistryInterface
的用法示例。
在下文中一共展示了StockRegistryInterface::getStock方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getStockId
/**
* Retrieve stock_id by store
*
* @param int $websiteId The website Id
*
* @return int
*/
private function getStockId($websiteId)
{
if (!isset($this->stockIdByWebsite[$websiteId])) {
$stockId = $this->stockRegistry->getStock($websiteId)->getStockId();
$this->stockIdByWebsite[$websiteId] = $stockId;
}
return $this->stockIdByWebsite[$websiteId];
}
示例2: getStockId
/**
* Retrieve stock identifier
*
* @return int
*/
public function getStockId()
{
$stockId = $this->getData(static::STOCK_ID);
if ($stockId === null) {
$stockId = $this->stockRegistry->getStock($this->getWebsiteId())->getStockId();
}
return (int) $stockId;
}
示例3: saveStockItemData
/**
* Prepare stock item data for save
*
* @param \Magento\Catalog\Model\Product $product
* @return $this
*/
protected function saveStockItemData($product)
{
$stockItemData = $product->getStockData();
$stockItemData['product_id'] = $product->getId();
if (!isset($stockItemData['website_id'])) {
$stockItemData['website_id'] = $this->stockConfiguration->getDefaultScopeId();
}
$stockItemData['stock_id'] = $this->stockRegistry->getStock($stockItemData['website_id'])->getStockId();
foreach ($this->paramListToCheck as $dataKey => $configPath) {
if (null !== $product->getData($configPath['item']) && null === $product->getData($configPath['config'])) {
$stockItemData[$dataKey] = false;
}
}
$originalQty = $product->getData('stock_data/original_inventory_qty');
if (strlen($originalQty) > 0) {
$stockItemData['qty_correction'] = (isset($stockItemData['qty']) ? $stockItemData['qty'] : 0) - $originalQty;
}
// todo resolve issue with builder and identity field name
$stockItem = $this->stockRegistry->getStockItem($stockItemData['product_id'], $stockItemData['website_id']);
$stockItem->addData($stockItemData);
$this->stockItemRepository->save($stockItem);
return $this;
}
示例4: validateStockItem
/**
* @param ProductInterface $product
* @param StockItemInterface $stockItem
* @throws LocalizedException
* @return void
*/
private function validateStockItem(ProductInterface $product, StockItemInterface $stockItem)
{
$defaultScopeId = $this->stockConfiguration->getDefaultScopeId();
$defaultStockId = $this->stockRegistry->getStock($defaultScopeId)->getStockId();
$stockId = $stockItem->getStockId();
if ($stockId !== null && $stockId != $defaultStockId) {
throw new LocalizedException(__('Invalid stock id: %1. Only default stock with id %2 allowed', $stockId, $defaultStockId));
}
$stockItemId = $stockItem->getItemId();
if ($stockItemId !== null && (!is_numeric($stockItemId) || $stockItemId <= 0)) {
throw new LocalizedException(__('Invalid stock item id: %1. Should be null or numeric value greater than 0', $stockItemId));
}
$defaultStockItemId = $this->stockRegistry->getStockItem($product->getId())->getItemId();
if ($defaultStockItemId && $stockItemId !== null && $defaultStockItemId != $stockItemId) {
throw new LocalizedException(__('Invalid stock item id: %1. Assigned stock item id is %2', $stockItemId, $defaultStockItemId));
}
}
示例5: _saveStockItem
/**
* Stock item saving.
*
* @return $this
*/
protected function _saveStockItem()
{
$indexer = $this->indexerRegistry->get('catalog_product_category');
/** @var $stockResource \Magento\CatalogInventory\Model\ResourceModel\Stock\Item */
$stockResource = $this->_stockResItemFac->create();
$entityTable = $stockResource->getMainTable();
while ($bunch = $this->_dataSourceModel->getNextBunch()) {
$stockData = [];
$productIdsToReindex = [];
// Format bunch to stock data rows
foreach ($bunch as $rowNum => $rowData) {
if (!$this->isRowAllowedToImport($rowData, $rowNum)) {
continue;
}
$row = [];
$row['product_id'] = $this->skuProcessor->getNewSku($rowData[self::COL_SKU])['entity_id'];
$productIdsToReindex[] = $row['product_id'];
$row['website_id'] = $this->stockConfiguration->getDefaultWebsiteId();
$row['stock_id'] = $this->stockRegistry->getStock($row['website_id'])->getStockId();
$stockItemDo = $this->stockRegistry->getStockItem($row['product_id'], $row['website_id']);
$existStockData = $stockItemDo->getData();
$row = array_merge($this->defaultStockData, array_intersect_key($existStockData, $this->defaultStockData), array_intersect_key($rowData, $this->defaultStockData), $row);
if ($this->stockConfiguration->isQty($this->skuProcessor->getNewSku($rowData[self::COL_SKU])['type_id'])) {
$stockItemDo->setData($row);
$row['is_in_stock'] = $this->stockStateProvider->verifyStock($stockItemDo);
if ($this->stockStateProvider->verifyNotification($stockItemDo)) {
$row['low_stock_date'] = $this->_localeDate->date(null, null, false)->format('Y-m-d H:i:s');
}
$row['stock_status_changed_auto'] = (int) (!$this->stockStateProvider->verifyStock($stockItemDo));
} else {
$row['qty'] = 0;
}
$stockData[] = $row;
}
// Insert rows
if (!empty($stockData)) {
$this->_connection->insertOnDuplicate($entityTable, $stockData);
}
if ($productIdsToReindex) {
$indexer->reindexList($productIdsToReindex);
}
}
return $this;
}
示例6: testGetStock
public function testGetStock()
{
$this->assertEquals($this->stock, $this->stockRegistry->getStock($this->websiteId));
}