Пример #1
0
 /**
  * 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
0
 /**
  * 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;
 }
 /**
  * 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;
 }
 /**
  * @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
0
 /**
  * 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
0
 public function testGetStock()
 {
     $this->assertEquals($this->stock, $this->stockRegistry->getStock($this->websiteId));
 }