/** * Initialize item option * * @param \Magento\Quote\Model\Quote\Item\Option $option * @param \Magento\Quote\Model\Quote\Item $quoteItem * @param int $qty * * @return \Magento\Framework\Object * @throws \Magento\Framework\Exception\LocalizedException */ public function initialize(\Magento\Quote\Model\Quote\Item\Option $option, \Magento\Quote\Model\Quote\Item $quoteItem, $qty) { $optionValue = $option->getValue(); $optionQty = $qty * $optionValue; $increaseOptionQty = ($quoteItem->getQtyToAdd() ? $quoteItem->getQtyToAdd() : $qty) * $optionValue; $qtyForCheck = $this->quoteItemQtyList->getQty($option->getProduct()->getId(), $quoteItem->getId(), $quoteItem->getQuoteId(), $increaseOptionQty); $stockItem = $this->getStockItem($option, $quoteItem); $result = $this->stockState->checkQuoteItemQty($option->getProduct()->getId(), $optionQty, $qtyForCheck, $optionValue, $option->getProduct()->getStore()->getWebsiteId()); if ($result->getItemIsQtyDecimal() !== null) { $option->setIsQtyDecimal($result->getItemIsQtyDecimal()); } if ($result->getHasQtyOptionUpdate()) { $option->setHasQtyOptionUpdate(true); $quoteItem->updateQtyOption($option, $result->getOrigQty()); $option->setValue($result->getOrigQty()); /** * if option's qty was updates we also need to update quote item qty */ $quoteItem->setData('qty', intval($qty)); } if ($result->getMessage() !== null) { $option->setMessage($result->getMessage()); $quoteItem->setMessage($result->getMessage()); } if ($result->getItemBackorders() !== null) { $option->setBackorders($result->getItemBackorders()); } $stockItem->unsIsChildItem(); return $result; }
/** * Initialize stock item * * @param \Magento\CatalogInventory\Api\Data\StockItemInterface $stockItem * @param \Magento\Quote\Model\Quote\Item $quoteItem * @param int $qty * * @return \Magento\Framework\DataObject * @throws \Magento\Framework\Exception\LocalizedException * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function initialize(\Magento\CatalogInventory\Api\Data\StockItemInterface $stockItem, \Magento\Quote\Model\Quote\Item $quoteItem, $qty) { /** * When we work with subitem */ if ($quoteItem->getParentItem()) { $rowQty = $quoteItem->getParentItem()->getQty() * $qty; /** * we are using 0 because original qty was processed */ $qtyForCheck = $this->quoteItemQtyList->getQty($quoteItem->getProduct()->getId(), $quoteItem->getId(), $quoteItem->getQuoteId(), 0); } else { $increaseQty = $quoteItem->getQtyToAdd() ? $quoteItem->getQtyToAdd() : $qty; $rowQty = $qty; $qtyForCheck = $this->quoteItemQtyList->getQty($quoteItem->getProduct()->getId(), $quoteItem->getId(), $quoteItem->getQuoteId(), $increaseQty); } $productTypeCustomOption = $quoteItem->getProduct()->getCustomOption('product_type'); if ($productTypeCustomOption !== null) { // Check if product related to current item is a part of product that represents product set if ($this->typeConfig->isProductSet($productTypeCustomOption->getValue())) { $stockItem->setIsChildItem(true); } } $stockItem->setProductName($quoteItem->getProduct()->getName()); $result = $this->stockState->checkQuoteItemQty($quoteItem->getProduct()->getId(), $rowQty, $qtyForCheck, $qty, $quoteItem->getProduct()->getStore()->getWebsiteId()); if ($stockItem->hasIsChildItem()) { $stockItem->unsIsChildItem(); } if ($result->getItemIsQtyDecimal() !== null) { $quoteItem->setIsQtyDecimal($result->getItemIsQtyDecimal()); if ($quoteItem->getParentItem()) { $quoteItem->getParentItem()->setIsQtyDecimal($result->getItemIsQtyDecimal()); } } /** * Just base (parent) item qty can be changed * qty of child products are declared just during add process * exception for updating also managed by product type */ if ($result->getHasQtyOptionUpdate() && (!$quoteItem->getParentItem() || $quoteItem->getParentItem()->getProduct()->getTypeInstance()->getForceChildItemQtyChanges($quoteItem->getParentItem()->getProduct()))) { $quoteItem->setData('qty', $result->getOrigQty()); } if ($result->getItemUseOldQty() !== null) { $quoteItem->setUseOldQty($result->getItemUseOldQty()); } if ($result->getMessage() !== null) { $quoteItem->setMessage($result->getMessage()); } if ($result->getItemBackorders() !== null) { $quoteItem->setBackorders($result->getItemBackorders()); } return $result; }
/** * Get items * * @return Item[] */ public function getItems() { $items = $this->getParentBlock()->getItems(); $oldSuperMode = $this->getQuote()->getIsSuperMode(); $this->getQuote()->setIsSuperMode(false); foreach ($items as $item) { // To dispatch inventory event sales_quote_item_qty_set_after, set item qty $item->setQty($item->getQty()); if (!$item->getMessage()) { //Getting product ids for stock item last quantity validation before grid display $stockItemToCheck = []; $childItems = $item->getChildren(); if (count($childItems)) { foreach ($childItems as $childItem) { $stockItemToCheck[] = $childItem->getProduct()->getId(); } } else { $stockItemToCheck[] = $item->getProduct()->getId(); } foreach ($stockItemToCheck as $productId) { $check = $this->stockState->checkQuoteItemQty($productId, $item->getQty(), $item->getQty(), $item->getQty(), $this->getQuote()->getStore()->getWebsiteId()); $item->setMessage($check->getMessage()); $item->setHasError($check->getHasError()); } } if ($item->getProduct()->getStatus() == ProductStatus::STATUS_DISABLED) { $item->setMessage(__('This product is disabled.')); $item->setHasError(true); } } $this->getQuote()->setIsSuperMode($oldSuperMode); return $items; }
public function testCheckQuoteItemQty() { $this->assertEquals($this->objectResult, $this->stockState->checkQuoteItemQty($this->productId, $this->qty, $this->qty, $this->qty, $this->websiteId)); }