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.

asked Apr 3, 2018 at 7:09
4
  • I think you need to add attribute options with store code. Commented Apr 3, 2018 at 7:16
  • @EmiproTechnologiesPvt.Ltd.,Can you please elaborate in more detail ? Commented Apr 3, 2018 at 7:17
  • We think there is some store related issue you faced. Please check table screenshot: nimb.ws/yGlMDW and check your table. Commented Apr 3, 2018 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. Commented Apr 3, 2018 at 7:29

1 Answer 1

3

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'
Rajeev K Tomy
17.3k6 gold badges64 silver badges104 bronze badges
answered Apr 3, 2018 at 8:42
1
  • hi @Amit, can we create the attribute without installer? Commented Jan 8, 2019 at 5:56

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.