问题描述
我有几个类别:男士、女士、男士、女士和优惠.
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 assigned to "offers" category.
我需要检索每个优惠"产品但按其他类别排序,我的意思是:
I need to retrieve every "offers" products but sort by the others categories, I mean:
优惠-> 男士产品-> 女士产品-> 男士产品-> 女性产品
OFFERS -> guys products -> ladies products -> men products -> women products
$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() . '-->';
}
现在我让产品按优惠"类别显示,但这些产品是按名称属性排序的,我需要在名称之前按类别排序这些产品.
Right now I am getting the products to display by the category "offers", but this products cames ordered by name attribute and I need those products sorted by category before name.
我怎样才能做到这一点?
How can I achieve this?
推荐答案
以下查询将返回所有产品并将它们与类别-产品关系表连接起来.它还根据 category_id 和类别内的位置对记录进行排序.
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;
唯一可能发生的事情是您遇到重复的产品,因为产品可能属于女士和女士产品类别.虽然这可以通过从查询中删除 x.catetgory_id
并添加一个 DISTINCT
来过滤重复项来简单地防止.
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');
这篇关于我无法按类别 ID 对产品系列进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!