Magento using addVisibleInCatalogFilterToCollection returns nothing when it should not

In Magento, I use the following code to get the best-seller data collection:

Model Function:

public function bestSellers($limit = 12){ $storeId = Mage::app()->getStore()->getId(); $_productCollection = Mage::getResourceModel('reports/product_collection') ->addOrderedQty() ->addAttributeToSelect('id') ->setStoreId($storeId) ->addStoreFilter($storeId) ->setOrder('ordered_qty', 'desc') //best sellers on top ->setPageSize($limit); Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($_productCollection); Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($_productCollection); return $_productCollection; } 

Block output:

 <?php $products = Mage::getModel('tabs/collections')->bestSellers($limit); ?> <pre> <?php print_r($productCollection->getData()); ?> </pre> 

However, it does not return anything when the addVisibleInCatalogFilterToCollection string is used in the model function, but if I delete the addVisibleInCatalogFilterToCollection string, it returns an array of the expected best-selling product data (including those that should not be visible in the catalog).

How can I return a data array with a visibility filter that works as it should? Instead of returning nothing. Very embarrassed. Thanks in advance!

Here's getSelect:

  SELECT SUM(order_items.qty_ordered) AS `ordered_qty`, `order_items`.`name` AS `order_items_name`, `order_items`.`product_id` AS `entity_id`, `e`.`entity_type_id`, `e`.`attribute_set_id`, `e`.`type_id`, `e`.`sku`, `e`.`has_options`, `e`.`required_options`, `e`.`created_at`, `e`.`updated_at`, `cat_index`.`position` AS `cat_index_position` FROM `sales_flat_order_item` AS `order_items` INNER JOIN `sales_flat_order` AS `order` ON `order`.entity_id = order_items.order_id AND `order`.state <> 'canceled' LEFT JOIN `catalog_product_entity` AS `e` ON (e.type_id NOT IN ('grouped', 'configurable', 'bundle')) AND e.entity_id = order_items.product_id AND e.entity_type_id = 4 INNER JOIN `catalog_category_product_index` AS `cat_index` ON cat_index.product_id=e.entity_id AND cat_index.store_id='1' AND cat_index.visibility IN(2, 4) AND cat_index.category_id='2' WHERE (parent_item_id IS NULL) GROUP BY `order_items`.`product_id` HAVING (SUM(order_items.qty_ordered) > 0) ORDER BY `ordered_qty` desc 
+4
source share
3 answers

Here is my block that I use to get best sellers. It also offers simple products, which are variants of the parent custom products, for custom products (only works correctly if one simple product is assigned to a maximum of 1 custom product): http://devblog.fishol.pl/bestselling-products-in-magento/

0
source

I think this happens because addVisibleInCatalogFilterToCollection() does more than you can think of. Look at this code (app\code\core\Mage\Catalog\Model\Product\Visibility.php, line 66) :

 public function addVisibleInCatalogFilterToCollection(Mage_Eav_Model_Entity_Collection_Abstract $collection) { $collection->setVisibility($this->getVisibleInCatalogIds()); 

// $ collection-> addAttributeToFilter ('visibility', array ('in' => $ this-> getVisibleInCatalogIds ())); return $ this; }

Now take a look at $collection->setVisibility($this->getVisibleInCatalogIds()) . Go to \app\code\core\Mage\Catalog\Model\Resource\Product\Collection.php :

 public function setVisibility($visibility) { $this->_productLimitationFilters['visibility'] = $visibility; $this->_applyProductLimitations(); return $this; } /** * Apply limitation filters to collection * Method allows using one time category product index table (or product website table) * for different combinations of store_id/category_id/visibility filter states * Method supports multiple changes in one collection object for this parameters * * @return Mage_Catalog_Model_Resource_Product_Collection */ protected function _applyProductLimitations() { $this->_prepareProductLimitationFilters(); $this->_productLimitationJoinWebsite(); $this->_productLimitationJoinPrice(); $filters = $this->_productLimitationFilters; if (!isset($filters['category_id']) && !isset($filters['visibility'])) { return $this; } $conditions = array( 'cat_index.product_id=e.entity_id', $this->getConnection()->quoteInto('cat_index.store_id=?', $filters['store_id']) ); if (isset($filters['visibility']) && !isset($filters['store_table'])) { $conditions[] = $this->getConnection() ->quoteInto('cat_index.visibility IN(?)', $filters['visibility']); } $conditions[] = $this->getConnection() ->quoteInto('cat_index.category_id=?', $filters['category_id']); if (isset($filters['category_is_anchor'])) { $conditions[] = $this->getConnection() ->quoteInto('cat_index.is_parent=?', $filters['category_is_anchor']); } $joinCond = join(' AND ', $conditions); $fromPart = $this->getSelect()->getPart(Zend_Db_Select::FROM); if (isset($fromPart['cat_index'])) { $fromPart['cat_index']['joinCondition'] = $joinCond; $this->getSelect()->setPart(Zend_Db_Select::FROM, $fromPart); } else { $this->getSelect()->join( array('cat_index' => $this->getTable('catalog/category_product_index')), $joinCond, array('cat_index_position' => 'position') ); } $this->_productLimitationJoinStore(); Mage::dispatchEvent('catalog_product_collection_apply_limitations_after', array( 'collection' => $this )); return $this; } 

So, as you can see, the product must be linked to the current website and must be added to any category from the current website in order to appear in the product collection. In addition, there are some other requirements.

+2
source

Just for reference in the future.

I had a very similar problem.

Random widgets weren’t showing in one widget.

because of

 Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection($collection); 

The product must be installed for the catalog (product β†’ visibility β†’ catalog, search).

0
source

Source: https://habr.com/ru/post/1399005/


All Articles