【问题标题】:change products display order through custom module in Magento通过 Magento 中的自定义模块更改产品显示顺序
【发布时间】:2014-06-05 14:39:50
【问题描述】:

首先,我是 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 文件中,我调用上述函数并显示产品,一切正常。这是.phtml文件的代码

<?php
$product_collection = $this->getProducts($sort_order);
foreach ($product_collection as $product) {
  //your code
}  
?>

现在我需要在模板中放置一个链接或一个按钮或一个下拉菜单,然后单击该链接/按钮/下拉菜单后,我想更改显示顺序。我不知道我怎样才能做到这一点。我不知道如何将该链接中的值传递给 BLOCK 或控制器,以便更改显示顺序。而且我不知道实现这一目标的最佳方法是什么。

顺便说一下,我使用的是 Magento 社区版。

【问题讨论】:

    标签: php magento magento-1.8


    【解决方案1】:

    使用-&gt;addAttributeToSort('attribute_code','sortorder') 而不是setOrder()sortorder 应该是 DESCASC

    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;
        }
    

    【讨论】:

    • 感谢您的回复。但我需要从模板中传递 ASC 或 DESC 值。我想知道我该怎么做?一个适当的解决方案或回复会更好:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多