Exemplo n.º 1
0
 /**
  * @param int $productId
  * @return \Magento\CatalogInventory\Model\Stock\Item
  */
 public function retrieve($productId)
 {
     if (empty($this->stockItemRegistry[$productId])) {
         /** @var \Magento\CatalogInventory\Model\Stock\Item $stockItem */
         $stockItem = $this->stockItemFactory->create();
         $this->stockItemResource->loadByProductId($stockItem, $productId);
         $this->stockItemRegistry[$productId] = $stockItem;
     }
     return $this->stockItemRegistry[$productId];
 }
Exemplo n.º 2
0
 /**
  * Get back to stock (when order is canceled or whatever else)
  *
  * @param int $productId
  * @param int|float $qty
  * @return $this
  */
 public function backItemQty($productId, $qty)
 {
     /** @var Item $stockItem */
     $stockItem = $this->_stockItemFactory->create()->loadByProduct($productId);
     if ($stockItem->getId() && $this->stockItemService->isQty($this->getProductType($productId))) {
         $stockItem->addQty($qty);
         if ($stockItem->getCanBackInStock() && $stockItem->getQty() > $stockItem->getMinQty()) {
             $stockItem->setIsInStock(true)->setStockStatusChangedAutomaticallyFlag(true);
         }
         $stockItem->save();
     }
     return $this;
 }
Exemplo n.º 3
0
 /**
  * Check product inventory data when quote item quantity declaring
  *
  * @param \Magento\Framework\Event\Observer $observer
  *
  * @return void
  * @throws \Magento\Framework\Model\Exception
  */
 public function validate(\Magento\Framework\Event\Observer $observer)
 {
     /* @var $quoteItem \Magento\Sales\Model\Quote\Item */
     $quoteItem = $observer->getEvent()->getItem();
     if (!$quoteItem || !$quoteItem->getProductId() || !$quoteItem->getQuote() || $quoteItem->getQuote()->getIsSuperMode()) {
         return;
     }
     $qty = $quoteItem->getQty();
     /** @var \Magento\CatalogInventory\Model\Stock\Item $stockItem */
     $stockItem = $this->stockItemFactory->create()->loadByProduct($quoteItem->getProduct());
     $parentStockItem = false;
     /**
      * Check if product in stock. For composite products check base (parent) item stock status
      */
     if ($quoteItem->getParentItem()) {
         $parentStockItem = $this->stockItemFactory->create()->loadByProduct($quoteItem->getParentItem()->getProduct());
     }
     if ($stockItem) {
         if (!$stockItem->getIsInStock() || $parentStockItem && !$parentStockItem->getIsInStock()) {
             $quoteItem->addErrorInfo('cataloginventory', \Magento\CatalogInventory\Helper\Data::ERROR_QTY, __('This product is out of stock.'));
             $quoteItem->getQuote()->addErrorInfo('stock', 'cataloginventory', \Magento\CatalogInventory\Helper\Data::ERROR_QTY, __('Some of the products are currently out of stock.'));
             return;
         } else {
             // Delete error from item and its quote, if it was set due to item out of stock
             $this->_removeErrorsFromQuoteAndItem($quoteItem, \Magento\CatalogInventory\Helper\Data::ERROR_QTY);
         }
     }
     /**
      * Check item for options
      */
     if (($options = $quoteItem->getQtyOptions()) && $qty > 0) {
         $qty = $quoteItem->getProduct()->getTypeInstance()->prepareQuoteItemQty($qty, $quoteItem->getProduct());
         $quoteItem->setData('qty', $qty);
         if ($stockItem) {
             $result = $stockItem->checkQtyIncrements($qty);
             if ($result->getHasError()) {
                 $quoteItem->addErrorInfo('cataloginventory', \Magento\CatalogInventory\Helper\Data::ERROR_QTY_INCREMENTS, $result->getMessage());
                 $quoteItem->getQuote()->addErrorInfo($result->getQuoteMessageIndex(), 'cataloginventory', \Magento\CatalogInventory\Helper\Data::ERROR_QTY_INCREMENTS, $result->getQuoteMessage());
             } else {
                 // Delete error from item and its quote, if it was set due to qty problems
                 $this->_removeErrorsFromQuoteAndItem($quoteItem, \Magento\CatalogInventory\Helper\Data::ERROR_QTY_INCREMENTS);
             }
         }
         foreach ($options as $option) {
             $result = $this->optionInitializer->initialize($option, $quoteItem, $qty);
             if ($result->getHasError()) {
                 $option->setHasError(true);
                 $quoteItem->addErrorInfo('cataloginventory', \Magento\CatalogInventory\Helper\Data::ERROR_QTY, $result->getMessage());
                 $quoteItem->getQuote()->addErrorInfo($result->getQuoteMessageIndex(), 'cataloginventory', \Magento\CatalogInventory\Helper\Data::ERROR_QTY, $result->getQuoteMessage());
             } else {
                 // Delete error from item and its quote, if it was set due to qty lack
                 $this->_removeErrorsFromQuoteAndItem($quoteItem, \Magento\CatalogInventory\Helper\Data::ERROR_QTY);
             }
         }
     } else {
         /* @var $stockItem \Magento\CatalogInventory\Model\Stock\Item */
         if (!$stockItem instanceof \Magento\CatalogInventory\Model\Stock\Item) {
             throw new \Magento\Framework\Model\Exception(__('The stock item for Product in option is not valid.'));
         }
         $result = $this->stockItemInitializer->initialize($stockItem, $quoteItem, $qty);
         if ($result->getHasError()) {
             $quoteItem->addErrorInfo('cataloginventory', \Magento\CatalogInventory\Helper\Data::ERROR_QTY, $result->getMessage());
             $quoteItem->getQuote()->addErrorInfo($result->getQuoteMessageIndex(), 'cataloginventory', \Magento\CatalogInventory\Helper\Data::ERROR_QTY, $result->getQuoteMessage());
         } else {
             // Delete error from item and its quote, if it was set due to qty lack
             $this->_removeErrorsFromQuoteAndItem($quoteItem, \Magento\CatalogInventory\Helper\Data::ERROR_QTY);
         }
     }
 }
Exemplo n.º 4
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($data)
 {
     if (isset($data['stock_item'])) {
         if ($this->_catalogData->isModuleEnabled('Magento_CatalogInventory')) {
             $stockItem = $this->_stockItemFactory->create()->setData($data['stock_item'])->setProduct($this);
             $this->setStockItem($stockItem);
         }
         unset($data['stock_item']);
     }
     $this->setData($data);
     return $this;
 }
 /**
  * Set the product data.
  *
  * @param $product
  *
  * @return $this
  */
 public function setProduct($product)
 {
     $this->id = $product->getId();
     $this->sku = $product->getSku();
     $this->name = $product->getName();
     $status = $this->statusFactory->create()->getOptionText($product->getStatus());
     $this->status = $status->getText();
     $options = $this->visibilityFactory->create()->getOptionArray();
     $this->visibility = (string) $options[$product->getVisibility()];
     $this->price = (double) number_format($product->getPrice(), 2, '.', '');
     $this->specialPrice = (double) number_format($product->getSpecialPrice(), 2, '.', '');
     $this->url = $product->getProductUrl();
     $this->imagePath = $this->mediaConfigFactory->create()->getMediaUrl($product->getSmallImage());
     $stock = $this->itemFactory->create()->setProduct($product);
     $this->stock = (double) number_format($stock->getQty(), 2, '.', '');
     $shortDescription = $product->getShortDescription();
     //limit short description
     if (strlen($shortDescription) > 250) {
         $shortDescription = substr($shortDescription, 0, 250);
     }
     $this->shortDescription = $shortDescription;
     //category data
     $count = 0;
     $categoryCollection = $product->getCategoryCollection()->addNameToResult();
     foreach ($categoryCollection as $cat) {
         $this->categories[$count]['Id'] = $cat->getId();
         $this->categories[$count]['Name'] = $cat->getName();
         ++$count;
     }
     //website data
     $count = 0;
     $websiteIds = $product->getWebsiteIds();
     foreach ($websiteIds as $websiteId) {
         $website = $this->storeManager->getWebsite($websiteId);
         $this->websites[$count]['Id'] = $website->getId();
         $this->websites[$count]['Name'] = $website->getName();
         ++$count;
     }
     //bundle product options
     if ($product->getTypeId() == \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE) {
         $optionCollection = $product->getTypeInstance()->getOptionsCollection($product);
         $selectionCollection = $product->getTypeInstance()->getSelectionsCollection($product->getTypeInstance()->getOptionsIds($product), $product);
         $options = $optionCollection->appendSelections($selectionCollection);
         foreach ($options as $option) {
             $count = 0;
             $title = str_replace(' ', '', $option->getDefaultTitle());
             $selections = $option->getSelections();
             $sOptions = [];
             foreach ($selections as $selection) {
                 $sOptions[$count]['name'] = $selection->getName();
                 $sOptions[$count]['sku'] = $selection->getSku();
                 $sOptions[$count]['id'] = $selection->getProductId();
                 $sOptions[$count]['price'] = (double) number_format($selection->getPrice(), 2, '.', '');
                 ++$count;
             }
             $this->{$title} = $sOptions;
         }
     }
     //configurable product options
     if ($product->getTypeId() == 'configurable') {
         $productAttributeOptions = $product->getTypeInstance(true)->getConfigurableAttributesAsArray($product);
         foreach ($productAttributeOptions as $productAttribute) {
             $count = 0;
             $label = strtolower(str_replace(' ', '', $productAttribute['label']));
             $options = [];
             foreach ($productAttribute['values'] as $attribute) {
                 $options[$count]['option'] = $attribute['default_label'];
                 if (isset($attribute['pricing_value'])) {
                     $options[$count]['price'] = (double) number_format($attribute['pricing_value'], 2, '.', '');
                 }
                 ++$count;
             }
             $this->{$label} = $options;
         }
     }
     unset($this->itemFactory, $this->mediaConfigFactory, $this->visibilityFactory, $this->statusFactory, $this->helper, $this->storeManager);
     return $this;
 }