/**
  * Update stock item on the stock and distribute qty by lots.
  *
  * @param \Magento\CatalogInventory\Model\StockManagement $subject
  * @param \Closure $proceed
  * @param array $items
  * @param int $websiteId is not used
  * @throws \Magento\Framework\Exception\LocalizedException
  * @return null
  */
 public function aroundRegisterProductsSale(\Magento\CatalogInventory\Model\StockManagement $subject, \Closure $proceed, array $items, $websiteId)
 {
     /* This code is moved from original 'registerProductsSale' method. */
     /* replace websiteId by stockId */
     $stockId = $this->_manStock->getCurrentStockId();
     $def = $this->_manTrans->begin();
     $lockedItems = $this->_resourceStock->lockProductsStock(array_keys($items), $stockId);
     $fullSaveItems = $registeredItems = [];
     foreach ($lockedItems as $lockedItemRecord) {
         $productId = $lockedItemRecord['product_id'];
         $orderedQty = $items[$productId];
         /** @var \Magento\CatalogInventory\Api\Data\StockItemInterface $stockItem */
         $stockItem = $this->_providerStockRegistry->getStockItem($productId, $stockId);
         $stockItemId = $stockItem->getItemId();
         $canSubtractQty = $stockItemId && $this->_canSubtractQty($stockItem);
         if (!$canSubtractQty || !$this->_configStock->isQty($lockedItemRecord['type_id'])) {
             continue;
         }
         if (!$stockItem->hasAdminArea() && !$this->_stockState->checkQty($productId, $orderedQty)) {
             $this->_manTrans->rollback($def);
             throw new \Magento\Framework\Exception\LocalizedException(__('Not all of your products are available in the requested quantity.'));
         }
         if ($this->_canSubtractQty($stockItem)) {
             $stockItem->setQty($stockItem->getQty() - $orderedQty);
         }
         $registeredItems[$productId] = $orderedQty;
         if (!$this->_stockState->verifyStock($productId) || $this->_stockState->verifyNotification($productId)) {
             $fullSaveItems[] = $stockItem;
         }
     }
     $this->_resourceStock->correctItemsQty($registeredItems, $stockId, '-');
     $this->_manTrans->commit($def);
     return $fullSaveItems;
 }
Exemple #2
0
 /**
  * Add stock status information to products
  *
  * @param AbstractCollection $productCollection
  * @return void
  */
 public function addStockStatusToProducts(AbstractCollection $productCollection)
 {
     $websiteId = $this->storeManager->getStore($productCollection->getStoreId())->getWebsiteId();
     foreach ($productCollection as $product) {
         $productId = $product->getId();
         $stockStatus = $this->stockRegistryProvider->getStockStatus($productId, $websiteId);
         $status = $stockStatus->getStockStatus();
         $product->setIsSalable($status);
     }
 }
 /**
  * @param int $productId
  * @param int $websiteId
  * @return \Magento\CatalogInventory\Api\Data\StockStatusInterface
  */
 public function getStockStatus($productId, $websiteId = null)
 {
     //if (!$websiteId) {
     $websiteId = $this->stockConfiguration->getDefaultWebsiteId();
     //}
     return $this->stockRegistryProvider->getStockStatus($productId, $websiteId);
 }
 /**
  * @param int $productId
  * @param float $itemQty
  * @param float $qtyToCheck
  * @param float $origQty
  * @param int $scopeId
  * @return int
  */
 public function checkQuoteItemQty($productId, $itemQty, $qtyToCheck, $origQty, $scopeId = null)
 {
     if ($scopeId === null) {
         $scopeId = $this->stockConfiguration->getDefaultScopeId();
     }
     $stockItem = $this->stockRegistryProvider->getStockItem($productId, $scopeId);
     return $this->stockStateProvider->checkQuoteItemQty($stockItem, $itemQty, $qtyToCheck, $origQty);
 }
Exemple #5
0
 public function testAddStockStatusToProducts()
 {
     $storeId = 1;
     $productId = 2;
     $status = 'test';
     $productMock = $this->getMockBuilder('Magento\\Catalog\\Model\\Product')->disableOriginalConstructor()->setMethods(['setIsSalable', 'getId'])->getMock();
     $productMock->expects($this->once())->method('setIsSalable')->with($status);
     $stockStatusMock = $this->getMockBuilder('Magento\\CatalogInventory\\Api\\Data\\StockStatusInterface')->disableOriginalConstructor()->getMock();
     $stockStatusMock->expects($this->once())->method('getStockStatus')->willReturn($status);
     $productCollectionMock = $this->getMockBuilder('Magento\\Catalog\\Model\\ResourceModel\\Collection\\AbstractCollection')->disableOriginalConstructor()->getMock();
     $productCollectionMock->expects($this->any())->method('getItemById')->with($productId)->willReturn($productMock);
     $productCollectionMock->expects($this->any())->method('getStoreId')->willReturn($storeId);
     $productMock->expects($this->any())->method('getId')->willReturn($productId);
     $iteratorMock = new \ArrayIterator([$productMock]);
     $productCollectionMock->expects($this->any())->method('getIterator')->willReturn($iteratorMock);
     $this->stockRegistryProviderMock->expects($this->once())->method('getStockStatus')->withAnyParameters()->willReturn($stockStatusMock);
     $this->assertNull($this->stock->addStockStatusToProducts($productCollectionMock));
 }
Exemple #6
0
 /**
  * Add stock status information to products
  *
  * @param AbstractCollection $productCollection
  * @return void
  */
 public function addStockStatusToProducts(AbstractCollection $productCollection)
 {
     $websiteId = $this->getStockConfiguration()->getDefaultScopeId();
     foreach ($productCollection as $product) {
         $productId = $product->getId();
         $stockStatus = $this->stockRegistryProvider->getStockStatus($productId, $websiteId);
         $status = $stockStatus->getStockStatus();
         $product->setIsSalable($status);
     }
 }
Exemple #7
0
 protected function setUp()
 {
     $this->objectManagerHelper = new ObjectManagerHelper($this);
     $this->product = $this->getMock('Magento\\Catalog\\Model\\Product', ['__wakeup', 'getIdBySku'], [], '', false);
     $this->product->expects($this->any())->method('getIdBySku')->willReturn($this->productId);
     //getIdBySku
     $this->productFactory = $this->getMock('Magento\\Catalog\\Model\\ProductFactory', ['create'], [], '', false);
     $this->productFactory->expects($this->any())->method('create')->will($this->returnValue($this->product));
     $this->stock = $this->getMockForAbstractClass('Magento\\CatalogInventory\\Api\\Data\\StockInterface', ['__wakeup'], '', false);
     $this->stockItem = $this->getMockBuilder('Magento\\CatalogInventory\\Api\\Data\\StockItemInterface')->setMethods(['setProductId', 'getData', 'addData', 'getItemId'])->disableOriginalConstructor()->getMockForAbstractClass();
     $this->stockStatus = $this->getMockForAbstractClass('Magento\\CatalogInventory\\Api\\Data\\StockStatusInterface', ['__wakeup'], '', false);
     $this->stockRegistryProvider = $this->getMockForAbstractClass('Magento\\CatalogInventory\\Model\\Spi\\StockRegistryProviderInterface', ['getStock', 'getStockItem', 'getStockStatus'], '', false);
     $this->stockRegistryProvider->expects($this->any())->method('getStock')->will($this->returnValue($this->stock));
     $this->stockRegistryProvider->expects($this->any())->method('getStockItem')->will($this->returnValue($this->stockItem));
     $this->stockRegistryProvider->expects($this->any())->method('getStockStatus')->will($this->returnValue($this->stockStatus));
     $this->stockItemRepository = $this->getMockForAbstractClass('\\Magento\\CatalogInventory\\Api\\StockItemRepositoryInterface', ['save'], '', false);
     $this->stockItemRepository->expects($this->any())->method('save')->will($this->returnValue($this->stockItem));
     $this->stockRegistry = $this->objectManagerHelper->getObject('\\Magento\\CatalogInventory\\Model\\StockRegistry', ['stockRegistryProvider' => $this->stockRegistryProvider, 'productFactory' => $this->productFactory, 'stockItemRepository' => $this->stockItemRepository]);
 }
 public function testGetStockStatus()
 {
     $this->stockStatusCriteriaFactory->expects($this->once())->method('create')->willReturn($this->stockStatusCriteria);
     $this->stockStatusCriteria->expects($this->once())->method('setScopeFilter')->willReturn(null);
     $this->stockStatusCriteria->expects($this->once())->method('setProductsFilter')->willReturn(null);
     $stockStatusCollection = $this->getMock('\\Magento\\CatalogInventory\\Model\\ResourceModel\\Stock\\Status\\Collection', ['getFirstItem', '__wakeup', 'getItems'], [], '', false);
     $stockStatusCollection->expects($this->once())->method('getItems')->willReturn([$this->stockStatus]);
     $this->stockStatusRepository->expects($this->once())->method('getList')->willReturn($stockStatusCollection);
     $this->stockStatus->expects($this->once())->method('getProductId')->willReturn($this->productId);
     $this->assertEquals($this->stockStatus, $this->stockRegistryProvider->getStockStatus($this->productId, $this->scopeId));
 }
 protected function setUp()
 {
     $this->objectManagerHelper = new ObjectManagerHelper($this);
     $this->stock = $this->getMock('\\Magento\\CatalogInventory\\Api\\Data\\StockInterface', [], [], '', false);
     $this->stockItem = $this->getMock('\\Magento\\CatalogInventory\\Api\\Data\\StockItemInterface', [], [], '', false);
     $this->stockStatus = $this->getMock('\\Magento\\CatalogInventory\\Api\\Data\\StockStatusInterface', [], [], '', false);
     $this->objectResult = $this->getMock('\\Magento\\Framework\\Object', [], [], '', false);
     $this->stockStateProvider = $this->getMock('Magento\\CatalogInventory\\Model\\Spi\\StockStateProviderInterface', ['verifyStock', 'verifyNotification', 'checkQty', 'suggestQty', 'getStockQty', 'checkQtyIncrements', 'checkQuoteItemQty'], [], '', false);
     $this->stockStateProvider->expects($this->any())->method('verifyStock')->willReturn(true);
     $this->stockStateProvider->expects($this->any())->method('verifyNotification')->willReturn(true);
     $this->stockStateProvider->expects($this->any())->method('checkQty')->willReturn(true);
     $this->stockStateProvider->expects($this->any())->method('suggestQty')->willReturn($this->qty);
     $this->stockStateProvider->expects($this->any())->method('getStockQty')->willReturn($this->qty);
     $this->stockStateProvider->expects($this->any())->method('checkQtyIncrements')->willReturn($this->objectResult);
     $this->stockStateProvider->expects($this->any())->method('checkQuoteItemQty')->willReturn($this->objectResult);
     $this->stockRegistryProvider = $this->getMock('Magento\\CatalogInventory\\Model\\Spi\\StockRegistryProviderInterface', ['getStock', 'getStockItem', 'getStockStatus'], [], '', false);
     $this->stockRegistryProvider->expects($this->any())->method('getStock')->will($this->returnValue($this->stock));
     $this->stockRegistryProvider->expects($this->any())->method('getStockItem')->will($this->returnValue($this->stockItem));
     $this->stockRegistryProvider->expects($this->any())->method('getStockStatus')->will($this->returnValue($this->stockStatus));
     $this->stockState = $this->objectManagerHelper->getObject('\\Magento\\CatalogInventory\\Model\\StockState', ['stockStateProvider' => $this->stockStateProvider, 'stockRegistryProvider' => $this->stockRegistryProvider]);
 }
 /**
  * Get back to stock (when order is canceled or whatever else)
  *
  * @param int $productId
  * @param float $qty
  * @param int $scopeId
  * @return bool
  */
 public function backItemQty($productId, $qty, $scopeId = null)
 {
     //if (!$scopeId) {
     $scopeId = $this->stockConfiguration->getDefaultScopeId();
     //}
     $stockItem = $this->stockRegistryProvider->getStockItem($productId, $scopeId);
     if ($stockItem->getItemId() && $this->stockConfiguration->isQty($this->getProductType($productId))) {
         if ($this->canSubtractQty($stockItem)) {
             $stockItem->setQty($stockItem->getQty() + $qty);
         }
         if ($this->stockConfiguration->getCanBackInStock($stockItem->getStoreId()) && $stockItem->getQty() > $stockItem->getMinQty()) {
             $stockItem->setIsInStock(true);
             $stockItem->setStockStatusChangedAutomaticallyFlag(true);
         }
         $stockItem->save();
     }
     return true;
 }
Exemple #11
0
 /**
  * Process Parents by child
  *
  * @param int $productId
  * @param int $websiteId
  * @return $this
  */
 protected function processParents($productId, $websiteId)
 {
     $parentIds = [];
     foreach ($this->getProductTypeInstances() as $typeInstance) {
         /* @var $typeInstance AbstractType */
         $parentIds = array_merge($parentIds, $typeInstance->getParentIdsByChild($productId));
     }
     if (!$parentIds) {
         return $this;
     }
     foreach ($parentIds as $parentId) {
         $item = $this->stockRegistryProvider->getStockItem($parentId, $websiteId);
         $status = \Magento\CatalogInventory\Model\Stock\Status::STATUS_IN_STOCK;
         $qty = 0;
         if ($item->getItemId()) {
             $status = $item->getIsInStock();
             $qty = $item->getQty();
         }
         $this->processChildren($parentId, $websiteId, $qty, $status);
     }
 }