7

I need to retrieve all filterable attributes for category. I'm using that snippet:

$category = Mage::getModel('catalog/category')->load($categoryId);

$layer = Mage::getModel('catalog/layer');

$layer->setCurrentCategory($category);

$attributes = $layer->getFilterableAttributes();//$attributes now is empty array

Category has product with filterable attributes, also in categories options anchor was enabled. This code using in SOAP API.

Maybe someone know where I made mistake?

  • Try to var_dump($category) to see if everything is okay in general. – michael Jan 1 '15 at 15:43
  • You might asking the same thing here stackoverflow.com/questions/3157799/… – Sukeshini Jan 1 '15 at 16:36
  • I can't use var_dump because it's code for api and it return only values defined in wsdl.xml file. But I check category by pass it to one of this value. Category model is OK, and it seting to layer correct, and I can get it back by $layer->getCurrentCategory();P – Quickerz Jan 1 '15 at 16:44
  • You can try to add Mage::log($collection->getSelect(), Zend_Log::INFO, 'blah.log', true); in app/code/core/Mage/Catalog/Model/Layer.php -> public function getFilterableAttributes() before return and to run logged SQL query directly in the database or to compare it with the SQL query that is logged when you open the category through the site. – michael Jan 2 '15 at 10:22
  • getFilterableAttributes() return empty array because $setIds = $this->_getSetIds() return empty array. It happen because $this->getProductCollection() return empty collection. Maybe something else needed fo $layer to get product collection from it? – Quickerz Jan 5 '15 at 7:24
0

Most probably you didn't set the store id, we need to do so since it's implemented in the api:

protected function _retrieve(){

    $id = $this->getRequest()->getParam('id');
    $store_id = $this->getRequest()->getParam('store_id');

    $category = Mage::getModel("catalog/category")->load($id);        
    $category->setStoreId($store_id);

    if (!($category->getId())) $this->_critical(self::RESOURCE_NOT_FOUND);

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

following code is used to get filterable attributes and their options

$categoryId = '5'; // replace with your category id
$category = Mage::getModel("catalog/category")->load($categoryId);
$layer = Mage::getModel("catalog/layer");
$layer->setCurrentCategory($category);

$setIds = Mage::getModel('eav/entity_attribute_set')
            ->load($attrSetName, 'attribute_set_name')
            ->getAttributeSetId();

$attributes = Mage::getResourceModel('catalog/product_attribute_collection');
$attributes->setItemObjectClass('catalog/resource_eav_attribute')
                ->setAttributeSetFilter($setIds)
                ->addStoreLabel(Mage::app()->getStore()->getId())
                ->setOrder('position', 'ASC');


$attributes->addFieldToFilter('additional_table.is_filterable', array('gt' => 0));
$attributes->load();

foreach ($attributes as $attribute) {
    $filter_attr = array();
    $filter_attr['title'] = $attribute->getFrontendLabel();
    $filter_attr['code'] = $attribute->getAttributeCode();

    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();
    $i=0;

    foreach($result->getItems() as $option) {
        $attr_option = array();
        if($attribute->getAttributeCode() == 'price') {
            $attr_option['label'] = str_replace(array('<span class="price">','</span>'),'',$option->getLabel());
        } else {
            $attr_option['label'] = $option->getLabel();
        }

        $attr_option['value'] = $option->getValue();
        $attr_option['count'] = $option->getCount();
        $i++;
        $filter_attr['options'][] = $attr_option;
    }

    if($i!=0){
        $filter_attributes[] = $filter_attr;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.