0

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?

0

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 – Vlad Patru Oct 25 '18 at 7:42
0

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 – Vlad Patru Oct 25 '18 at 7:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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