如何从类别中获取所有产品,包括Magento中的子类别? [英] How to get all product from a category including its subcategories in Magento?

查看:39
本文介绍了如何从类别中获取所有产品,包括Magento中的子类别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种从一个类别(包括其子类别)中检索所有产品并向我返回产品集合的方法.

I'm looking for a way to retrieve all products from a category includings its subcategories and return me a Product Collection.

我知道我可以遍历类别以获取产品ID并将其加载到视图中,但是我希望获得一个产品集合,因为目前在大多数类别/视图中都已完成.

I know I can iterate over categories to get ids of product and load them in the view, but I would have liked to get a product collection as it is done currently in most categories/views.

有什么想法吗?

推荐答案

我已经通过在产品收集模型中实现addCategoriesFilter解决了此问题,这是补丁.修改后的代码将被复制到local池中,以允许更新到较新的版本.

I've solved this problem by implementing addCategoriesFilter in product collection model, here is the patch. Modified code to be copied to the local pool to allow updates to a newer version.

@@ -103,6 +103,7 @@
      * Allowed filters
      *  store_id                int;
      *  category_id             int;
+     *  category_ids            array;
      *  category_is_anchor      int;
      *  visibility              array|int;
      *  website_ids             array|int;
@@ -567,6 +568,21 @@
     }

     /**
+     * Specify categories filter for product collection
+     *
+     * @param array $categories
+     * @return Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection
+     */
+    public function addCategoriesFilter(array $categories)
+    {
+        $this->_productLimitationFilters['category_ids'] = $categories;
+
+        ($this->getStoreId() == 0)? $this->_applyZeroStoreProductLimitations() : $this->_applyProductLimitations();
+
+        return $this;
+    }
+
+    /**
      * Join minimal price attribute to result
      *
      * @return Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection
@@ -1592,7 +1608,7 @@
         $this->_productLimitationJoinPrice();
         $filters = $this->_productLimitationFilters;

-        if (!isset($filters['category_id']) && !isset($filters['visibility'])) {
+        if (!isset($filters['category_id']) && !isset($filters['category_ids']) && !isset($filters['visibility'])) {
             return $this;
         }

@@ -1604,11 +1620,16 @@
             $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'])) {
+
+        if (!isset($filters['category_ids'])) {
             $conditions[] = $this->getConnection()
-                ->quoteInto('cat_index.is_parent=?', $filters['category_is_anchor']);
+                ->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);

用法:

$category = $layer->getCurrentCategory();
$categories = $category->getAllChildren(true);

$collection = Mage::getResourceModel('catalog/product_collection');
$collection->addCategoriesFilter($categories);

这篇关于如何从类别中获取所有产品,包括Magento中的子类别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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