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
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.
-
-
@AmitBera yes it worked, upgraded module version and run setup:upgrade. May 2, 2018 at 13:45
-
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);
-
1
-
@Mageician inject
Magento\Eav\Setup\EavSetupFactory
in the constructor and then use$this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
Nov 14, 2022 at 9:50
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. May 2, 2018 at 8:18
-
-
-
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
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();
}
}
}
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);
}