5

I want to update product custom attribute when custom module is upgraded. Attribute code is product_brand and i want to set it's is_used_for_promo_rules to 1. Any help appreciated.

5 Answers 5

6

I used following code

<?php

namespace Custom\Module\Setup; 
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Eav\Setup\EavSetup; 

class UpgradeData implements UpgradeDataInterface 
{
    public function __construct(
        EavSetup $eavSetupFactory
        ) 
    { 
        $this->eavSetupFactory = $eavSetupFactory; 
    } 

    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { 
        $setup->startSetup();
        $this->eavSetupFactory->updateAttribute(4,135,'is_used_for_promo_rules',1,null); 
        $setup->endSetup(); 
    } 

}

in updateAttribute 4 is entity type id and 135 is attributer id.

3
  • is your solution worked?
    – Amit Bera
    May 2, 2018 at 12:54
  • @AmitBera yes it worked, upgraded module version and run setup:upgrade. May 2, 2018 at 13:45
  • excellent.Where the issue ? Only parameters of update function ?
    – Amit Bera
    May 2, 2018 at 13:46
2

It’s pretty easy when you have the eavSetup in your setup module. It’s only necessary to execute:

$eavSetup->updateAttribute(Product::ENTITY, $attributeCode, $attributeField, $value);

For example, if I want to change the ‘position’ field value of the ‘price’ product attribute:

$eavSetup->updateAttribute('catalog_product', 'price', 'position', 100);
2
  • 1
    Where did $eavSetup come from?
    – Mageician
    Jun 19, 2020 at 19:56
  • @Mageician inject Magento\Eav\Setup\EavSetupFactory in the constructor and then use $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
    – P. Sopacua
    Nov 14, 2022 at 9:50
1

For this ,you have to create UpgradeData script at your custom module.

On this upgradedata script you need to inject Magento\Eav\Setup\EavSetup class and this class has function updateAttribute() which can update is_used_for_promo_rules field value to 1.

4
  • It will not work, I did try this a weeks ago. If you have another solution You are Most Welcome.
    – anonymous
    May 2, 2018 at 8:18
  • can u please share your code which have tried
    – Amit Bera
    May 2, 2018 at 8:21
  • codeshare.io/5gNdlR Check here
    – anonymous
    May 2, 2018 at 8:38
  • Hi i found this updateAttribute($entityTypeId, $id, $field, $value = null, $sortOrder = null) function and called in upgradeData script like $this->eavSetupFactory->updateAttribute(4,135,array('is_used_for_promo_rules' => 1),null,null); this code but it is not working. May 2, 2018 at 9:48
1

Try below UpgradeData.php script.

<?php

namespace Vendor\CustomModule\Setup;

use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Eav\Setup\EavSetup;

class UpgradeData implements UpgradeDataInterface
{
    /**
     * @var \Magento\Eav\Model\ResourceModel\Entity\Attribute
     */
    protected $_eavAttribute;


    public function __construct(
        EavSetup $eavSetupFactory,
        \Magento\Eav\Model\ResourceModel\Entity\Attribute $eavAttribute
    ) {
        $this->eavSetupFactory = $eavSetupFactory;
        $this->_eavAttribute = $eavAttribute;
    }

    /**
     * @param ModuleDataSetupInterface $setup
     * @param ModuleContextInterface $context
     */

    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        if (version_compare($context->getVersion(), '1.0.1', '<')) {
            $setup->startSetup();
            $entityType = $this->eavSetupFactory->getEntityType(\Magento\Customer\Model\Customer::ENTITY);
            $entityTypeId = $entityType['entity_type_id'];
            $attributeId = $this->_eavAttribute->getIdByCode(\Magento\Customer\Model\Customer::ENTITY, 'prefix');
            $this->eavSetupFactory->updateAttribute($entityTypeId, $attributeId, 'is_required', 0, null);
            $setup->endSetup();
        }
    }
}
0

Follow these steps to update the attributes of the products

use Magento\Catalog\Model\ResourceModel\Product\Action;

class UpdateAttributes
{

    protected $_updateAction;

public function __construct(

        Action $action

)
{

        $this->_updateAction = $action;

}

public yourfunction(){

$this->_updateAction->updateAttributes($productId,
['is_used_for_promo_rules'=>1],0);

}

Your Answer

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

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