柳暗花明笔记

    magento 修改列表页面的排序方法及不记住上次的选择排序

    所属分类:[ php&web ] 猫哲学家 发表于:2015-09-24 16:11 浏览:410

    要自己写一个模块来实现:

    包:Lanmiz

    模块:Catalog

    目录结构和系统的Catalog目录结构是一样的:

    class : Lanmiz_Catalog_Model_Config 注意搞明白class名和目录结构的关系

    /**
    * @package Lanmiz_Catalog
    */

    class Lanmiz_Catalog_Model_Config extends Mage_Catalog_Model_Config
    {
    public function getAttributeUsedForSortByArray()
    {
    $options = parent::getAttributeUsedForSortByArray();

    if (!isset($options['created_at']))
    {
    $options['created_at'] = Mage::helper('catalog')->__('Newly Listed');
    }

    return $options;
    }
    }

     

    class:Lanmiz_Catalog_Block_Product_List_Toolbar


    /**
    * Product list toolbar
    *
    * @category Lanmiz
    * @package Lanmiz_Catalog
    * @author Lanmiz overwrite
    */
    class Lanmiz_Catalog_Block_Product_List_Toolbar extends Mage_Catalog_Block_Product_List_Toolbar
    {


    /**
    * Overwrite sort field
    * @return array
    */
    public function getAvailableOrders()
    {

    $this->addOrderToAvailableOrders('mostview', 'Popularity');

    $this->removeOrderFromAvailableOrders('position');
    $this->removeOrderFromAvailableOrders('relevance');
    $this->setDefaultOrder('created_at');
    $this->setDefaultDirection('desc');

    // print_r($this->_availableOrder);

    // $new_order = array();

    asort($this->_availableOrder);

    return $this->_availableOrder;
    }

    /**
    * Overwrite system function
    * @return $this
    */
    public function setCollection($collection)
    {
    $this->_collection = $collection;

    $this->_collection->setCurPage($this->getCurrentPage());

    $limit = (int)$this->getLimit();

    if ($limit)
    {
    $this->_collection->setPageSize($limit);
    }

    if ($this->getCurrentOrder())
    {
    $this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
    }

    if($this->getCurrentOrder()=='mostview')
    {
    $this->_collection->getSelect()->joinInner('report_event AS _table_views', ' _table_views.object_id = e.entity_id', 'COUNT(_table_views.event_id) AS views')->group('e.entity_id')->order('views DESC');
    }

    $limit= $this->getDefaultPerPageValue();

    $page = $this->getRequest()->getParam('p');

    if(isset($page))
    {
    if(($page - 1) == 0)
    {
    $this->_collection->getSelect()->limit($limit);
    }
    else
    {
    $this->_collection->getSelect()->limit($limit, ($page - 1) * $limit);
    }
    }
    else
    {
    $this->_collection->getSelect()->limit($limit);
    }

    $this->_collection->load();

    return $this;
    }


    /**
    * 上次的排序不记到cookie上
    *
    * @return string
    */
    public function getCurrentOrder()
    {
    $order = $this->_getData('_current_grid_order');
    if ($order) {
    return $order;
    }

    $orders = $this->getAvailableOrders();
    $defaultOrder = $this->_orderField;

    if (!isset($orders[$defaultOrder])) {
    $keys = array_keys($orders);
    $defaultOrder = $keys[0];
    }

    $order = $this->getRequest()->getParam($this->getOrderVarName());
    if ($order && isset($orders[$order])) {
    if ($order == $defaultOrder) {
    Mage::getSingleton('catalog/session')->unsSortOrder();
    } else {
    $this->_memorizeParam('sort_order', $order);
    }
    } else {
    // $order = Mage::getSingleton('catalog/session')->getSortOrder();
    }
    // validate session value
    if (!$order || !isset($orders[$order])) {
    $order = $defaultOrder;
    }
    $this->setData('_current_grid_order', $order);
    return $order;
    }

    }

    标签: magento 
    上一编文章 jquery on 比较好用 下一编文章 magento 覆盖原来模块的config.xml写法
    最近文章
    发表评论