Magento. 按位置对产品集合进行排序

6
我有产品集合。
$_products = Mage::getModel('catalog/product')->getCollection();

在管理面板中的目录->管理分类->分类产品中,我有每个产品的位置。 我如何按位置对 $_products 进行排序?

3个回答

6
如果您想按类别 $category_id 中的位置对产品进行排序,可以使用以下方法。
//Load the category model

 $category = Mage::getModel('catalog/category')->load($category_id)
             ->getProductCollection()
             ->addAttributeToSort('position', 'ASC');

$products_list = $category->getData();

您将会得到按位置排序的该类别 $category_id 的所有产品。

你好,感谢回答。不幸的是,在产品集合中 ->addAttributeToSort('position', 'ASC') 不起作用。 - user3162709
您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - Arun Krish

2
您需要为产品添加类别,以便将其作为集合的筛选条件来设置位置。
$category = Mage::getModel('catalog/category')->load($categoryId);
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->addCategoryFilter($category);
$collection->addAttributeToSort('position', 'ASC');

0

我创建了一个独立的扩展,它创建了一个名为“position”的新产品属性。在这个扩展中,我创建了一个观察者来监听类别保存事件。因此,当管理员保存类别时,在我的观察者中,我获取每个位置并为每个产品设置我的属性“position”。最后,我可以通过“position”(产品)属性对产品集合进行排序。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接