Magento过滤产品系列由多个类别 [英] Magento filter product collection by multiple categories

查看:129
本文介绍了Magento过滤产品系列由多个类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有多种类别过滤产品集合的简单方法吗?要获取列出的任何类别中的所有项目? addCategoryFilter 似乎不允许数组。



是获取每个类别的集合的唯一方法

 

code> addAttributeToFilter('category_ids',array('finset'=> array('1','2')))

或类似的,但是这是不可能的,因为1.4。



注意:我使用1.6,任何使用,我使用这样的:

  $ product = Mage :: getModel('catalog / product'); 
$ _productCollection = $ product-> getCollection()
- > addAttributeToSelect('*')
- > addAttributeToFilter('status',1)
- > addStoreFilter();


解决方案

Magento现在的工作方式是获取商店,并且在商店上,你可以从storecollection获取类别,如$ oStoreCollection-> addCategoryFilter(array('1','2'));



通过可能会帮助您的解决方案,位于此处:



http://www.magentocommerce.com/boards/&/viewthread/201114/#t329230



它们使用的代码如下所示:
覆盖Mage / Catalog / Model / Resource / Eav / Mysql4 / Product / Collection,并添加以下方法:

  public function addCategoriesFilter($ categories)
{
$ this-> _productLimitationFilters ['category_ids'] = $ categories;

if($ this-> getStoreId()== Mage_Core_Model_App :: ADMIN_STORE_ID){
$ this-> _applyZeroStoreProductLimitations();
} else {
$ this-> _applyProductLimitations();
}

return $ this;
}

protected function _applyProductLimitations()
{
$ this-> _prepareProductLimitationFilters();
$ this-> _productLimitationJoinWebsite();
$ this-> _productLimitationJoinPrice();
$ filters = $ this-> _productLimitationFilters;

//添加:支持过滤多个类别。
if(!isset($ filters ['category_id'])&&!isset($ filters ['category_ids'])&&!isset($ filters ['visibility'])){
return $ this;
}

$条件=数组(
'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']);
}

//添加:支持过滤多个类别。
if(!isset($ filters ['category_ids'])){
$ 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']);
}
} else {
$ conditions [] = $ this-> getConnection() - " quoteInto('cat_index.category_id IN('。implode(',',$ filters ['category_ids'])')',);
}

$ 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 category_product_index')),
$ joinCond,
array('cat_index_position'=>'position')
);
}

$ this-> _productLimitationJoinStore();

Mage :: dispatchEvent('catalog_product_collection_apply_limitations_after',array(
'collection'=> $ this
));

return $ this;
}

protected function _applyZeroStoreProductLimitations()
{
$ filters = $ this-> _productLimitationFilters;

//添加:supprot用于过滤多个类别。
$ categoryCondition = null;
if(!isset($ filters ['category_ids'])){
$ categoryCondition = $ this-> getConnection() - > quoteInto('cat_pro.category_id =?',$ filters [ 'category_id']);
} else {
$ categoryCondition = $ this-> getConnection() - " quoteInto('cat_pro.category_id IN('。implode(',',$ filters ['category_ids'])。 ')',);
}

$ conditions = array(
'cat_pro.product_id = e.entity_id',
$ categoryCondition
);
$ joinCond = join('AND',$ conditions);

$ fromPart = $ this-> getSelect() - > getPart(Zend_Db_Select :: FROM);
if(isset($ fromPart ['cat_pro'])){
$ fromPart ['cat_pro'] ['joinCondition'] = $ joinCond;
$ this-> getSelect() - > setPart(Zend_Db_Select :: FROM,$ fromPart);
}
else {
$ this-> getSelect() - > join(
array('cat_pro'=> $ this-> getTable category_product')),
$ joinCond,
array('cat_index_position'=>'position')
);
}

return $ this;
}

然后这样调用:

  $ collection = Mage :: getModel('catalog / product') - > getCollection()
- > addAttributeToSelect('*')
- > distinct(true)//这是您需要添加的内容
- > addCategoriesFilter($ category-> getAllChildren(true)); //确保您不要忘记在这里检索您的类别。

HTH


Is there an easy way to filter a product collection by multiple categories? To get all items in any of the listed categories? addCategoryFilter doesn't seem to allow an array.

Is the only way to get the collections for each category of interest separately then merge them?

I understand it used to be possible with something like

addAttributeToFilter('category_ids',array('finset'=>array('1','2')))

or similar, but that this is no longer possible since 1.4.

Note: I am using 1.6, and in case it's of any use, I'm using something like this:

$product = Mage::getModel('catalog/product');
$_productCollection = $product->getCollection()
  ->addAttributeToSelect('*')
  ->addAttributeToFilter('status',1)
  ->addStoreFilter();

解决方案

The way Magento works now, is to get the Store, and on the store, you can get the categories from the storecollection like $oStoreCollection->addCategoryFilter(array('1','2'));

I came across a solution that might help you, found here at:

http://www.magentocommerce.com/boards/&/viewthread/201114/#t329230

The code they use, looks like this: Override Mage/Catalog/Model/Resource/Eav/Mysql4/Product/Collection, and add the following methods:

public function addCategoriesFilter($categories)
    {
        $this->_productLimitationFilters['category_ids'] = $categories;

        if ($this->getStoreId() == Mage_Core_Model_App::ADMIN_STORE_ID) {
            $this->_applyZeroStoreProductLimitations();
        } else {
            $this->_applyProductLimitations();
        }

        return $this;
    }

    protected function _applyProductLimitations()
    {
        $this->_prepareProductLimitationFilters();
        $this->_productLimitationJoinWebsite();
        $this->_productLimitationJoinPrice();
        $filters = $this->_productLimitationFilters;

        // Addition: support for filtering multiple categories.
        if (!isset($filters['category_id']) && !isset($filters['category_ids']) && !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']);
        }

        // Addition: support for filtering multiple categories.
        if (!isset($filters['category_ids'])) {
             $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']);
            }
        } else {
            $conditions[] = $this->getConnection()->quoteInto('cat_index.category_id IN(' . implode(',', $filters['category_ids']) . ')', "");
        }

        $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;
    }

    protected function _applyZeroStoreProductLimitations()
    {
        $filters = $this->_productLimitationFilters;

        // Addition: supprot for filtering multiple categories.
        $categoryCondition = null;
        if (!isset($filters['category_ids'])) {
            $categoryCondition = $this->getConnection()->quoteInto('cat_pro.category_id=?', $filters['category_id']);
        } else {
            $categoryCondition = $this->getConnection()->quoteInto('cat_pro.category_id IN(' . implode(',', $filters['category_ids']) . ')', "");
        }

        $conditions = array(
            'cat_pro.product_id=e.entity_id',
            $categoryCondition
        );
        $joinCond = join(' AND ', $conditions);

        $fromPart = $this->getSelect()->getPart(Zend_Db_Select::FROM);
        if (isset($fromPart['cat_pro'])) {
            $fromPart['cat_pro']['joinCondition'] = $joinCond;
            $this->getSelect()->setPart(Zend_Db_Select::FROM, $fromPart);
        }
        else {
            $this->getSelect()->join(
                array('cat_pro' => $this->getTable('catalog/category_product')),
                $joinCond,
                array('cat_index_position' => 'position')
            );
        }

        return $this;
    }

It then gets called like this:

$collection = Mage::getModel('catalog/product')->getCollection()
                        ->addAttributeToSelect('*')
                        ->distinct(true) // THIS IS WHAT YOU NEED TO ADD
                        ->addCategoriesFilter($category->getAllChildren(true)); // Make sure you don't forget to retrieve your category here.

HTH

这篇关于Magento过滤产品系列由多个类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆