简体   繁体   English

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

[英]Magento. Sort products collection by position

I have products collection. 我有产品收藏。

$_products = Mage::getModel('catalog/product')->getCollection();

In admin panel in Catalog->Manage Categories->Category products i have position for each product. 在目录->管理类别->类别产品的管理面板中,我有每种产品的位置。 How i can sort $_products by position ? 我如何按位置对$ _products进行排序?

If you want to sort products by position in category $category_id. 如果要按类别$ category_id中的位置对产品进行排序。 you can use the following 您可以使用以下内容

//Load the category model

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

$products_list = $category->getData();

you will get all products sorted by position in that category $category_id 您将获得按类别$ category_id中的位置排序的所有产品

you need to add category, where you set positions for products, as filter for collection 您需要添加类别,在其中设置产品的位置,作为收集的过滤器

$category = Mage::getModel('catalog/category')->load($categoryId);
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->addCategoryFilter($category);
$collection->addAttributeToSort('position', 'ASC');

I have created separated extension which create new products attribute with name 'position'. 我创建了单独的扩展程序,该扩展程序创建了名称为“ position”的新产品属性。 In this extension i have create observer which listening category save event. 在此扩展中,我创建了一个观察器,该监听器保存了侦听类别。 So when admin save category in my observer i get each position and set my attribute 'position' for each product. 因此,当管理员在观察者中保存类别时,我会获得每个职位并为每个产品设置属性“职位”。 And at at last i can sort products collection by 'position'(product) attribute. 最后,我可以按“位置”(product)属性对产品集合进行排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM