简体   繁体   English

Magento:对产品系列进行排序

[英]Magento: Sorting a product collection

I'm creating a template to display featured products on the home page, and I'd like to control the order of the products. 我正在创建一个模板以在主页上显示特色产品,我想控制产品的顺序。

This is what I'm using at the moment to fetch a product collection based on a category: 这就是我目前使用的基于类别获取产品集合的方法:

<?php
    $_productCollection = $this->getLoadedProductCollection();
?>

No specific sorting at all. 没有具体的排序。

When I were going to sort the products, I expected this to work: 当我要对产品进行分类时,我希望这可以工作:

<?php
    $_productCollection = $this->getLoadedProductCollection()->addAttributeToSort('name', 'ASC');
?>

But there's no difference at all. 但是根本没有区别。 What am I doing wrong? 我究竟做错了什么?

Thank you in advance! 先感谢您!

use this one I have worked on the same way try it. 使用这个我用同样的方式尝试它。

$collection = Mage::getModel('catalog/product')
             ->getCollection()
             ->addAttributeToSort('name', Varien_Data_Collection::SORT_ORDER_ASC);

for descending order: 降序排列:

$collection = Mage::getModel('catalog/product')
              ->getCollection()
              ->addAttributeToSort('name', Varien_Data_Collection::SORT_ORDER_DESC);

for product with its category: 对于其类别的产品:

$collection = Mage::getModel('catalog/category')->load($categoryId)
             ->getProductCollection()
             ->addAttributeToSort('name', Varien_Data_Collection::SORT_ORDER_ASC);

Or you can find more help on magento wiki . 或者你可以在magento wiki上找到更多帮助。

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

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