首先,我是Magento的新手,并试图在其中创建一个模块。它显示一些产品,并根据属性对它们进行特定的排序。下面是块
的代码class Your_Block_Name extends Mage_Core_Block_Template {
public function getProducts() {
$collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect("*")
->addFieldToFilter('status', '1')
->addFieldToFilter('my_attribute', array("notnull" => true))
->setOrder('my_attribute', 'whatever is the order');
return $collection;
}
}
在.phtml文件中,我调用上述函数并显示产品,一切正常。下面是. php文件
的代码<?php
$product_collection = $this->getProducts($sort_order);
foreach ($product_collection as $product) {
//your code
}
?>
现在我需要在模板中放置一个链接或按钮或下拉菜单,然后单击该链接/按钮/下拉菜单后,我想更改显示的顺序。我不知道怎样才能做到这一点。我不知道如何将该链接的值传递给BLOCK或控制器,以便它改变显示的顺序。我不知道实现这一目标的最佳方法是什么。
顺便说一下,我使用Magento社区版本。
用->addAttributeToSort('attribute_code','sortorder')
代替setOrder()
。sortorder
应为DESC
或ASC
和
public function getProducts($sortOrder='DESC') {
$collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect("*")
->addFieldToFilter('status', '1')
->addFieldToFilter('my_attribute', array("notnull" => true))
->addAttributeToSort('attribute_code',$sortOrder);
return $collection;
}