I can't sort product collection by category id

后端 未结 1 745
别跟我提以往
别跟我提以往 2020-12-21 23:26

I have a few categories: guys, ladies, men, women and offers.

Every product is assigned to guys, ladies, men OR women categories.

And some products are assig

相关标签:
1条回答
  • 2020-12-22 00:03

    The following query would return all products and join them against the category-product relation table. It also sorts the records on category_id and position within the category.

    SELECT `e`.* 
    FROM `catalog_product_entity` AS `e` 
    LEFT JOIN catalog_category_product ON(entity_id=product_id)
    ORDER BY category_id AND position;
    

    The only thing that can happen is that you run into duplicate products, since products might be in the ladies and woman products category. Though this can simply be prevented by removing the x.catetgory_id from the query and add a DISTINCT to filter duplicates.

    The underneath is just a quick mockup of how the collection should look like. You still need to test this. Hope this is a guidance in the good direction for you.

    $collection = Mage::getResourceModel('catalog/product_collection')
        ->addAttributeToSelect('*')
        ->joinTable('catalog/category_product', 'entity_id=product_id', array('product_id' => 'product_id'), null, 'left')
        ->addAtributeToSort('category_id');
    
    0 讨论(0)
提交回复
热议问题