Example #1
0
 public function execute()
 {
     $params = $this->getRequest()->getParams();
     /** @var \Magento\Checkout\Model\Cart $cart */
     $cart = $this->cartFactory->create();
     $successMessage = '';
     $websiteId = $this->storeManager->getStore()->getWebsiteId();
     foreach ($params as $key => $product) {
         if ($product && is_array($product)) {
             $productModel = $this->productFactory->create();
             // loadByAttribute() return false if the product was not found. There is no need to check the ID,
             // but lets stay on the safe side for the future Magento releases
             /** @var \Magento\Catalog\Model\Product $productBySKU */
             $productBySKU = $productModel->loadByAttribute('sku', $product['sku']);
             if (!$productBySKU || !($productId = $productBySKU->getId())) {
                 continue;
             }
             $stockItem = $this->stockItemApiFactory->create();
             /** @var \Magento\CatalogInventory\Model\ResourceModel\Stock\Item $stockItemResource */
             $stockItemResource = $this->stockItemApiResourceFactory->create();
             $stockItemResource->loadByProductId($stockItem, $productId, $websiteId);
             $qty = $stockItem->getQty();
             try {
                 if (!$cart->getQuote()->hasProductId($productId) && is_numeric($product['qty']) && $qty > $product['qty']) {
                     $cart->addProduct($productBySKU, (int) $product['qty']);
                     $successMessage .= __('%1 was added to your shopping cart.' . '</br>', $this->escaper->escapeHtml($productBySKU->getName()));
                 }
                 unset($params[$key]);
             } catch (\Exception $e) {
                 $this->rejoinerHelper->log($e->getMessage());
             }
         }
     }
     if (isset($params['coupon_code'])) {
         $cart->getQuote()->setCouponCode($params['coupon_code'])->collectTotals();
     }
     try {
         $cart->getQuote()->save();
         $cart->save();
     } catch (\Exception $e) {
         $this->rejoinerHelper->log($e->getMessage());
     }
     $this->checkoutSession->setCartWasUpdated(true);
     if ($successMessage) {
         $this->messageManager->addSuccess($successMessage);
     }
     $url = $this->_url->getUrl('checkout/cart/', ['updateCart' => true]);
     $this->getResponse()->setRedirect($url);
 }
 /**
  * @expectedException \Magento\Framework\Exception\CouldNotDeleteException
  * @expectedExceptionMessage Stock Item with id "1" does not exist.
  */
 public function testDeleteByIdException()
 {
     $id = 1;
     $this->stockItemFactoryMock->expects($this->once())->method('create')->willReturn($this->stockItemMock);
     $this->stockItemResourceMock->expects($this->once())->method('load')->with($this->stockItemMock, $id);
     $this->stockItemMock->expects($this->once())->method('getItemId')->willReturn(null);
     $this->assertTrue($this->model->deleteById($id));
 }
 /**
  * @inheritdoc
  */
 public function get($stockItemId)
 {
     $stockItem = $this->stockItemFactory->create();
     $this->resource->load($stockItem, $stockItemId);
     if (!$stockItem->getItemId()) {
         throw new NoSuchEntityException(__('Stock Item with id "%1" does not exist.', $stockItemId));
     }
     return $stockItem;
 }
 /**
  * Detect current stock and get appropriate stock item.
  *
  * @param \Magento\CatalogInventory\Model\StockRegistryProvider $subject
  * @param \Closure $proceed
  * @param int $productId
  * @param int $scopeId
  * @return \Magento\CatalogInventory\Api\Data\StockItemInterface
  */
 public function aroundGetStockItem(\Magento\CatalogInventory\Model\StockRegistryProvider $subject, \Closure $proceed, $productId, $scopeId)
 {
     $result = $this->_storageStockRegistry->getStockItem($productId, $scopeId);
     if (null === $result) {
         $criteria = $this->_factoryStockItemCrit->create();
         $criteria->setProductsFilter($productId);
         $stockId = $this->_toolStockManager->getCurrentStockId();
         $criteria->setStockFilter($stockId);
         $collection = $this->_repoStockItem->getList($criteria);
         $result = current($collection->getItems());
         if ($result && $result->getItemId()) {
             $this->_storageStockRegistry->setStockItem($productId, $scopeId, $result);
         } else {
             $result = $this->_factoryStockItem->create();
         }
     }
     return $result;
 }
 /**
  * @param int $productId
  * @param int $scopeId
  * @return \Magento\CatalogInventory\Api\Data\StockItemInterface
  */
 public function getStockItem($productId, $scopeId)
 {
     $key = $scopeId . '/' . $productId;
     if (!isset($this->stockItems[$key])) {
         $criteria = $this->stockItemCriteriaFactory->create();
         $criteria->setProductsFilter($productId);
         $collection = $this->stockItemRepository->getList($criteria);
         $stockItem = current($collection->getItems());
         if ($stockItem && $stockItem->getItemId()) {
             $this->stockItems[$key] = $stockItem;
         } else {
             return $this->stockItemFactory->create();
         }
     }
     return $this->stockItems[$key];
 }
 /**
  * @param int $productId
  * @param int $scopeId
  * @return \Magento\CatalogInventory\Api\Data\StockItemInterface
  */
 public function getStockItem($productId, $scopeId)
 {
     $stockItem = $this->stockRegistryStorage->getStockItem($productId, $scopeId);
     if (null === $stockItem) {
         $criteria = $this->stockItemCriteriaFactory->create();
         $criteria->setProductsFilter($productId);
         $collection = $this->stockItemRepository->getList($criteria);
         $stockItem = current($collection->getItems());
         if ($stockItem && $stockItem->getItemId()) {
             $this->stockRegistryStorage->setStockItem($productId, $scopeId, $stockItem);
         } else {
             $stockItem = $this->stockItemFactory->create();
         }
     }
     return $stockItem;
 }
 /**
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 protected function setUp()
 {
     $this->objectManagerHelper = new ObjectManagerHelper($this);
     $this->stock = $this->getMockForAbstractClass('Magento\\CatalogInventory\\Api\\Data\\StockInterface', ['__wakeup', 'getStockId'], '', false);
     $this->stockItem = $this->getMockForAbstractClass('Magento\\CatalogInventory\\Api\\Data\\StockItemInterface', ['__wakeup', 'getItemId'], '', false);
     $this->stockStatus = $this->getMockForAbstractClass('Magento\\CatalogInventory\\Api\\Data\\StockStatusInterface', ['__wakeup', 'getProductId'], '', false);
     $this->stockFactory = $this->getMock('\\Magento\\CatalogInventory\\Api\\Data\\StockInterfaceFactory', ['create'], [], '', false);
     $this->stockFactory->expects($this->any())->method('create')->willReturn($this->stock);
     $this->stockItemFactory = $this->getMock('\\Magento\\CatalogInventory\\Api\\Data\\StockItemInterfaceFactory', ['create'], [], '', false);
     $this->stockItemFactory->expects($this->any())->method('create')->willReturn($this->stockItem);
     $this->stockStatusFactory = $this->getMock('\\Magento\\CatalogInventory\\Api\\Data\\StockStatusInterfaceFactory', ['create'], [], '', false);
     $this->stockStatusFactory->expects($this->any())->method('create')->willReturn($this->stockStatus);
     $this->stockRepository = $this->getMockBuilder('\\Magento\\CatalogInventory\\Api\\StockRepositoryInterface')->disableOriginalConstructor()->getMock();
     $this->stockItemRepository = $this->getMockBuilder('\\Magento\\CatalogInventory\\Api\\StockItemRepositoryInterface')->disableOriginalConstructor()->getMock();
     $this->stockStatusRepository = $this->getMockBuilder('\\Magento\\CatalogInventory\\Api\\StockStatusRepositoryInterface')->disableOriginalConstructor()->getMock();
     $this->stockCriteriaFactory = $this->getMock('Magento\\CatalogInventory\\Api\\StockCriteriaInterfaceFactory', ['create'], [], '', false);
     $this->stockCriteria = $this->getMockForAbstractClass('Magento\\CatalogInventory\\Api\\StockCriteriaInterface', ['setScopeFilter'], '', false);
     $this->stockItemCriteriaFactory = $this->getMock('Magento\\CatalogInventory\\Api\\StockItemCriteriaInterfaceFactory', ['create'], [], '', false);
     $this->stockItemCriteria = $this->getMockForAbstractClass('Magento\\CatalogInventory\\Api\\StockItemCriteriaInterface', ['setProductsFilter', 'setScopeFilter'], '', false);
     $this->stockStatusCriteriaFactory = $this->getMock('Magento\\CatalogInventory\\Api\\StockStatusCriteriaInterfaceFactory', ['create'], [], '', false);
     $this->stockStatusCriteria = $this->getMockForAbstractClass('Magento\\CatalogInventory\\Api\\StockStatusCriteriaInterface', ['setProductsFilter', 'setScopeFilter'], '', false);
     $this->stockRegistryProvider = $this->objectManagerHelper->getObject('\\Magento\\CatalogInventory\\Model\\StockRegistryProvider', ['stockRepository' => $this->stockRepository, 'stockFactory' => $this->stockFactory, 'stockItemRepository' => $this->stockItemRepository, 'stockItemFactory' => $this->stockItemFactory, 'stockStatusRepository' => $this->stockStatusRepository, 'stockStatusFactory' => $this->stockStatusFactory, 'stockCriteriaFactory' => $this->stockCriteriaFactory, 'stockItemCriteriaFactory' => $this->stockItemCriteriaFactory, 'stockStatusCriteriaFactory' => $this->stockStatusCriteriaFactory]);
 }
Example #8
0
 /**
  * Same as setData(), but also initiates the stock item (if it is there)
  *
  * @param array $data Array to form the object from
  * @return \Magento\Catalog\Model\Product
  */
 public function fromArray(array $data)
 {
     if (isset($data['stock_item'])) {
         if ($this->moduleManager->isEnabled('Magento_CatalogInventory')) {
             $stockItem = $this->_stockItemFactory->create();
             $this->dataObjectHelper->populateWithArray($stockItem, $data['stock_item'], '\\Magento\\CatalogInventory\\Api\\Data\\StockItemInterface');
             $stockItem->setProduct($this);
             $this->setStockItem($stockItem);
         }
         unset($data['stock_item']);
     }
     $this->setData($data);
     return $this;
 }