2

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.

2

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.

  • is your solution worked? – Amit Bera May 2 '18 at 12:54
  • @AmitBera yes it worked, upgraded module version and run setup:upgrade. – Nitin Pawar May 2 '18 at 13:45
  • excellent.Where the issue ? Only parameters of update function ? – Amit Bera May 2 '18 at 13:46
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.

  • It will not work, I did try this a weeks ago. If you have another solution You are Most Welcome. – Vivek May 2 '18 at 8:18
  • can u please share your code which have tried – Amit Bera May 2 '18 at 8:21
  • codeshare.io/5gNdlR Check here – Vivek May 2 '18 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. – Nitin Pawar May 2 '18 at 9:48
1

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);
0

Follow these steps to update the products attributes

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

your class{

    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, privacy policy and cookie policy

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