/** * prepare cart items URLs * * @return void * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function prepareItemUrls() { $products = []; /* @var $item \Magento\Quote\Model\Quote\Item */ foreach ($this->getItems() as $item) { $product = $item->getProduct(); $option = $item->getOptionByCode('product_type'); if ($option) { $product = $option->getProduct(); } if ($item->getStoreId() != $this->_storeManager->getStore()->getId() && !$item->getRedirectUrl() && !$product->isVisibleInSiteVisibility()) { $products[$product->getId()] = $item->getStoreId(); } } if ($products) { $products = $this->_catalogUrlBuilder->getRewriteByProductStore($products); foreach ($this->getItems() as $item) { $product = $item->getProduct(); $option = $item->getOptionByCode('product_type'); if ($option) { $product = $option->getProduct(); } if (isset($products[$product->getId()])) { $object = new \Magento\Framework\Object($products[$product->getId()]); $item->getProduct()->setUrlDataObject($object); } } } }
/** * Get array of last added items * * @param int|null $count * @return array */ public function getRecentItems($count = null) { if ($count === null) { $count = $this->getItemCount(); } $items = []; if (!$this->getSummaryCount()) { return $items; } $i = 0; $allItems = array_reverse($this->getItems()); foreach ($allItems as $item) { /* @var $item \Magento\Quote\Model\Quote\Item */ if (!$item->getProduct()->isVisibleInSiteVisibility()) { $productId = $item->getProduct()->getId(); $products = $this->_catalogUrl->getRewriteByProductStore([$productId => $item->getStoreId()]); if (!isset($products[$productId])) { continue; } $urlDataObject = new \Magento\Framework\Object($products[$productId]); $item->getProduct()->setUrlDataObject($urlDataObject); } $items[] = $item; if (++$i == $count) { break; } } return $items; }
/** * Rebuild all index data * * @return void * @throws \Exception */ public function reindexAll() { $this->_catalogResourceUrl->beginTransaction(); try { $this->_catalogUrl->refreshRewrites(); $this->_catalogResourceUrl->commit(); } catch (\Exception $e) { $this->_catalogResourceUrl->rollBack(); throw $e; } }
/** * Get array of last added items * * @return \Magento\Quote\Model\Quote\Item[] */ protected function getRecentItems() { $items = []; if (!$this->getSummaryCount()) { return $items; } foreach (array_reverse($this->getAllQuoteItems()) as $item) { /* @var $item \Magento\Quote\Model\Quote\Item */ if (!$item->getProduct()->isVisibleInSiteVisibility()) { $productId = $item->getProduct()->getId(); $products = $this->catalogUrl->getRewriteByProductStore([$productId => $item->getStoreId()]); if (!isset($products[$productId])) { continue; } $urlDataObject = new \Magento\Framework\Object($products[$productId]); $item->getProduct()->setUrlDataObject($urlDataObject); } $items[] = $this->itemPoolInterface->getItemData($item); } return $items; }
/** * Add or Move item product to shopping cart * * Return true if product was successful added or exception with code * Return false for disabled or unvisible products * * @param \Magento\Checkout\Model\Cart $cart * @param bool $delete delete the item after successful add to cart * @return bool * @throws \Magento\Catalog\Model\Product\Exception */ public function addToCart(\Magento\Checkout\Model\Cart $cart, $delete = false) { $product = $this->getProduct(); $storeId = $this->getStoreId(); if ($product->getStatus() != \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED) { return false; } if (!$product->isVisibleInSiteVisibility()) { if ($product->getStoreId() == $storeId) { return false; } $urlData = $this->_catalogUrl->getRewriteByProductStore([$product->getId() => $storeId]); if (!isset($urlData[$product->getId()])) { return false; } $product->setUrlDataObject(new \Magento\Framework\Object($urlData)); $visibility = $product->getUrlDataObject()->getVisibility(); if (!in_array($visibility, $product->getVisibleInSiteVisibilities())) { return false; } } if (!$product->isSalable()) { throw new ProductException(__('Product is not salable.')); } $buyRequest = $this->getBuyRequest(); $cart->addProduct($product, $buyRequest); if (!$product->isVisibleInSiteVisibility()) { $cart->getQuote()->getItemByProduct($product)->setStoreId($storeId); } if ($delete) { $this->delete(); } return true; }
/** * Prepare Url Data object * * @return $this */ protected function _prepareUrlDataObject() { $objects = array(); /** @var $item \Magento\Catalog\Model\Product */ foreach ($this->_items as $item) { if ($this->getFlag('do_not_use_category_id')) { $item->setDoNotUseCategoryId(true); } if (!$item->isVisibleInSiteVisibility() && $item->getItemStoreId()) { $objects[$item->getEntityId()] = $item->getItemStoreId(); } } if ($objects && $this->hasFlag('url_data_object')) { $objects = $this->_catalogUrl->getRewriteByProductStore($objects); foreach ($this->_items as $item) { if (isset($objects[$item->getEntityId()])) { $object = new \Magento\Framework\Object($objects[$item->getEntityId()]); $item->setUrlDataObject($object); } } } return $this; }
/** * @magentoDataFixture Magento/Catalog/Model/Resource/_files/url_rewrites.php */ public function testGetLastUsedRewriteRequestIncrement() { $this->assertEquals(1000, $this->_model->getLastUsedRewriteRequestIncrement('url-key-', '.html', 1)); }