我正在寻找一种方法来检索某个类别的所有产品,包括其子类别,并返回一个产品系列。
我知道我可以迭代类别以获取产品的ID并在视图中加载它们,但我希望获得产品集合,因为它目前在大多数类别/视图中完成。
有什么想法吗?
答案 0 :(得分:6)
我通过在产品集合模型中实现addCategoriesFilter解决了这个问题,这里是补丁。修改后的代码将复制到local
池,以允许更新到更新版本。
@@ -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);
答案 1 :(得分:3)
您必须在de后端将该类别设置为Anchor = Yes。这样你的收藏品就会将所有产品都纳入他的子类别。
答案 2 :(得分:3)
如果您想要显示分配到商店根类别下面的类别的所有商品,您可以执行以下操作(或将根类别替换为您想要的类别):
$root_category = Mage::getModel('catalog/category')->load(Mage::app()->getStore()->getRootCategoryId());
$all_cat_ids = explode(",", $root_category->getAllChildren());
$collection->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id=entity_id', null, 'left');
$collection->addAttributeToFilter('category_id', array('in' => $all_cat_ids));
希望这有帮助。
答案 3 :(得分:3)
类似于马特的答案,但没有“XXX已存在”错误。
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require_once 'app/Mage.php';
umask(0);
$app = Mage::app('admin');
$rootCategory = Mage::getModel('catalog/category')->load(17);
$collection = Mage::getResourceModel('catalog/product_collection');
$allCatIds = explode(",", $rootCategory->getAllChildren());
$collection
->getSelect()
->group('entity_id');
$collection
->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id=entity_id', null, 'left')
->addAttributeToFilter('category_id', array('in' => $allCatIds));
echo $collection->count();
答案 4 :(得分:2)
对于不完整的答案感到抱歉,但可能有效的方法是查看父级(1/2/3
)的类别路径并使用查询(在类别集合上?)来抓取该类别的所有后代(路径如1/2/3%
)。然后,您可以使用它来过滤产品集合。也许其他人可以充实这些细节并更好地回答:)