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.
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.– Emipro Technologies Pvt. Ltd.2018年04月03日 07:16:28 +00:00Commented Apr 3, 2018 at 7:16
-
@EmiproTechnologiesPvt.Ltd.,Can you please elaborate in more detail ?anonymous– anonymous2018年04月03日 07:17:38 +00:00Commented 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.Emipro Technologies Pvt. Ltd.– Emipro Technologies Pvt. Ltd.2018年04月03日 07:20:10 +00:00Commented 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.anonymous– anonymous2018年04月03日 07:29:32 +00:00Commented Apr 3, 2018 at 7:29
1 Answer 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– Jafar Pinjar2019年01月08日 05:56:50 +00:00Commented Jan 8, 2019 at 5:56
Explore related questions
See similar questions with these tags.