/** * @return \Magento\Catalog\Model\ResourceModel\Product\Collection */ public function getProductsCollection() { /* @var $product \Magento\Catalog\Model\Product */ $product = $this->productFactory->create(); /* @var $collection \Magento\Catalog\Model\ResourceModel\Product\Collection */ $collection = $product->getCollection(); /** @var $resourceStock \Magento\CatalogInventory\Model\ResourceModel\Stock */ $resourceStock = $this->stockFactory->create(); $resourceStock->addLowStockFilter($collection, ['qty', 'notify_stock_qty', 'low_stock_date', 'use_config' => 'use_config_notify_stock_qty']); $collection->addAttributeToSelect('name', true)->addAttributeToFilter('status', ['in' => $this->productStatus->getVisibleStatusIds()])->setOrder('low_stock_date'); $this->eventManager->dispatch('rss_catalog_notify_stock_collection_select', ['collection' => $collection]); return $collection; }
/** * 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; }
/** * Regenerate search index for specific store * * @param int $storeId Store View Id * @param int|array $productIds Product Entity Id * @return \Generator * * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function rebuildStoreIndex($storeId, $productIds = null) { if ($productIds !== null) { $productIds = array_unique(array_merge($productIds, $this->getProductIdsFromParents($productIds))); } // prepare searchable attributes $staticFields = []; foreach ($this->getSearchableAttributes('static') as $attribute) { $staticFields[] = $attribute->getAttributeCode(); } $dynamicFields = ['int' => array_keys($this->getSearchableAttributes('int')), 'varchar' => array_keys($this->getSearchableAttributes('varchar')), 'text' => array_keys($this->getSearchableAttributes('text')), 'decimal' => array_keys($this->getSearchableAttributes('decimal')), 'datetime' => array_keys($this->getSearchableAttributes('datetime'))]; // status and visibility filter $visibility = $this->getSearchableAttribute('visibility'); $status = $this->getSearchableAttribute('status'); $statusIds = $this->catalogProductStatus->getVisibleStatusIds(); $allowedVisibility = $this->engine->getAllowedVisibility(); return $this->iteratorFactory->create(['storeId' => $storeId, 'productIds' => $productIds, 'staticFields' => $staticFields, 'dynamicFields' => $dynamicFields, 'visibility' => $visibility, 'allowedVisibility' => $allowedVisibility, 'status' => $status, 'statusIds' => $statusIds]); }
/** * Render RSS * * @return string */ protected function _toHtml() { $newUrl = $this->getUrl('rss/catalog/notifystock', array('_secure' => true, '_nosecret' => true)); $title = __('Low Stock Products'); /** @var $rssObj \Magento\Rss\Model\Rss */ $rssObj = $this->_rssFactory->create(); $rssObj->_addHeader(array('title' => $title, 'description' => $title, 'link' => $newUrl, 'charset' => 'UTF-8')); $globalNotifyStockQty = (double) $this->_scopeConfig->getValue(\Magento\CatalogInventory\Model\Stock\Item::XML_PATH_NOTIFY_STOCK_QTY, \Magento\Store\Model\ScopeInterface::SCOPE_STORE); /* @var $product \Magento\Catalog\Model\Product */ $product = $this->_productFactory->create(); /* @var $collection \Magento\Catalog\Model\Resource\Product\Collection */ $collection = $product->getCollection(); /** @var $resourceStock \Magento\CatalogInventory\Model\Resource\Stock */ $resourceStock = $this->_stockFactory->create(); $resourceStock->addLowStockFilter($collection, array('qty', 'notify_stock_qty', 'low_stock_date', 'use_config' => 'use_config_notify_stock_qty')); $collection->addAttributeToSelect('name', true)->addAttributeToFilter('status', array('in' => $this->_productStatus->getVisibleStatusIds()))->setOrder('low_stock_date'); $this->_eventManager->dispatch('rss_catalog_notify_stock_collection_select', array('collection' => $collection)); /* using resource iterator to load the data one by one instead of loading all at the same time. loading all data at the same time can cause the big memory allocation. */ $this->_resourceIterator->walk($collection->getSelect(), array(array($this, 'addNotifyItemXmlCallback')), array('rssObj' => $rssObj, 'product' => $product, 'globalQty' => $globalNotifyStockQty)); return $rssObj->createRssXml(); }
/** * Regenerate search index for specific store * * @param int $storeId Store View Id * @param int|array $productIds Product Entity Id * @return void * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ protected function rebuildStoreIndex($storeId, $productIds = null) { $this->cleanIndex($storeId, $productIds); // prepare searchable attributes $staticFields = []; foreach ($this->getSearchableAttributes('static') as $attribute) { $staticFields[] = $attribute->getAttributeCode(); } $dynamicFields = ['int' => array_keys($this->getSearchableAttributes('int')), 'varchar' => array_keys($this->getSearchableAttributes('varchar')), 'text' => array_keys($this->getSearchableAttributes('text')), 'decimal' => array_keys($this->getSearchableAttributes('decimal')), 'datetime' => array_keys($this->getSearchableAttributes('datetime'))]; // status and visibility filter $visibility = $this->getSearchableAttribute('visibility'); $status = $this->getSearchableAttribute('status'); $statusIds = $this->catalogProductStatus->getVisibleStatusIds(); $allowedVisibility = $this->engineProvider->get()->getAllowedVisibility(); $lastProductId = 0; while (true) { $products = $this->getSearchableProducts($storeId, $staticFields, $productIds, $lastProductId); if (!$products) { break; } $productAttributes = []; $productRelations = []; foreach ($products as $productData) { $lastProductId = $productData['entity_id']; $productAttributes[$productData['entity_id']] = $productData['entity_id']; $productChildren = $this->getProductChildIds($productData['entity_id'], $productData['type_id']); $productRelations[$productData['entity_id']] = $productChildren; if ($productChildren) { foreach ($productChildren as $productChildId) { $productAttributes[$productChildId] = $productChildId; } } } $productIndexes = []; $productAttributes = $this->getProductAttributes($storeId, $productAttributes, $dynamicFields); foreach ($products as $productData) { if (!isset($productAttributes[$productData['entity_id']])) { continue; } $productAttr = $productAttributes[$productData['entity_id']]; if (!isset($productAttr[$visibility->getId()]) || !in_array($productAttr[$visibility->getId()], $allowedVisibility)) { continue; } if (!isset($productAttr[$status->getId()]) || !in_array($productAttr[$status->getId()], $statusIds)) { continue; } $productIndex = [$productData['entity_id'] => $productAttr]; $hasChildren = false; $productChildren = $productRelations[$productData['entity_id']]; if ($productChildren) { foreach ($productChildren as $productChildId) { if (isset($productAttributes[$productChildId])) { $productChildAttr = $productAttributes[$productChildId]; if (!isset($productChildAttr[$status->getId()]) || !in_array($productChildAttr[$status->getId()], $statusIds)) { continue; } $hasChildren = true; $productIndex[$productChildId] = $productChildAttr; } } } if ($productChildren !== null && !$hasChildren) { continue; } $index = $this->prepareProductIndex($productIndex, $productData, $storeId); $productIndexes[$productData['entity_id']] = $index; } $this->saveProductIndexes($storeId, $productIndexes); } $this->fulltextResource->resetSearchResults(); }
/** * Retrieve visible statuses * * @return array */ public function getVisibleStatuses() { return $this->_catalogProductStatus->getVisibleStatusIds(); }
public function testGetVisibleStatusIds() { $this->assertEquals([0 => 1], $this->status->getVisibleStatusIds()); }
/** * @return \Magento\Framework\Api\Filter */ private function addFilterStatus() { if ($this->getStatus() === 1) { $this->searchCriteriaBuilder->addFilter(ProductInterface::STATUS, $this->productStatus->getVisibleStatusIds(), 'in'); } }