Sep 13, 2024 5 min read
app/code/SMG/CustomAddProductAttribute/Setup/InstallData.php
<?php
namespace SMG\CustomAddProductAttribute\Setup;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
class InstallData implements InstallDataInterface
{
private $eavSetupFactory;
public function __construct(
EavSetupFactory $eavSetupFactory
) {
$this->eavSetupFactory = $eavSetupFactory;
}
}
<?php
public function install(
ModuleDataSetupInterface $setup,
ModuleContextInterface $context
) {
}
Here are all lines code of InstallData.php to create product attribute programmically.
<?php
namespace SMG\CustomAddProductAttribute\Setup;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
class InstallData implements InstallDataInterface
{
private $eavSetupFactory;
public function __construct(EavSetupFactory $eavSetupFactory)
{
$this->eavSetupFactory = $eavSetupFactory;
}
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
$eavSetup->addAttribute(
Magento\Catalog\Model\Product::ENTITY,
'sample_attribute',
[
'type' => 'text',
'backend' => '',
'frontend' => '',
'label' => 'SMG Custom Attribute',
'input' => 'text',
'class' => '',
'source' => '',
'global' => Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => false,
'default' => '',
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'used_in_product_listing' => true,
'unique' => false,
'apply_to' => ''
]
);
}
}
Refer the following link to see the several attribute properties.
After created sample_attribute attribue to update the this attribute’s properties if needed. UpgradeData.php is required to create and upgrade module schema_version.
<?php
namespace SMG\CustomAddProductAttribute\Setup;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Eav\Setup\EavSetupFactory;
class UpgradeData implements UpgradeDataInterface
{
private $eavSetupFactory;
public function __construct(EavSetupFactory $eavSetupFactory)
{
$this->eavSetupFactory = $eavSetupFactory;
}
public function upgrade(ModuleDataSetupInterface $setup,ModuleContextInterface $context)
{
if (version_compare($context->getVersion(), "1.0.1", "<"))
{
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
$eavSetup->updateAttribute(
Magento\Catalog\Model\Product::ENTITY,
'sample_attribute',
'frontend_label',
'SMG Custom Attribute'
);
}
if (version_compare($context->getVersion(), "1.0.2", "<"))
{
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
$eavSetup->updateAttribute(
Magento\Catalog\Model\Product::ENTITY,
'sample_attribute',
'frontend_label',
'Custom Attribute'
);
}
}
}