Magento – Adding custom product attributes to layered navigation but not shown

layered-navigationmagento2product-attribute

I want to show the following attribute in the layered navigation

$eavSetup->addAttribute(
    \Magento\Catalog\Model\Product::ENTITY,
    'test',
    [
                'type' => 'varchar',
                'label' => 'Test',
                'input' => 'select',
                'required' => false,
                'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
                'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                'visible' => true,
                'user_defined' => true,
                'searchable' => false,
                'filterable' => true,
                'filterable_in_search' => true,
                'comparable' => false,
                'visible_on_front' => true,
                'unique' => false,
                'group' => 'General',
                'is_used_in_grid' => true,
                'is_visible_in_grid' => false,
                'is_filterable_in_grid' => true,
                'source' => 'XXX\CustomProductsAttributes\Model\Config\Source\BrandOptions',
                'apply_to'=>'simple,configurable,bundle,grouped'
    ]
);

The attribute is created with the same values that the one I've created through the m2 admin area, with the difference, the one created through the admin area is shown in the layered navigation. I've tested with a new product which has these two attributes, but only appears this one instead of both in the layered navigation.

Best Answer

Here i have add my code for custom attribute. It's working fine for me. Please check and update your code.

$eavSetup->addAttribute(
        \Magento\Catalog\Model\Product::ENTITY, 'rb_brand',
        [
        'group' => 'Product Details',
        'sort_order' => 100,
        'type' => 'int',
        'backend' => '',
        'frontend' => '',
        'label' => 'Shop by Brand',
        'input' => 'select',
        'class' => '',
        'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
        'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_GLOBAL,
        'visible' => true,
        'required' => false,
        'user_defined' => true,
        'searchable' => true,
        'filterable' => true,
        'comparable' => true,
        'visible_in_advanced_search' => true,
        'visible_on_front' => true,
        'used_in_product_listing' => true,
        'unique' => false,
        'apply_to' => '',
        'is_used_in_grid' => true,
        'is_visible_in_grid' => false,
        'is_filterable_in_grid' => true,
        ]
    );
Related Topic