Magento2 Configurable Product – Adding Custom Product Attributes to Layered Navigation

configurable-productmagento2product-attribute

I've added a product attribute by adding the following to InstallData.php within my module.

 $eavSetup->addAttribute(
        \Magento\Catalog\Model\Product::ENTITY,
        'allergens',
        [
            'type' => 'int',
            'label' => 'Free from the following allergens',
            'input' => 'multiselect',
            'required' => false,
            'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
            'global' => Attribute::SCOPE_GLOBAL,
            'visible' => true,
            'user_defined' => false,
            'searchable' => false,
            'filterable' => true,
            'filterable_in_search' => true,
            'comparable' => false,
            'visible_on_front' => false,
            'unique' => false,
            'group' => 'General',
            'is_used_in_grid' => true,
            'is_visible_in_grid' => false,
            'is_filterable_in_grid' => true,
            'label' => [
                'values' => [
                    'Gluten',
                    'Soy',
                    'Eggs',
                    'Shellfish',
                    'Dairy',
                    'Fish',
                    'Nuts',
                    'Yeast'
                ]
            ]
        ]
    );

I've checked this attribute in the CMS, and i can see it's marked as filterable with results, and use in layered navigation is set to true. The issue is that it's not appearing alongside the other filters in my custom layered navigation.

$block->getFilters() is retrieving the filter,

but when i echo the results echo $filter->getItemsCount() it's showing 0 results? Even though i have products with this attribute assigned.

The product deffinately has the attribute saved correctly, so i'm not sure what's happening here.

Any help would be appreciated.

Thanks

Best Answer

I inadvertently created the attributes as system attributes, i added 'system' => 0 where i added the attribute and this seems to have resolved the issue.