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;

Пожалуйста, предложите, как мне поступить

1

Решение

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

//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);
2

Другие решения

Других решений пока нет …