2014-09-03 79 views
1

首先,向大家问好,并为我可怜的英语道歉!Magento - 平板产品/类别开启时如何使用多个类别过滤产品集合

我要筛选使用多个类别的产品集合,这是我的代码:

$collection = Mage::getModel('catalog/product') 
         ->getCollection() 
         ->addAttributeToSelect('*') 
         ->addMinimalPrice() 
         ->addFinalPrice() 
         ->addTaxPercents() 
         ->addTierPriceData() 
         ->addUrlRewrite() 
         ->setStoreId($this->_storeId) 
         ->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left') 
         ->addAttributeToFilter('category_id', $categoryIds); 

$ categoryIds是返回数组( '在'=>阵列(1,2,3,4,5, 6,7,8)

它工作得很好,但是当我打开使用平面目录类别/使用平面目录产品实在不行。

请帮帮我。

------编辑----

我通过echo $ collection-> getSelect()打印sql。 SQL没有“where”条件?

SELECT 1 AS `status`, `e`.`entity_id`, `e`.`type_id`, `e`.`attribute_set_id`, 
`e`.`name`, `e`.`short_description`, `e`.`price`, `e`.`special_price`, 
`e`.`special_from_date`, `e`.`special_to_date`, `e`.`small_image`, `e`.`thumbnail`, 
`e`.`news_from_date`, `e`.`news_to_date`, `e`.`url_key`, `e`.`required_options`, 
`e`.`image_label`, `e`.`small_image_label`, `e`.`thumbnail_label`, `e`.`msrp_enabled`, 
`e`.`msrp_display_actual_price_type`, `e`.`msrp`, `e`.`tax_class_id`, 
`e`.`price_type`, `e`.`weight_type`, `e`.`price_view`, `e`.`shipment_type`, 
`e`.`links_purchased_separately`, `e`.`links_exist`, `e`.`groupdeal_status`, 
`e`.`groupdeal_datetime_from`, `e`.`groupdeal_datetime_to`, `e`.`entity_id`, 
`e`.`attribute_set_id`, `e`.`type_id`, `e`.`cost`, `e`.`created_at`, 
`e`.`gift_message_available`, `e`.`has_options`, `e`.`image_label`, 
`e`.`is_recurring`, `e`.`links_exist`, `e`.`links_purchased_separately`, 
`e`.`links_title`, `e`.`msrp`, `e`.`msrp_display_actual_price_type`, 
`e`.`msrp_enabled`, `e`.`name`, `e`.`news_from_date`, `e`.`news_to_date`, `e`.`price`, 
`e`.`price_type`, `e`.`price_view`, `e`.`recurring_profile`, `e`.`required_options`, 
`e`.`shipment_type`, `e`.`short_description`, `e`.`sku`, `e`.`sku_type`, 
`e`.`small_image`, `e`.`small_image_label`, `e`.`special_from_date`, 
`e`.`special_price`, `e`.`special_to_date`, `e`.`tax_class_id`, `e`.`thumbnail`, 
`e`.`thumbnail_label`, `e`.`updated_at`, `e`.`url_key`, `e`.`url_path`, 
`e`.`visibility`, `e`.`weight`, `e`.`weight_type`, `e`.`groupdeal_datetime_from`, 
`e`.`groupdeal_datetime_to`, `e`.`groupdeal_status`, `price_index`.`price`, 
`price_index`.`tax_class_id`, `price_index`.`final_price`, IF(price_index.tier_price 
IS NOT NULL, 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`, `at_category_id`.`category_id` 
FROM `catalog_product_flat_1` AS `e` INNER JOIN `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 LEFT JOIN `catalog_category_product` AS 
`at_category_id` ON (at_category_id.`product_id`=e.entity_id) 
+0

您是否成功重新索引数据? – 2014-09-03 10:13:08

+0

是的,我did.I重新索引数据后开启使用平面目录类别/使用平面目录产品。 – 2014-09-03 10:20:52

+0

您的catalog_product_flat_1的内容是什么?它填写正确吗? – 2014-09-03 10:30:23

回答

1

我有类似的问题,几乎相同的实现,解决方法是将其中过滤器添加到您的收藏这样的:

 
$collection->getSelect()->where('at_category_id.category_id IN (?)', $arrayCatIds); 

,并删除当前

->addAttributeToFilter('category_id', $categoryIds);
和你做。