大米CMS官网论坛,大米站长联盟,大米站长之家,大米开发者社区

 找回密码
 注册大米会员

QQ登录

只需一步,快速开始

查看: 5019|回复: 0
打印 上一主题 下一主题

magento1搜索产生filter选项代码示例

[复制链接]

498

主题

775

帖子

7645

积分

超级版主

Rank: 8Rank: 8

积分
7645

授权用户

跳转到指定楼层
楼主
发表于 2019-2-18 09:16:43 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 追影 于 2019-2-20 11:35 编辑

controller:
  1. <?php
  2. class Silk_Mapi_PepperProductsController extends Silk_Mapi_Controller_Action {
  3.     protected $_productCollection;

  4.     public function listAction()
  5.     {
  6.         $response = $this->getResponse();
  7.         $response->setHeader('Content-type', 'application/json');
  8.         $this->loadLayout();
  9.         $this->getLayout()->createBlock('mapi/pepperlist')->toHtml();
  10.     }

  11. }
复制代码

block:
  1. <?php
  2. class Silk_Mapi_Block_Pepper extends Mage_Catalog_Block_Product_List
  3. {
  4.     protected $_filters = null;
  5.     protected function getCurrentCategoryId(){
  6.         $categoryId = $this->getRequest()->getParam('cat');
  7.         if(!$categoryId) $categoryId = Mage::app()->getStore()->getRootCategoryId();
  8.         $this->setCategoryId($categoryId);
  9.         return $categoryId;
  10.     }
  11.         /**
  12.      * Need use as _prepareLayout - but problem in declaring collection from
  13.      * another block (was problem with search result)
  14.      */
  15.     protected function _beforeToHtml()
  16.     {

  17.         // called prepare sortable parameters
  18.         $collection = $this->_getProductCollection();

  19.         $toolbar = $this->getToolbarBlock();
  20.         // use sortable parameters
  21.         if ($orders = $this->getAvailableOrders()) {
  22.             $toolbar->setAvailableOrders($orders);
  23.         }
  24.         if ($sort = $this->getSortBy()) {
  25.             $toolbar->setDefaultOrder($sort);
  26.         }
  27.         if ($dir = $this->getDefaultDirection()) {
  28.             $toolbar->setDefaultDirection($dir);
  29.         }
  30.         if ($modes = $this->getModes()) {
  31.             $toolbar->setModes($modes);
  32.         }

  33.         // set collection to toolbar and apply sort
  34.         $toolbar->setCollection($collection);

  35.         $this->setChild('toolbar', $toolbar);
  36.         Mage::dispatchEvent('catalog_block_product_list_collection', array(
  37.             'collection' => $this->_getProductCollection()
  38.         ));
  39.         $collection->load();
  40.         $category = Mage::getModel('catalog/category')->load($this->getCurrentCategoryId());
  41.         $productresult['avaiable_sort_fileds'] = $category->getAvailableSortByOptions();
  42.         $productresult['avaiable_filter_fileds'] = $this->_filters;
  43.         $productresult['total_num'] = $toolbar->getTotalNum();
  44.         $productresult['products'] = $this->formatProductCollection($collection);
  45.         $result['code'] = 200;
  46.         $result['msg'] = '';
  47.         $result['data'] = $productresult;

  48.         $data = json_encode( $result,320);
  49.         echo $data;
  50.     }

  51.     protected function formatProductCollection($collection)
  52.     {
  53.         $results = array();
  54.         /** @var Silk_Mapi_Helper_Product $productHelper */
  55.         $productHelper = $this->helper("mapi/product");

  56.         /** @var Mage_Catalog_Helper_Image $imgHelper */
  57.         $imgHelper = Mage::helper('catalog/image');
  58.         /** @var Mage_Catalog_Model_Product $product */
  59.         foreach ($collection as $product) {
  60.             //$product = Mage::getModel('catalog/product')->load($product->getId());
  61.             $image = $imgHelper->init($product, 'small_image')->resize(250) . "";
  62.             $results[]  = $productHelper->formatListItem($product);
  63.         }
  64.         return $results;
  65.     }

  66.     protected function _getProductCollection()
  67.     {
  68.         $this->getCurrentCategoryId();
  69.         if (is_null($this->_productCollection)) {
  70.             $layer = $this->getLayer();
  71.             /* @var $layer Mage_Catalog_Model_Layer */
  72.             if ($this->getShowRootCategory()) {
  73.                 $this->setCategoryId(Mage::app()->getStore()->getRootCategoryId());
  74.             }

  75.             $origCategory = null;
  76.             if ($this->getCategoryId()) {
  77.                 $category = Mage::getModel('catalog/category')->load($this->getCategoryId());
  78.                 if ($category->getId()) {
  79.                     $origCategory = $layer->getCurrentCategory();
  80.                     $layer->setCurrentCategory($category);
  81.                     $this->addModelTags($category);
  82.                 }
  83.             }
  84.             $this->_productCollection = $layer->getProductCollection();
  85.             $attributes = $layer->getFilterableAttributes();
  86.             $this->formatLayers($attributes);//apply了request 產生filter option
  87.             $this->prepareSortableFieldsByCategory($layer->getCurrentCategory());

  88.             if ($origCategory) {
  89.                 $layer->setCurrentCategory($origCategory);
  90.             }
  91.         }

  92.         return $this->_productCollection;
  93.     }

  94.     protected function formatLayers($attributes)
  95.     {
  96.         $results = array();
  97.         $categoryBlock = Mage::app()->getLayout()->createBlock('catalog/layer_filter_category')
  98.             ->setLayer($this->getLayer())
  99.             ->init();
  100.         $this->getLayer()->apply();
  101.         $cate_items = $categoryBlock->getItems();
  102.         foreach($cate_items as $item){
  103.             $categoryFilters[] = array(
  104.                 'label' => $this->stripTags($item->getLabel()),
  105.                 'value' => $item->getValue(),
  106.                 'count' => (int)$item->getCount(),
  107.             );
  108.         }
  109.         if (count($categoryFilters)) {
  110.             $results['filters'][] = array(
  111.                 'code' => 'cat',
  112.                 'label' => Mage::helper('catalog')->__('Category'),
  113.                 'options' => $categoryFilters
  114.             );
  115.         }
  116.         foreach ($attributes as $attribute) {
  117.             if ($attribute->getAttributeCode() == 'price') {
  118.                 $filterBlockName = 'catalog/layer_filter_price';
  119.             } elseif ($attribute->getBackendType() == 'decimal') {
  120.                 $filterBlockName = 'catalog/layer_filter_decimal';
  121.             } else {
  122.                 $filterBlockName = 'catalogsearch/layer_filter_attribute';
  123.             }
  124.             $result = Mage::app()->getLayout()->createBlock($filterBlockName)->setLayer($this->getLayer())->setAttributeModel($attribute)->init();
  125.             $this->getLayer()->apply();
  126.             $filters = array();
  127.             foreach($result->getItems() as $item) {
  128.                 if(intval($item->getCount())>0){
  129.                 $filters[] = array(
  130.                     'label' => $this->stripTags($item->getLabel()),
  131.                     'value' => $item->getValue(),
  132.                     'count' => (int)$item->getCount(),
  133.                 );
  134.                 }
  135.             }
  136.             if($filters){
  137.             $results['filters'][] = array(
  138.                 'code' => $attribute->getAttributeCode(),
  139.                 'label' => $attribute->getStoreLabel(),
  140.                 'options' => $filters
  141.             );
  142.             }
  143.         }
  144.         $this->_filters = $results;
  145.         return $this->_filters;
  146.     }


  147.     public function getLayer()
  148.     {
  149.         $layer = Mage::registry('current_layer');
  150.         if ($layer) {
  151.             return $layer;
  152.         }
  153.         $layer = Mage::getSingleton('catalogsearch/layer');
  154.         Mage::register('current_layer',$layer);
  155.         return $layer;
  156.     }

  157.     protected function _getRequest()
  158.     {
  159.         if (!$this->_request) {
  160.             $this->_request = Mage::app()->getRequest();
  161.         }
  162.         return $this->_request;
  163.     }
  164. }
复制代码




分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册大米会员

本版积分规则

QQ|小黑屋|大米CMS社区 ( 蜀ICP备11002200号-2广告联系:广告联系 

Powered by 大米CMS

© 2010-2020 大米CMS Inc.

快速回复 返回顶部 返回列表