使用addVisibleInCatalogFilterToCollection的Magento什么时候不返回

在Magento中,我使用以下代码来获取畅销书数据集:

型号功能:

  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; 

}

块输出:

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

但是,当在模型函数中使用addVisibleInCatalogFilterToCollection行时,它不会返回任何内容,但是如果我删除了addVisibleInCatalogFilterToCollection行,那么它将返回一个预期的畅销产品数据数组(包括那些在目录中不可见的数据).

如何通过可见性过滤器按原样返回我的数据数组?而不是一无所获.非常困惑.提前致谢!

这是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

最佳答案 这是我用来获得畅销书的Block.它还提供简单的产品,这些产品是父母可配置产品的变体,可配置产品(仅当一个简单产品分配给最多1个可配置产品时才能正常工作):
http://devblog.fishol.pl/bestselling-products-in-magento/

点赞