2012-02-01 156 views
1

我在写扩展名的产品列表,但是当我编写集合时,它添加了额外的过滤器,我不需要的类别。Magento Collection删除类别过滤器

这里是我的代码:

$collection = Mage::getResourceModel('catalog/product_collection'); 
$attributes = Mage::getSingleton('catalog/config')->getProductAttributes(); 
$collection->addAttributeToSelect($attributes) 
      ->addMinimalPrice() 
      ->addFinalPrice() 
      ->addTaxPercents() 
      ->addStoreFilter(); 

$collection->addIdFilter($result); 
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection); 
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection); 
$collection->printlogquery(true); 

你可以看到我没加任何类别过滤器检查,但是当我打印我得到的查询这样的:

SELECT `e`.*, `price_index`.`price`, `price_index`.`tax_class_id`, 
      `price_index`.`final_price`, IF(`price_index`.`tier_price`, 
      LEAST(`price_index`.`min_price`, `price_index`.`tier_price`), 
      `price_index`.`min_price`) AS `minimal_price`, 
      `price_index`.`min_price`, `price_index`.`max_price`, 
      `price_index`.`tier_price`, 
      `cat_index`.`position` AS `cat_index_position` 
     FROM `mage_catalog_product_entity` AS `e` 
INNER JOIN `mage_catalog_product_index_price` AS `price_index` 
     ON price_index.entity_id = e.entity_id 
     AND price_index.website_id = '1' 
     AND price_index.customer_group_id = 0 
INNER JOIN `mage_catalog_category_product_index` AS `cat_index` 
     ON cat_index.product_id=e.entity_id 
     AND cat_index.store_id='1' 
     AND cat_index.visibility IN(2, 4) 
     AND cat_index.category_id='3' 
    WHERE (e.entity_id in ('724', '729', '733', '737', '741', '745', '749', '755', 
          '759', '766', '770', '775', '780', '785', '920', '921', 
          '923', '927', '957', '958', '959', '960', '961', '962', 
          '963', '964', '965', '966', '967', '1146', '1147', '1185', 
          '1186', '1187', '1188', '1189', '1190', '1191', '1192', 
          '1193', '1194', '1195', '1196', '1274', '1275', '1276', 
          '1277', '1278', '1279', '1280', '1281', '1282', '1283', 
          '1284', '1285', '1286', '1287', '1288', '1289', '1290', 
          '1291', '1292', '1293', '1294', '1295', '1310', '1311', 
          '1312', '1313', '1314', '1315')) 

这里可以看到,在查询它正在打印我不需要的cat_index.category_id='3'。我需要知道如何删除此类别过滤器?当您使用

Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection); 

相反

+0

这类别过滤器是关系到其类别商店。 – ShaunOReilly 2012-02-01 11:37:05

+0

这不是我所说的最小工作范例(MWE)。 – 2012-02-01 11:38:05

+0

@ShaunOReilly所以我如何删除该类别的过滤器? – 2012-02-01 11:42:18

回答

3

类别过滤器被添加,删除线,并将其替换为:

$collection->addAttributeToFilter('visibility', array('in' => array(Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG, Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH))); 
+0

感谢它工作正常:) – 2012-02-03 07:05:52