I'm getting a little frustrated with this, below is code i have to create a custom multi-select attribute for a product, it uses a custom source to pull the data for it from a database table, when selecting the attributes on the product edit screen it saves the id values (primary keys of the table the data comes from).
I thought that the source would also be used on the front end to create check boxes, but nothing i do makes it show up (and i have added a multi select in the admin directly and that attribute shows up just fine):
$eavSetup->addAttribute(
Product::ENTITY,
'svgs',
[
'type' => 'varchar',
'label' => 'Select SVG/Filters',
'input' => 'multiselect',
'source' => 'Vendor\Module\Model\Config\Attributes\SvgOptions',
'backend'=> 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
'required' => false,
'sort_order' => 100,
'global' => ScopedAttributeInterface::SCOPE_STORE,
'group' => 'Product Overview SVG/Filters',
'visible_on_front' => true,
'used_in_product_listing' => true,
'filterable'=>true,
'searchable'=>true,
'comparable'=>false,
'user_defined'=>true,
'apply_to' => 'simple,configurable,virtual,bundle,downloadable',
'is_used_in_grid' => true,
'is_visible_in_grid' => false,
'is_filterable_in_grid' => true
]
);
My data source is this:
public function getAllOptions() : array
{
/** @var Collection $svgCollection */
$svgCollection = $this->svgCollectionFactory->create();
/** @var Collection $svgCategoryCollection */
$svgCategoryCollection = $this->svgCategoryCollectionFactory->create();
// get all categories
$categories = $svgCategoryCollection->setOrder('name','ASC');
// for each category as an option group, assign SVGs
$options = [];
/** @var Category $category */
foreach($categories as $category) {
// reset where in collection so each loop works
$svgCollection->clear()->getSelect()->reset('where');
// get svgs in this category
$svgs = $svgCollection->addFieldToFilter('category_id',$category->getId())
->setOrder('display_name','ASC');
if($svgs->count() > 0){
// create svgs array
$svgOptions = [];
/** @var SVG $svg */
foreach ($svgs as $svg) {
$svgOptions[] = [
'label'=>$svg->getData('display_name'),
'value'=>$svg->getId()
];
}
// add category with svgs to options
$options[] = [
'label'=>$category->getData('name'),
'value'=>$svgOptions
];
}
}
return $options;
}
All the options show in the admin and save no problem, it's just the layered nav they won't show up on
Hello @marcintota03e5
Also try Filterable (no results) Will have to index after changing settings
Also double check attribute values are saved for the product under the store scope you are using.
If you are using only one store toggle single store mode if you are not using that already. That simplifies things.
Changing the backend_type of the attribute from 'text' to 'varchar' and re-index.
Don't forget to copy catalog_product_entity_text's value over to catalog_producut_entity_varchar.
Hope it helps !
If you find our reply helpful, please give us kudos.
A Leading Magento Development Agency That Delivers Powerful Results, Innovation, and Secure Digital Transformation.
WebDesk Solution Support Team
Get a Free Quote | | Adobe Commerce Partner | Hire Us | Call Us 877.536.3789
Thank You,
WebDesk Solution Support Team
Get a Free Quote | Email | Adobe Commerce Partner | Hire Us | Call Us 877.536.3789
Location: 150 King St. W. Toronto, ON M5H 1J9