Create attribute option using Data patch in Magento2
-
12-04-2021 - |
Question
I am creating a product attribute with option using Magento 2.3 Data patch.
Used below code.
<?php
declare(strict_types=1);
namespace Vendor\Module\Setup\Patch\Data;
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
class AddFeatureAttribute implements DataPatchInterface
{
private $moduleDataSetup;
private $eavSetupFactory;
public $_storeManager;
private $logger;
protected $_dir;
public function __construct(
ModuleDataSetupInterface $moduleDataSetup,
EavSetupFactory $eavSetupFactory,
\Psr\Log\LoggerInterface $logger,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\Filesystem\DirectoryList $dir
) {
$this->moduleDataSetup = $moduleDataSetup;
$this->eavSetupFactory = $eavSetupFactory;
$this->_storeManager = $storeManager;
$this->logger = $logger;
$this->_dir = $dir;
}
/**
* {@inheritdoc}
*/
public function apply()
{
/** @var EavSetup $eavSetup */
$eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
$fileName = 'attributes.csv';
$pubPath = $this->_dir->getPath('pub');
$attributeFile = $pubPath.'/'.$fileName;
$arrResult = array();
if(($handle = fopen($attributeFile, 'r')) !== FALSE) {
$row = 1;
while(($data = fgetcsv($handle, 1000000, ',')) !== FALSE) {
$col_count = count($data);
$arrResult[] = $data;
$row++;
}
fclose($handle);
}
$i = 0;
foreach ($arrResult as $line) {
if($i > 0){
$attributeCode = $line[0];
$attributeName = $line[1];
$inputType = strtolower($line[2]);
$options = $line[3];
$type = 'int';
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
$attributeCode,
[
'type' => $type,
'group' => 'General',
'backend' => '',
'frontend' => '',
'label' => $attributeName,
'input' => $inputType,
'class' => '',
'source' => '',
'global' => ScopedAttributeInterface::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => true,
'searchable' => false,
'filterable' => true,
"filterable_in_search" => '1',
'comparable' => false,
'visible_on_front' => true,
'used_in_product_listing' => true,
'unique' => false,
'option' => [
'values' => $options,
],
'system' => 1
]
);
}
$i++;
}
}
public static function getDependencies()
{
return [];
}
/**
* {@inheritdoc}
*/
public function getAliases()
{
return [];
}
}
The sample csv file is like below
The above code not creating the options, please someone help me on that how can we create a attribute with options. Thanks!!
La solution
you can try following way..
Here my attribute is Color
Vendor/Module/Setup/Patch/Data/AddColorProductAttribute.php
<?php
namespace Vendor\Module\Setup\Patch\Data;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\Patch\PatchRevertableInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
/**
* Class AddColorProductAttribute
*
* @package Vendor\Module\Setup\Patch\Data
*/
class AddColorProductAttribute implements DataPatchInterface, PatchRevertableInterface
{
/**
* @var ModuleDataSetupInterface
*/
private $moduleDataSetup;
/**
* @var EavSetupFactory
*/
private $eavSetupFactory;
/**
* Constructor
*
* @param ModuleDataSetupInterface $moduleDataSetup
* @param EavSetupFactory $eavSetupFactory
*/
public function __construct(
ModuleDataSetupInterface $moduleDataSetup,
EavSetupFactory $eavSetupFactory
) {
$this->moduleDataSetup = $moduleDataSetup;
$this->eavSetupFactory = $eavSetupFactory;
}
/**
* {@inheritdoc}
*/
public function apply()
{
$this->moduleDataSetup->getConnection()->startSetup();
/** @var EavSetup $eavSetup */
$eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'color',
[
'type' => 'int',
'label' => 'color',
'input' => 'select',
'source' => \Vendor\Module\Model\Product\Attribute\Source\Color::class,
'frontend' => '',
'required' => true,
'backend' => '',
'sort_order' => '30',
'global' => ScopedAttributeInterface::SCOPE_GLOBAL,
'default' => null,
'visible' => true,
'user_defined' => true,
'searchable' => true,
'filterable' => true,
'comparable' => true,
'visible_on_front' => true,
'unique' => false,
'apply_to' => 'simple,grouped,bundle,configurable,virtual',
'group' => 'General',
'used_in_product_listing' => true,
'is_used_in_grid' => true,
'is_visible_in_grid' => false,
'is_filterable_in_grid' => false,
'option' => ''
]
);
$this->moduleDataSetup->getConnection()->endSetup();
}
public function revert()
{
$this->moduleDataSetup->getConnection()->startSetup();
/** @var EavSetup $eavSetup */
$eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
$eavSetup->removeAttribute(\Magento\Catalog\Model\Product::ENTITY, 'color');
$this->moduleDataSetup->getConnection()->endSetup();
}
/**
* {@inheritdoc}
*/
public function getAliases()
{
return [];
}
/**
* {@inheritdoc}
*/
public static function getDependencies()
{
return [
];
}
}
Vendor/Module/Model/Product/Attribute/Source/Color.php
<?php
namespace Vendor\Module\Model\Product\Attribute\Source;
/**
* Class Color
*
* @package Vendor\Module\Model\Product\Attribute\Source
*/
class Color extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
{
/**
* getAllOptions
*
* @return array
*/
public function getAllOptions()
{
$this->_options = [
];
return $this->_options;
}
/**
* @return array
*/
public function getFlatColumns()
{
$attributeCode = $this->getAttribute()->getAttributeCode();
return [
$attributeCode => [
'unsigned' => false,
'default' => null,
'extra' => null,
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
'length' => 255,
'nullable' => true,
'comment' => $attributeCode . ' column',
],
];
}
/**
* @return array
*/
public function getFlatIndexes()
{
$indexes = [];
$index = 'IDX_' . strtoupper($this->getAttribute()->getAttributeCode());
$indexes[$index] = ['type' => 'index', 'fields' => [$this->getAttribute()->getAttributeCode()]];
return $indexes;
}
/**
* @param int $store
* @return \Magento\Framework\DB\Select|null
*/
public function getFlatUpdateSelect($store)
{
return $this->eavAttrEntity->create()->getFlatUpdateSelect($this->getAttribute(), $store);
}
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à magento.stackexchange