/** * Calculate count of product index items cache * * @return $this */ public function calculate() { $collection = $this->getCollection()->setCustomerId($this->getCustomerId())->addIndexFilter()->setVisibility($this->_productVisibility->getVisibleInSiteIds()); $count = $collection->getSize(); $this->_getSession()->setData($this->_countCacheKey, $count); return $this; }
/** * Get category collection array * * @param null|string|bool|int|\Magento\Store\Model\Store $storeId * @return array|bool */ public function getCollection($storeId) { $products = []; /* @var $store \Magento\Store\Model\Store */ $store = $this->_storeManager->getStore($storeId); if (!$store) { return false; } $adapter = $this->_getWriteAdapter(); $this->_select = $adapter->select()->from(['e' => $this->getMainTable()], [$this->getIdFieldName(), 'updated_at'])->joinInner(['w' => $this->getTable('catalog_product_website')], 'e.entity_id = w.product_id', [])->joinLeft(['url_rewrite' => $this->getTable('url_rewrite')], 'e.entity_id = url_rewrite.entity_id AND url_rewrite.is_autogenerated = 1' . $adapter->quoteInto(' AND url_rewrite.store_id = ?', $store->getId()) . $adapter->quoteInto(' AND url_rewrite.entity_type = ?', ProductUrlRewriteGenerator::ENTITY_TYPE), ['url' => 'request_path'])->where('w.website_id = ?', $store->getWebsiteId()); $this->_addFilter($store->getId(), 'visibility', $this->_productVisibility->getVisibleInSiteIds(), 'in'); $this->_addFilter($store->getId(), 'status', $this->_productStatus->getVisibleStatusIds(), 'in'); // Join product images required attributes $imageIncludePolicy = $this->_sitemapData->getProductImageIncludePolicy($store->getId()); if (\Magento\Sitemap\Model\Source\Product\Image\IncludeImage::INCLUDE_NONE != $imageIncludePolicy) { $this->_joinAttribute($store->getId(), 'name'); $this->_select->columns(['name' => $this->getReadConnection()->getIfNullSql('t2_name.value', 't1_name.value')]); if (\Magento\Sitemap\Model\Source\Product\Image\IncludeImage::INCLUDE_ALL == $imageIncludePolicy) { $this->_joinAttribute($store->getId(), 'thumbnail'); $this->_select->columns(['thumbnail' => $this->getReadConnection()->getIfNullSql('t2_thumbnail.value', 't1_thumbnail.value')]); } elseif (\Magento\Sitemap\Model\Source\Product\Image\IncludeImage::INCLUDE_BASE == $imageIncludePolicy) { $this->_joinAttribute($store->getId(), 'image'); $this->_select->columns(['image' => $this->getReadConnection()->getIfNullSql('t2_image.value', 't1_image.value')]); } } $query = $adapter->query($this->_select); while ($row = $query->fetch()) { $product = $this->_prepareProduct($row, $store->getId()); $products[$product->getId()] = $product; } return $products; }
/** * Get category collection array * * @param null|string|bool|int|\Magento\Store\Model\Store $storeId * @return array|bool */ public function getCollection($storeId) { $products = array(); /* @var $store \Magento\Store\Model\Store */ $store = $this->_storeManager->getStore($storeId); if (!$store) { return false; } $urConditions = array('e.entity_id = ur.product_id', 'ur.category_id IS NULL', $this->_getWriteAdapter()->quoteInto('ur.store_id = ?', $store->getId()), $this->_getWriteAdapter()->quoteInto('ur.is_system = ?', 1)); $this->_select = $this->_getWriteAdapter()->select()->from(array('e' => $this->getMainTable()), array($this->getIdFieldName(), 'updated_at'))->joinInner(array('w' => $this->getTable('catalog_product_website')), 'e.entity_id = w.product_id', array())->joinLeft(array('ur' => $this->getTable('core_url_rewrite')), join(' AND ', $urConditions), array('url' => 'request_path'))->where('w.website_id = ?', $store->getWebsiteId()); $this->_addFilter($store->getId(), 'visibility', $this->_productVisibility->getVisibleInSiteIds(), 'in'); $this->_addFilter($store->getId(), 'status', $this->_productStatus->getVisibleStatusIds(), 'in'); // Join product images required attributes $imageIncludePolicy = $this->_sitemapData->getProductImageIncludePolicy($store->getId()); if (\Magento\Sitemap\Model\Source\Product\Image\IncludeImage::INCLUDE_NONE != $imageIncludePolicy) { $this->_joinAttribute($store->getId(), 'name'); $this->_select->columns(array('name' => $this->getReadConnection()->getIfNullSql('t2_name.value', 't1_name.value'))); if (\Magento\Sitemap\Model\Source\Product\Image\IncludeImage::INCLUDE_ALL == $imageIncludePolicy) { $this->_joinAttribute($store->getId(), 'thumbnail'); $this->_select->columns(array('thumbnail' => $this->getReadConnection()->getIfNullSql('t2_thumbnail.value', 't1_thumbnail.value'))); } elseif (\Magento\Sitemap\Model\Source\Product\Image\IncludeImage::INCLUDE_BASE == $imageIncludePolicy) { $this->_joinAttribute($store->getId(), 'image'); $this->_select->columns(array('image' => $this->getReadConnection()->getIfNullSql('t2_image.value', 't1_image.value'))); } } $query = $this->_getWriteAdapter()->query($this->_select); while ($row = $query->fetch()) { $product = $this->_prepareProduct($row, $store->getId()); $products[$product->getId()] = $product; } return $products; }
public function getProductCollection() { $brand = $this->getBrand(); $collection = $this->_productCollectionFactory->create(); $collection->addAttributeToSelect('*'); $collection->setVisibility($this->_catalogProductVisibility->getVisibleInSiteIds()); // var_dump(get_class_methods($collection)); // die; $collection->addAttributeToSelect('name'); $collection->addStoreFilter()->addAttributeToFilter('manufacturer', $brand->getAttributeId()); // var_dump(count($collection)); return $collection; }
/** * Retrieve Product Compare items collection * * @return \Magento\Catalog\Model\ResourceModel\Product\Compare\Item\Collection */ public function getItems() { if ($this->_items === null) { $this->_compareProduct->setAllowUsedFlat(false); $this->_items = $this->_itemCollectionFactory->create(); $this->_items->useProductItem(true)->setStoreId($this->_storeManager->getStore()->getId()); if ($this->httpContext->getValue(Context::CONTEXT_AUTH)) { $this->_items->setCustomerId($this->currentCustomer->getCustomerId()); } elseif ($this->_customerId) { $this->_items->setCustomerId($this->_customerId); } else { $this->_items->setVisitorId($this->_customerVisitor->getId()); } $this->_items->addAttributeToSelect($this->_catalogConfig->getProductAttributes())->loadComparableAttributes()->addMinimalPrice()->addTaxPercents()->setVisibility($this->_catalogProductVisibility->getVisibleInSiteIds()); } return $this->_items; }
/** * Calculate cache product compare collection * * @param bool $logout * @return $this */ public function calculate($logout = false) { /** @var $collection Collection */ $collection = $this->_itemCollectionFactory->create()->useProductItem(true); if (!$logout && $this->_customerSession->isLoggedIn()) { $collection->setCustomerId($this->_customerSession->getCustomerId()); } elseif ($this->_customerId) { $collection->setCustomerId($this->_customerId); } else { $collection->setVisitorId($this->_logVisitor->getId()); } /* Price data is added to consider item stock status using price index */ $collection->addPriceData()->setVisibility($this->_catalogProductVisibility->getVisibleInSiteIds()); $count = $collection->getSize(); $this->_catalogSession->setCatalogCompareItemsCount($count); return $this; }
/** * Retrieve Index Product Collection * * @return \Magento\Reports\Model\ResourceModel\Product\Index\Collection\AbstractCollection */ public function getItemsCollection() { if ($this->_collection === null) { $attributes = $this->_catalogConfig->getProductAttributes(); $this->_collection = $this->getModel()->getCollection()->addAttributeToSelect($attributes); if ($this->getCustomerId()) { $this->_collection->setCustomerId($this->getCustomerId()); } $this->_collection->excludeProductIds($this->getModel()->getExcludeProductIds())->addUrlRewrite()->setPageSize($this->getPageSize())->setCurPage(1); /* Price data is added to consider item stock status using price index */ $this->_collection->addPriceData(); $ids = $this->getProductIds(); if (empty($ids)) { $this->_collection->addIndexFilter(); } else { $this->_collection->addFilterByIds($ids); } $this->_collection->setAddedAtOrder()->setVisibility($this->_productVisibility->getVisibleInSiteIds()); } return $this->_collection; }
/** * Get random items collection with or without related children * * @param int $limit * @param bool $nonChildrenOnly * @return ImportCollection */ protected function _getItemsRandomCollection($limit, $nonChildrenOnly = false) { $collection = $this->_orderItemCollectionFactory->create()->setOrderFilter($this)->setRandomOrder(); if ($nonChildrenOnly) { $collection->filterByParent(); } $products = []; foreach ($collection as $item) { $products[] = $item->getProductId(); } $productsCollection = $this->productListFactory->create()->addIdFilter($products)->setVisibility($this->_productVisibility->getVisibleInSiteIds())->addPriceData()->setPageSize($limit)->load(); foreach ($collection as $item) { $product = $productsCollection->getItemById($item->getProductId()); if ($product) { $item->setProduct($product); } } return $collection; }
/** * Retrieve visible in site visibilities * * @return array */ public function getVisibleInSiteVisibilities() { return $this->_catalogProductVisibility->getVisibleInSiteIds(); }
/** * Retrieve allowed visibility values for current engine * * @return int[] */ public function getAllowedVisibility() { return $this->catalogProductVisibility->getVisibleInSiteIds(); }
/** * Add products to items and item options * * @return $this */ protected function _assignProducts() { \Magento\Framework\Profiler::start('WISHLIST:' . __METHOD__, array('group' => 'WISHLIST', 'method' => __METHOD__)); $productIds = array(); $this->_productIds = array_merge($this->_productIds, array_keys($productIds)); $attributes = $this->_wishlistConfig->getProductAttributes(); /** @var \Magento\Catalog\Model\Resource\Product\Collection $productCollection */ $productCollection = $this->_productCollectionFactory->create(); if ($this->_productVisible) { $productCollection->setVisibility($this->_productVisibility->getVisibleInSiteIds()); } $productCollection->addPriceData()->addTaxPercents()->addIdFilter($this->_productIds)->addAttributeToSelect($attributes)->addOptionsToResult()->addUrlRewrite(); if ($this->_productSalable) { $productCollection = $this->_adminhtmlSales->applySalableProductTypesFilter($productCollection); } $this->_eventManager->dispatch('wishlist_item_collection_products_after_load', array('product_collection' => $productCollection)); $checkInStock = $this->_productInStock && !$this->_inventoryData->isShowOutOfStock(); foreach ($this as $item) { $product = $productCollection->getItemById($item->getProductId()); if ($product) { if ($checkInStock && !$product->isInStock()) { $this->removeItemByKey($item->getId()); } else { $product->setCustomOptions(array()); $item->setProduct($product); $item->setProductName($product->getName()); $item->setName($product->getName()); $item->setPrice($product->getPrice()); } } else { $item->isDeleted(true); } } \Magento\Framework\Profiler::stop('WISHLIST:' . __METHOD__); return $this; }
/** * Add products to items and item options * * @return $this */ protected function _assignProducts() { \Magento\Framework\Profiler::start('WISHLIST:' . __METHOD__, ['group' => 'WISHLIST', 'method' => __METHOD__]); /** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection */ $productCollection = $this->_productCollectionFactory->create(); if ($this->_productVisible) { $productCollection->setVisibility($this->_productVisibility->getVisibleInSiteIds()); } $attributesToSelect = ['name', 'visibility', 'small_image', 'thumbnail', 'links_purchased_separately', 'links_title']; $productCollection->addPriceData()->addTaxPercents()->addIdFilter($this->_productIds)->addAttributeToSelect($attributesToSelect)->addOptionsToResult()->addUrlRewrite(); if ($this->_productSalable) { $productCollection = $this->_adminhtmlSales->applySalableProductTypesFilter($productCollection); } $this->_eventManager->dispatch('wishlist_item_collection_products_after_load', ['product_collection' => $productCollection]); $checkInStock = $this->_productInStock && !$this->stockConfiguration->isShowOutOfStock(); foreach ($this as $item) { $product = $productCollection->getItemById($item->getProductId()); if ($product) { if ($checkInStock && !$product->isInStock()) { $this->removeItemByKey($item->getId()); } else { $product->setCustomOptions([]); $item->setProduct($product); $item->setProductName($product->getName()); $item->setName($product->getName()); $item->setPrice($product->getPrice()); } } else { $item->isDeleted(true); } } \Magento\Framework\Profiler::stop('WISHLIST:' . __METHOD__); return $this; }