如何从类别中获取所有产品,包括Magento中的子类别?

如何解决如何从类别中获取所有产品,包括Magento中的子类别?

| 我正在寻找一种方法来从一个类别(包括其子类别)中检索所有产品,并向我返回产品集合。 我知道我可以遍历类别以获取产品ID并将其加载到视图中,但是我希望获得一个产品集合,因为目前在大多数类别/视图中都已完成。 有任何想法吗?     

解决方法

        我已经通过在产品集合模型中实现addCategoriesFilter解决了这个问题,这是补丁。修改后的代码将被复制到“ 0”池中,以允许更新到较新的版本。
@@ -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);
    ,        您必须在后端将类别设置为Anchor = Yes。这样,您的收藏将剔除他的子类别中的所有产品。     ,        如果要在商店的根目录下显示分配给该目录的所有产品,可以执行以下操作(或将根目录替换为所需的目录):
$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));
希望这可以帮助。     ,        与Matt的答案相似,但没有出现“ 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\',\'left\')
    ->addAttributeToFilter(\'category_id\',array(\'in\' => $allCatIds));

echo $collection->count();
    ,        抱歉,答案不完整,但可行的方法是查看父级的类别路径(
1/2/3
),并使用一个查询(在类别集合上?)来获取该类别的所有后代(path6ѭ之类的路径)。然后,您可以使用它来过滤产品集合。也许其他人可以充实那些细节并回答得更好:)     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 <select id="xxx"> SELECT di.id, di.name, di.work_type, di.updated... <where> <if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 <property name="dynamic.classpath" value="tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-