在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
答案 0 :(得分:2)
我认为这是因为addVisibleInCatalogFilterToCollection()
比你想象的更多。我们来看看它的代码(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-&gt; addAttributeToFilter('visibility',array('in'=&gt; $ this-&gt; getVisibleInCatalogIds())); 返回$ this; }
现在,让我们仔细看看$collection->setVisibility($this->getVisibleInCatalogIds())
。我们去\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;
}
因此,正如您所看到的,产品应分配到当前网站,并应从当前网站添加到某个类别,以显示在产品集合中。此外,您还可以看到其他一些要求。
答案 1 :(得分:0)
这是我用来获得畅销书的Block。它还提供简单的产品,这些产品是父母可配置产品的变体,可配置产品(仅当一个简单产品被分配给最多1个可配置产品时才能正常工作):http://devblog.fishol.pl/bestselling-products-in-magento/
答案 2 :(得分:0)
仅供将来参考。
我遇到了类似的问题。
其中一个小部件没有显示随机产品。
因为
Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection($collection);
必须为目录设置产品(产品 - &gt;可见性 - &gt;目录,搜索)。