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); }
/** * @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; }
/** * 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; }