I have following module for courses added on my magento store
class Abhi_Courses_Block_Course_List extends Mage_Core_Block_Template
{
public function __construct()
{
parent::__construct();
$courses = Mage::getResourceModel('abhi_courses/course_collection')
->setStoreId(Mage::app()->getStore()->getId())
->addAttributeToSelect('*')
->addAttributeToFilter('status', 1);
$courses->setOrder('display_order', 'asc');
$this->setCourses($courses);
}
and following code to get courses list on list template.
$_courses = $this->getCourses();
I have two websites setup, one for UK and another for US and there are two different display order fields for both websites
display_order_us
display_order_uk
I am able to get the current website store on list template but not able to sort the courses based on particular field like if user is on UK site courses should be sorted by display_order_us field value.
I tried following but its not working ( passing field as parameter )
$_courses = $this->getCourses('display_order');
and in controller
public $param;
public function __construct($sort_order='')
{
parent::__construct();
$courses = Mage::getResourceModel('abhi_courses/course_collection')
->setStoreId(Mage::app()->getStore()->getId())
->addAttributeToSelect('*')
->addAttributeToFilter('status', 1);
if($sort_order !=''){
$courses->setOrder('display_order', 'asc');
$this->setCourses($courses);
}
Best Answer
You are placing your logic in the class constructor, which is called during layout build, not when calling
$this->getCourses()
You'd call something like this in your template
You'd leave your consructor as this
Then you'd define this method in your block
Other way (most common approach, in fact) could be loading the collection directly in a
getCourses()
method in your block, instead of using class constructor. Then you could use the parameter