繁体   English   中英

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

[英]Magento. Sort products collection by position

我有产品收藏。

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

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

如果要按类别$ category_id中的位置对产品进行排序。 您可以使用以下内容

//Load the category model

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

$products_list = $category->getData();

您将获得按类别$ category_id中的位置排序的所有产品

您需要添加类别,在其中设置产品的位置,作为收集的过滤器

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

我创建了单独的扩展程序,该扩展程序创建了名称为“ position”的新产品属性。 在此扩展中,我创建了一个观察器,该监听器保存了侦听类别。 因此,当管理员在观察者中保存类别时,我会获得每个职位并为每个产品设置属性“职位”。 最后,我可以按“位置”(product)属性对产品集合进行排序。

暂无
暂无

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

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