0

Ive tried to add 2 product attributes in Magento 2.3.5 programatically using install data. The attributes aren't being created and after some searching i found out that > 2.3.3 install and upgrade data where phased out for a declarative approach. Does anyone have any examples on how to do this outside of installdata/upgradedata? Here is what i was trying to add:

<?php
 
namespace XX\Pricing\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->removeAttribute(\Magento\Catalog\Model\Product::ENTITY, 'product_price_tier');
        $eavSetup->addAttribute(\Magento\Catalog\Model\Product::ENTITY, 'product_price_tier', [
            'group' => 'Product Price Tier',
            'type' => 'int',
            'sort_order' => 200,
            'backend' => '',
            'frontend' => '',
            'label' => 'Custom Yes/No',
            'input' => 'boolean',
            'class' => '',
            'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
            'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::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' => ''
        ]);
 
        $eavSetup->removeAttribute(\Magento\Catalog\Model\Product::ENTITY, 'product_price_family');
        $eavSetup->addAttribute(\Magento\Catalog\Model\Product::ENTITY, 'product_price_family', [
            'group' => 'Product Details',
            'type' => 'varchar',
            'backend' => '',
            'frontend' => '',
            'sort_order' => 220,
            'label' => 'Product Price Family',
            'input' => 'text',
            'class' => '',
            'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
            'visible' => true,
            'required' => false,
            'user_defined' => false,
            'default' => '',
            'searchable' => false,
            'filterable' => false,
            'comparable' => false,
            'unique' => false,
            'visible_on_front' => false,
            'used_in_product_listing' => true,
            'apply_to' => ''
        ]);
    }
}
0

You can create product attribute by using Magento default feature instead of going with programmatically.

1
  • I would rather the module create the attributes if the option is there as it will be used on many sites – gazZz Sep 4 '20 at 8:25
0

Try this.

<?php

namespace Foo\Bar\Setup\Patch\Data;

use Magento\Catalog\Model\Product;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;

class AddProductAttributeExample implements DataPatchInterface
{
    /**
     * @var ModuleDataSetupInterface
     */
    protected $moduleDataSetup;

    /**
     * @var EavSetupFactory
     */
    protected $eavSetupFactory;

    /**
     * AddProductAttributeExample constructor.
     *
     * @param ModuleDataSetupInterface $moduleDataSetup
     * @param EavSetupFactory $eavSetupFactory
     */
    public function __construct(
        ModuleDataSetupInterface $moduleDataSetup,
        EavSetupFactory $eavSetupFactory
    ) {
        $this->moduleDataSetup = $moduleDataSetup;
        $this->eavSetupFactory = $eavSetupFactory;
    }

    /**
     * @inheritDoc
     */
    public static function getDependencies()
    {
        return [];
    }

    /**
     * @inheritDoc
     */
    public function getAliases()
    {
        return [];
    }

    /**
     * @inheritDoc
     */
    public function apply()
    {
        $this->moduleDataSetup->startSetup();
        $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);

        $eavSetup->addAttribute(
            Product::ENTITY,
            'attribute_code',
            [
                // attribute params
            ]
        );

        $this->moduleDataSetup->endSetup();
        return $this;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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