问题描述
我有几类:男士,女士,男士,女士和特价商品.
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.
我该如何实现?
推荐答案
以下查询将返回所有产品,并将它们加入到category-product关系表中.还将对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对产品集合进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!