|
本帖最后由 追影 于 2019-2-20 11:35 编辑
controller:
- <?php
- class Silk_Mapi_PepperProductsController extends Silk_Mapi_Controller_Action {
- protected $_productCollection;
- public function listAction()
- {
- $response = $this->getResponse();
- $response->setHeader('Content-type', 'application/json');
- $this->loadLayout();
- $this->getLayout()->createBlock('mapi/pepperlist')->toHtml();
- }
- }
复制代码
block:- <?php
- class Silk_Mapi_Block_Pepper extends Mage_Catalog_Block_Product_List
- {
- protected $_filters = null;
- protected function getCurrentCategoryId(){
- $categoryId = $this->getRequest()->getParam('cat');
- if(!$categoryId) $categoryId = Mage::app()->getStore()->getRootCategoryId();
- $this->setCategoryId($categoryId);
- return $categoryId;
- }
- /**
- * Need use as _prepareLayout - but problem in declaring collection from
- * another block (was problem with search result)
- */
- protected function _beforeToHtml()
- {
- // called prepare sortable parameters
- $collection = $this->_getProductCollection();
- $toolbar = $this->getToolbarBlock();
- // use sortable parameters
- if ($orders = $this->getAvailableOrders()) {
- $toolbar->setAvailableOrders($orders);
- }
- if ($sort = $this->getSortBy()) {
- $toolbar->setDefaultOrder($sort);
- }
- if ($dir = $this->getDefaultDirection()) {
- $toolbar->setDefaultDirection($dir);
- }
- if ($modes = $this->getModes()) {
- $toolbar->setModes($modes);
- }
- // set collection to toolbar and apply sort
- $toolbar->setCollection($collection);
- $this->setChild('toolbar', $toolbar);
- Mage::dispatchEvent('catalog_block_product_list_collection', array(
- 'collection' => $this->_getProductCollection()
- ));
- $collection->load();
- $category = Mage::getModel('catalog/category')->load($this->getCurrentCategoryId());
- $productresult['avaiable_sort_fileds'] = $category->getAvailableSortByOptions();
- $productresult['avaiable_filter_fileds'] = $this->_filters;
- $productresult['total_num'] = $toolbar->getTotalNum();
- $productresult['products'] = $this->formatProductCollection($collection);
- $result['code'] = 200;
- $result['msg'] = '';
- $result['data'] = $productresult;
- $data = json_encode( $result,320);
- echo $data;
- }
- protected function formatProductCollection($collection)
- {
- $results = array();
- /** @var Silk_Mapi_Helper_Product $productHelper */
- $productHelper = $this->helper("mapi/product");
- /** @var Mage_Catalog_Helper_Image $imgHelper */
- $imgHelper = Mage::helper('catalog/image');
- /** @var Mage_Catalog_Model_Product $product */
- foreach ($collection as $product) {
- //$product = Mage::getModel('catalog/product')->load($product->getId());
- $image = $imgHelper->init($product, 'small_image')->resize(250) . "";
- $results[] = $productHelper->formatListItem($product);
- }
- return $results;
- }
- protected function _getProductCollection()
- {
- $this->getCurrentCategoryId();
- if (is_null($this->_productCollection)) {
- $layer = $this->getLayer();
- /* @var $layer Mage_Catalog_Model_Layer */
- if ($this->getShowRootCategory()) {
- $this->setCategoryId(Mage::app()->getStore()->getRootCategoryId());
- }
- $origCategory = null;
- if ($this->getCategoryId()) {
- $category = Mage::getModel('catalog/category')->load($this->getCategoryId());
- if ($category->getId()) {
- $origCategory = $layer->getCurrentCategory();
- $layer->setCurrentCategory($category);
- $this->addModelTags($category);
- }
- }
- $this->_productCollection = $layer->getProductCollection();
- $attributes = $layer->getFilterableAttributes();
- $this->formatLayers($attributes);//apply了request 產生filter option
- $this->prepareSortableFieldsByCategory($layer->getCurrentCategory());
- if ($origCategory) {
- $layer->setCurrentCategory($origCategory);
- }
- }
- return $this->_productCollection;
- }
- protected function formatLayers($attributes)
- {
- $results = array();
- $categoryBlock = Mage::app()->getLayout()->createBlock('catalog/layer_filter_category')
- ->setLayer($this->getLayer())
- ->init();
- $this->getLayer()->apply();
- $cate_items = $categoryBlock->getItems();
- foreach($cate_items as $item){
- $categoryFilters[] = array(
- 'label' => $this->stripTags($item->getLabel()),
- 'value' => $item->getValue(),
- 'count' => (int)$item->getCount(),
- );
- }
- if (count($categoryFilters)) {
- $results['filters'][] = array(
- 'code' => 'cat',
- 'label' => Mage::helper('catalog')->__('Category'),
- 'options' => $categoryFilters
- );
- }
- foreach ($attributes as $attribute) {
- if ($attribute->getAttributeCode() == 'price') {
- $filterBlockName = 'catalog/layer_filter_price';
- } elseif ($attribute->getBackendType() == 'decimal') {
- $filterBlockName = 'catalog/layer_filter_decimal';
- } else {
- $filterBlockName = 'catalogsearch/layer_filter_attribute';
- }
- $result = Mage::app()->getLayout()->createBlock($filterBlockName)->setLayer($this->getLayer())->setAttributeModel($attribute)->init();
- $this->getLayer()->apply();
- $filters = array();
- foreach($result->getItems() as $item) {
- if(intval($item->getCount())>0){
- $filters[] = array(
- 'label' => $this->stripTags($item->getLabel()),
- 'value' => $item->getValue(),
- 'count' => (int)$item->getCount(),
- );
- }
- }
- if($filters){
- $results['filters'][] = array(
- 'code' => $attribute->getAttributeCode(),
- 'label' => $attribute->getStoreLabel(),
- 'options' => $filters
- );
- }
- }
- $this->_filters = $results;
- return $this->_filters;
- }
- public function getLayer()
- {
- $layer = Mage::registry('current_layer');
- if ($layer) {
- return $layer;
- }
- $layer = Mage::getSingleton('catalogsearch/layer');
- Mage::register('current_layer',$layer);
- return $layer;
- }
- protected function _getRequest()
- {
- if (!$this->_request) {
- $this->_request = Mage::app()->getRequest();
- }
- return $this->_request;
- }
- }
复制代码
|
|