我可以';t按类别id对产品集合进行排序


I can't sort product collection by category id

我有几个类别:男生、女生、男生、女生和offer。

每种产品都分为男性、女性、男性或女性类别。

一些产品被分配到"优惠"类别。

我需要检索每个"优惠"产品,但按其他类别排序,我的意思是:

优惠->伙计们的产品->女士用品->男士用品->女性产品

$collection = Mage::getResourceModel('catalog/product_collection')
        ->addAttributeToSort('category_ids', 'ASC')
        ->addAttributeToSelect(array('name', 'sku', 'status', 'visibility', 'is_saleable'));
    foreach ($_productCollection as $_product) {
        echo "<!-- "  . $_product->getName() . '-->';
    }

现在我正在让产品按类别"offers"显示,但这些产品是按名称属性排序的,我需要这些产品在名称之前按类别排序。

我怎样才能做到这一点?

下面的查询将返回所有产品,并将它们连接到类别产品关系表中。它还根据category_id和在类别中的位置对记录进行排序。

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

唯一可能发生的事情是,你会遇到重复的产品,因为产品可能属于女士和女士产品类别。尽管这可以通过从查询中删除x.catetgory_id并添加DISTINCT来过滤重复项来简单地防止。

下面只是该系列外观的快速模型。您仍然需要对此进行测试。希望这对你来说是一个好的方向。

$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');