سؤال

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.

هل كانت مفيدة؟

المحلول

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'
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى magento.stackexchange
scroll top