3

I'm having issues while creating product attribute with Install Data.php with my custom module.

Attribute is crated also with options what i have defined in source but i am facing weird check below screenshot.

Admin Magento

Vendor/Module/Setup/InstallData.php

 public function install(
    ModuleDataSetupInterface $setup,
    ModuleContextInterface $context
) {
    $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

    $eavSetup->removeAttribute(\Magento\Catalog\Model\Product::ENTITY,'brands');

    $eavSetup->addAttribute(
             \Magento\Catalog\Model\Product::ENTITY, 'brands', [
            'type' => 'int',
            'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
            'frontend' => '',
            'label' => 'Brands',
            'input' => 'select',
            'group' => 'General',
            'class' => 'brands',
            'source' => 'Vendor\Module\Model\Brands\Values',
            'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
            'visible' =>    true,
            'required' => false,
            'user_defined' => false,
            'default' => '1',
            'searchable' => false,
            'filterable' => false,
            'comparable' => false,
            'visible_on_front' => false,
            'used_in_product_listing' => true,
            'unique' => false
                ]
    );

Vendor/Module/Model/Brands/Values.php

<?php
namespace Vivek\ShopAsGroup\Model\Brands;

use Magento\Eav\Model\ResourceModel\Entity\Attribute\OptionFactory;
use Magento\Framework\DB\Ddl\Table;


class Values extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
{

protected $optionFactory;


public function getAllOptions()
{

    $this->_options=[ ['label'=>'Select Options', 'value'=>''],
                      ['label'=>'Nike', 'value'=>'1'],
                      ['label'=>'Puma', 'value'=>'2'],
                      ['label'=>'Adidas', 'value'=>'3']
                     ];
    return $this->_options;
}


public function getOptionText($value)
{
    foreach ($this->getAllOptions() as $option) 
    {
        if ($option['value'] == $value) {
            return $option['label'];
        }
    }
    return false;
}


public function getFlatColumns()
{
    $attributeCode = $this->getAttribute()->getAttributeCode();
    return [
        $attributeCode => [
            'unsigned' => false,
            'default' => null,
            'extra' => null,
            'type' => Table::TYPE_INTEGER,
            'nullable' => true,
            'comment' => 'Custom Attribute Options  ' . $attributeCode . ' column',
        ],
    ];
}
}

Please help me with this, What i am missing ??

Thanks in Advance.

  • I think you need to add attribute options with store code. – Emipro Technologies Pvt. Ltd. Apr 3 '18 at 7:16
  • @EmiproTechnologiesPvt.Ltd.,Can you please elaborate in more detail ? – anonymous Apr 3 '18 at 7:17
  • We think there is some store related issue you faced. Please check table screenshot: nimb.ws/yGlMDW and check your table. – Emipro Technologies Pvt. Ltd. Apr 3 '18 at 7:20
  • I dont think its causing issues, cause i checked my DB and i am running with Sample data and all sample attributes options has store_id = 0. – anonymous Apr 3 '18 at 7:29
1

Please do following suggestions in the install script.

Make 'user_defined' => true from 'user_defined' => false

In case the above solution didn't work, then remove the backend and source attributes. i.e,

  • 'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend'
  • 'source' => 'Vendor\Module\Model\Brands\Values'
  • hi @Amit, can we create the attribute without installer? – jafar pinjar Jan 8 at 5:56

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.