我正在尝试获取所有启用和禁用的产品,并且我正在使用此代码:
/*$categoryId = 3; // a category id that you can get from admin
$category = Mage::getModel('catalog/category')->load($categoryId);
$collection = Mage::getModel('catalog/product')
->getCollection()
->addCategoryFilter($category)
->addAttributeToSelect('*')
->addAttributeToFilter('status', array('gt' => 0))
->load();
$categoryId = 3; // a category id that you can get from admin
$category = Mage::getModel('catalog/category')->load($categoryId);*/
以上代码仅带来启用的产品。
通过评论状态过滤器,它仍然会带来相同的结果,即仅启用的产品。
/*$collection = Mage::getModel('catalog/product')
->getCollection()
->addCategoryFilter($category)
->addAttributeToSelect('*')
//->addAttributeToFilter('status', array('gt' => 0))
->load();*/
它仍然只提供已启用的产品。 但是当我评论类别检查时,它会带来所有产品:(有人可以帮忙吗?
注意:
对于那些不清楚此查询的人,让我告诉您状态已启用 = 1 和状态已禁用 = 2。
因此大于零的状态应该为我带来启用和禁用的产品,但它没有这样做。 那么有什么想法吗???
我编辑代码
$collection = Mage::getModel('catalog/category')->load(3)
->getProductCollection()
->addAttributeToSelect('*')
->addAttributeToSort('entity_id', 'ASC');
die((string) $collection->getSelect());
这会带来这个查询:
SELECT `e`.*, `cat_index`.`position` AS `cat_index_position` FROM `catalog_product_entity` AS `e` 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.category_id='3' ORDER BY `e`.`entity_id` ASC
最佳答案
我已经找到为什么会发生这种情况了。
这是因为当您想要使用 $productCollection->addCategoryFilter()
或使用 $category->getProductCollection()
查询从类别中获取产品时,请使用产品到类别连接INDEX表。当您运行重新索引时,该产品类别连接 INDEX 表仅填充已启用的产品,并且您无法以这种方式获取已禁用的产品。我不知道Magento中是否有这个错误,您可以使用原始SQL查询从特定类别中获取您的产品。
关于magento - 如何获取特定类别的所有启用和禁用产品?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11683166/