使用addVisibleInCatalogFilterToCollection的Magento在不应返回任何内容时不返回任何内容
在Magento中,我使用以下代码获取畅销书数据收集: 模型函数:使用addVisibleInCatalogFilterToCollection的Magento在不应返回任何内容时不返回任何内容,magento,collections,report,visibility,product,Magento,Collections,Report,Visibility,Product,在Magento中,我使用以下代码获取畅销书数据收集: 模型函数: public function bestSellers($limit = 12){ $storeId = Mage::app()->getStore()->getId(); $_productCollection = Mage::getResourceModel('reports/product_collection') ->addOrderedQty()
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:
public function addVisibleInCatalogFilterToCollection(Mage_Eav_Model_Entity_Collection_Abstract $collection)
{
$collection->setVisibility($this->getVisibleInCatalogIds());
我认为这是因为addVisibleInCatalogFilterToCollection所做的事情超出了您的想象。让我们看看它的代码app\code\core\Mage\Catalog\Model\Product\Visibility.php,第66行: //$collection->addAttributeToFilter'visibility',array'in'=>$this->getVisibleInCatalogIds; 退还$this; } 现在,让我们仔细看看$collection->setVisibility$this->getVisibleInCatalogIds。让我们转到\app\code\core\Mage\Catalog\Model\Resource\Product\Collection.php:
所以,正如您所看到的,产品应该被分配到当前网站,并且应该被添加到当前网站的某个类别中,以显示在产品集合中。此外,您可以看到,这里还有一些其他要求。这是我用来获取畅销书的区块。它还提供简单产品,这些产品是母公司可配置产品的变体,只有当一个简单产品被分配给最多1个可配置产品时,可配置产品才能正常工作:仅供将来参考 我也有过类似的问题 其中一个小部件没有显示随机产品 因为
必须为目录产品->可见性->目录、搜索设置产品可见。此代码适用于我。。。尝试禁用第三方模块,这是哪个magento版本?使用magento CE v1.6.2.0并启用平面产品目录。和你的一样?我相信我在安装时禁用了平面目录,我在同一个CE版本上尝试过它-当你禁用它时会有什么不同吗?echo$productCollection->getSelect;的输出是什么?它返回一个Varien_Db_Select对象,其中包含许多与sales flat order表相关的随机内容。但不是我需要的数据。当我禁用平面目录时,会出现以下错误:在第816行的app/code/core/Mage/Eav/Model/Entity/Abstract.php中的非对象上调用成员函数getBackend。现在试试我实际说的echo。刷新索引后,我得到了产品,呜呼!所有产品都被分配到一个类别和一个网站。但是,当使用可见性过滤器时,它不会返回任何可配置的产品。visiblity筛选器会删除已删除的产品,但也会删除可配置的产品,因为单个畅销选项显然不可见。关于如何将可配置产品作为父产品返回,而不从系统返回已删除的产品,同时使用某些说明的可见性过滤器,您有什么想法吗?您将无法共享断开链接的代码,是吗?我的代码不是从可配置程序返回简单产品。
public function addVisibleInCatalogFilterToCollection(Mage_Eav_Model_Entity_Collection_Abstract $collection)
{
$collection->setVisibility($this->getVisibleInCatalogIds());
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;
}
Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection($collection);