I have a product attribute for configurable products as a "dropdown" and I would like to show it on product listing/grid how can I do this?
2 Answers
set this in the attribute creation. 'used_in_product_listing' => true,
<?php
namespace Mageplaza\HelloWorld\Setup;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
class InstallData implements InstallDataInterface
{
private $eavSetupFactory;
public function __construct(EavSetupFactory $eavSetupFactory)
{
$this->eavSetupFactory = $eavSetupFactory;
}
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'sample_attribute',
[
'type' => 'int',
'backend' => '',
'frontend' => '',
'label' => 'Sample Atrribute',
'input' => '',
'class' => '',
'source' => '',
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
'visible' => true,
'required' => true,
'user_defined' => false,
'default' => '',
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'used_in_product_listing' => true,
'unique' => false,
'apply_to' => ''
]
);
}
}
-
The attribute was created from admin, it is set to Yes for Used in product Listing Commented Oct 25, 2018 at 7:42
You can do this by admin configuration. Please traverse to
admin panel ->Stores ->Product ->Your attribute ->StoreFront properties ->Used in Product Listing ->Yes
Clear cache and run indexing and check the grid.
Hope it helps !
-
The attribute is set to Yes for Used in product Listing but it only shows if is a swatch Commented Oct 25, 2018 at 7:43