Magento 使用 addVisibleInCatalogFilterToCollection 不返回任何内容

标签 magento collections report visibility product

在 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; 

}

block 输出:

<?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

最佳答案

我认为发生这种情况是因为 addVisibleInCatalogFilterToCollection() 所做的事情超出了您的想象。让我们看一下它的代码(app\code\core\Mage\Catalog\Model\Product\Visibility.php,第66行):

public function addVisibleInCatalogFilterToCollection(Mage_Eav_Model_Entity_Collection_Abstract $collection)
{
    $collection->setVisibility($this->getVisibleInCatalogIds());
//$collection->addAttributeToFilter('visibility', array('in'=>$this->getVisibleInCatalogIds())); 返回$这个; }

现在,让我们仔细看看 $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;
}

因此,如您所见,产品应分配到当前网站,并应添加到当前网站的某个类别中以显示在产品集合中。此外,如您所见,这里还有一些其他要求。

关于Magento 使用 addVisibleInCatalogFilterToCollection 不返回任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9499365/

相关文章:

mysql - Magento 目录搜索查询约束错误

php - 如何在 Magento2 的客户表中添加自定义列?

php - Magento - Mage 注册表项 "_resource_helper/core"已存在

hadoop - 我可以在电子商务(Magento)中使用大数据吗?

python - 在 Python 中实现可观察集合的推荐方法?

java - Collections.emptyList() 与新实例

java - 用于从两个格式化源创建页面的适当报告合并技术

python - 奇怪的 .txt 报告到 Pandas 数据框中

java - java.util.TreeMap 的子类在调用 put(key, value) 方法时给出 NullPointerException

widget - 如何使用 float_time 小部件在 Qweb Reports 中打印时间字段(声明为 float )?