2015-09-24 4 views
1

Программным способом Я строю пользовательские веб-сервисы для мобильных приложений,Magento получить Многослойный фильтр из коллекции

Я хочу, чтобы загрузить фильтр по сбору продуктов программно.

В настоящее время я получаю фильтр по всей категории, но когда мы применяем какой-либо фильтр, он снова даст такую ​​же опцию фильтра.

Ниже приведен код, который я использовал для категории фильтра

$layer = Mage::getModel("catalog/layer"); 
     $_category = Mage::getModel("catalog/category")->load($category_id); 
     $layer->setCurrentCategory($_category); 
     $attributes = $layer->getFilterableAttributes(); 

     /* custom for filters 
     $collection = Mage::getResourceModel('catalog/product_attribute_collection'); 
     $collection->setItemObjectClass('catalog/resource_eav_attribute'); 
     $collection->setAttributeSetFilter(array(4)); 
     $collection->addStoreLabel(Mage::app()->getStore()->getId()); 
     $collection->setOrder('position', 'ASC'); 
     $collection->addFieldToFilter('additional_table.is_filterable', array('gt' => 0)); 
     $attributes = $collection->load(); 
     */ 

     // print_r($attributes->getData());exit(); 
     $attributeCollection =array(); 

     $i=0; 
     $attributeCollection = array(); 
     foreach ($attributes as $attribute) { 
     if($attribute->getAttributeCode() == 'price') { 
     $filterBlockName = 'catalog/layer_filter_price'; 
     }elseif($attribute->getBackendType() == 'decimal'){ 
     $filterBlockName = 'catalog/layer_filter_decimal'; 
     }else{ 
     $filterBlockName = 'catalog/layer_filter_attribute'; 
     } 
     $result = Mage::app()->getLayout()->createBlock($filterBlockName)->setLayer($layer)->setAttributeModel($attribute)->init(); 
     $attributeCollection[$i]['Code'] = $attribute->getAttributeCode(); 
     $attributeCollection[$i]['Label'] = $attribute->getStoreLabel(); 
     $j=0; 
     $attributeOptionCollection =array(); 
     foreach($result->getItems() as $option) { 
     if($attribute->getAttributeCode()=='price'){ 
      $attributeOptionCollection[$j]['Label'] = strip_tags($option->getLabel()); 
     }else{ 
      $attributeOptionCollection[$j]['Label'] = $option->getLabel(); 
     } 
     $attributeOptionCollection[$j]['Value'] = $option->getValue(); 
     $attributeOptionCollection[$j]['Type'] = $option->getFrontend(); 
     $j++; 
     } 
     $attributeCollection[$i]['Options'] = $attributeOptionCollection; 
     $i++; 
    } 
    // print_r($attributeCollection);exit(); 
    // echo "<pre>"; 
    $counter = 0; 
    $availableSortOptions = $_category->getavailablesortbyoptions(); 
    foreach ($availableSortOptions as $key=>$options) { 
     $optinsList[$counter]['Code'] = $key; 
     $optinsList[$counter]['Label'] =$options; 
     $counter++; 
     // print_r($options); 
    } 
     $information['Filters'] = $attributeCollection; 

Пожалуйста, подсказывают, как я проследовать

+0

БУДУ проверить и получить результат –

ответ

2

Я использовал это для определенной категории - надеюсь, что это помогает

//require necessary files 
require_once('../app/Mage.php'); 

$categoryID = $_POST['categoryID']; 

//necessary initialization 
Mage::app(); 
$websiteId = Mage::app()->getWebsite()->getId(); 
$store = Mage::app()->getStore(); 

try{ 
    $json = array('status' => true); 
    $json['data'] = array(); 
    $layer = Mage::getModel("catalog/layer"); 
    $category = Mage::getModel("catalog/category")->load($categoryID); // 3rd Category 
    $layer->setCurrentCategory($category); 
    $attributes = $layer->getFilterableAttributes(); 
    foreach ($attributes as $attribute) { 
     $filterBlockName = 'catalog/layer_filter_attribute'; 
     $result = Mage::app()->getLayout()->createBlock($filterBlockName)->setLayer($layer)->setAttributeModel($attribute)->init(); 
     foreach($result->getItems() as $option) { 
      $count[] = array('attribute_name' => $option->getLabel(),'attribute_value' => $option->getValue()); 
     } 
     if($count!=null){ 
      $json['data'][] = array('name'=>ucfirst($attribute->getAttributeCode()),'count'=>$count); 
     } 
     unset($count); 
    } 
} 
catch (Exception $e) { 
    $json = array('status' => false, 'message' => $e->getMessage()); 
} 
echo json_encode($json); 
Смежные вопросы