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')
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
source share