在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
最佳答案
这是我用来获得畅销书的街区。它还将作为父母可配置产品的变体的简单产品转换为可配置产品(仅当将一个简单产品分配给最多1个可配置产品时,该产品才能正常工作):http://devblog.fishol.pl/bestselling-products-in-magento/