1

I am trying to get filterable attributes of product collection after applying filter to it programmatically.

same as layered navigation idea after I apply filter to category, it just return attributes that I can filter with it. that's what I am trying to do here.

I need that to return filterable attributes after applying filter to collection to send it to mobile app via api.

$category = Mage::getModel('catalog/category')->setStoreId(1)->load(84);
$collection = $category->getProductCollection()
                        ->setOrder('position', 'ASC')
                        ->addAttributeToSelect('*')
                        ->addAttributeToFilter('status', 1)
                        ->addAttributeToFilter('visibility', 4)
                        ->setStoreId(1);


$filtringAttributes = Array
(
    (object) array
        (
            'label' => 'Ref Nav Doors',
            'code' => 'ref_nav_doors',
            'value' => 693
        ),

    (object) array
        (
            'label' => 'Color',
            'code' => 'color',
            'value' => 5
        )
);

if (!empty($filtringAttributes)) {

    foreach ($filtringAttributes as $attribute) {
        $key = $attribute->code;
        $value = $attribute->value;
        $collection->addAttributeToFilter($key, $value);
    }
}
4
  • What is the result of your code?
    – mtr.web
    Oct 26, 2018 at 18:37
  • My code just filtering products collection using attributes and return filtered collection.. i want to get filterable attributes from this collection Oct 26, 2018 at 19:06
  • You want to return the attributes that it is filtered by, or all possible attributes to filter with?
    – mtr.web
    Oct 26, 2018 at 19:32
  • I already have the attributes that it is filtered by & i want all possible attributes that i can filter with. Oct 26, 2018 at 20:05

1 Answer 1

0

I am not sure if this fits your needs exactly, but it will give you all of the columns added by your addAttributeToSelect('*') function, and all of those will be filterable in the collection.

$first = $collection
         ->getFirstItem()
         ->toArray();

$columns = array_keys($first);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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