1

I would like to add a custom attribute to the product and create an installData.php according to the guide. However, nothing changed after se:up. Please help.

app/code/My/Module/Setup/InstallData.php

<?php
namespace My\Module\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' => 'csutom_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' => ''
        ]
    );
}

public static function getDependencies()
{
    return [];
}

public function getAliases()
{
    return [];
}

public static function getVersion()
{
    return '1.0.0';
}

}

1 Answer 1

1

Did you create an attribute after creating the module and then run s:u to upgrade the database?

If it is, you need to change the setup version first to UpgradeSchema to create an attribute or manually delete the entry of your custom module from the database table name: setup_module.

To change version:

Go to vendor/module/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="<vendor>_<module>" setup_version="0.0.1">
    </module>
</config>

And change the setup version here. Or change the version to your static method.

public static function getVersion()
{
    return '1.0.0';
}

Thanks

4
  • I can't see the new attribute after s:u even changed the version as per your advice.
    – hill2011
    Commented Dec 4, 2022 at 15:25
  • Did you try to delete the entry table name: setup_module and reinstall your module? @hill2011 Commented Dec 5, 2022 at 10:11
  • I think just the delay and now it works. Thanks!
    – hill2011
    Commented Dec 5, 2022 at 10:18
  • That's good to know @hill2011 Commented Dec 5, 2022 at 10:20

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.