cancel
Showing results for 
Search instead for 
Did you mean: 

Unable to update custom product attribute value programatically

Unable to update custom product attribute value programatically

Hello, I have created a few custom product attributes in Magento 2.2.6 and have no problem saving a new product with these new attributes, load this created product, and update the standard Magento attributes. But if I try to update a custom attribute, it just will not update the value. It won't even throw an error, it looks like everything went fine, but the value did not change.

 

Example custom attribute creation:

$eavSetup = $this->eavSetupFactory->create();
$eavSetup->addAttribute(
    \Magento\Catalog\Model\Product::ENTITY,
    'protein', [
        'group'      => $this->attributeGroupName,
         type'       => 'varchar',
        'label'      => 'Protein',
        'input'      => 'text',
        'required'   => false,
        'visible'    => true,
        'sort_order' => 50,
        'global'     => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE
    ]
);

Creating and updating product:

$product = $this->productFactory->create();
$product = $product->loadByAttribute('sku', $data['sku']) ?: $product;

$product->setSku($data['sku']);
$product->setName($data['name']);
$product->setProtein($data['protein']);
// $product->setData('protein', $data['protein']);

$this->productRepository->save($product);

If anyone has any idea why the value of the custom attributes won't update, I would be grateful!

12 REPLIES 12

Re: Unable to update custom product attribute value programatically

Hello @lee_peuker,

 

Official method should work, but Not
The official method setCustomAttribute($attributeCode, $attributeValue) below should work but NOT because of the official bug https://github.com/magento/magento2/issues/4703.

The bug doesn't fix yet in version 2.2.3.

$product = $this->productRepository->getById($productId);
$product->setCustomAttribute($attributeCode, $attributeValue);
$this->productRepository->save($product);


Temporary method
After many debug and tries, I found the method $product->setData($attributeCode, $attributeValue) below could set custom attribute successfully, it is a temporary method.

Hope it could help you. Any better way is welcome to post Smiley Happy

$product = $this->productRepository->getById($productId);
$product->setData($attributeCode, $attributeValue);
$this->productRepository->save($product);

--
If my answer is useful, please Accept as Solution & give Kudos

Re: Unable to update custom product attribute value programatically

Hey @gelanivishal, thank you very much for your answer. I allready tried your suggested solutions, but they did not work either. I tried changing my custom attributes the following ways:

$product->setProtein($data['protein']);
$product->setCustomAttribute('protein', $data['protein']); $product->setData('protein', $data['protein']);

If I return the value before saving with $product->getProtein() I get the changed value, so this works. But as soon as I save the product, the attribute value resets to the value before I changed it.

Re: Unable to update custom product attribute value programatically

Hi lee_peuker,

 

Did you ever solve this issue or find any workaround? I am still facing this issue in M2.2.6

Re: Unable to update custom product attribute value programatically

Anybody found solution to this yet? I am facing the same issue in magento v2.3.0

Re: Unable to update custom product attribute value programatically

Hello @gelanivishal 

 

Can u give me proper solution?

 

I am facing same issue in Magento 2.3.1.

 

Thanks


Magento Developer
Ankita Biswas

Re: Unable to update custom product attribute value programatically

Use saveAttribute() method to save custom product attribute.

sample attribute best_price

$mypro=1001;
$productRepository=$objectManager->get('\Magento\Catalog\Api\ProductRepositoryInterface'); $product =$productRepository->;getById($mypro);

$product->setBestPrice(0);
$product->getResource()->saveAttribute($product, 'best_price');

 

 

Re: Unable to update custom product attribute value programatically

 $productId = $this->productFactory->create()->getIdBySku('24-MB01');
 $product = $this->productFactory->create()->load($productId);

 $product->addAttributeUpdate('custom_attribute', 'EEE', 0);

This works for me 2.3.2. I have to get the product id first. Using loadByAttribute('sku','xxx') would result to Integrity constraint violation. Thus, I need to load the product model using product id.

Re: Unable to update custom product attribute value programatically

Hi, @lee_peuker @Ankita Biswas @manizstha 

i used this to set special price programmatically and that work for me 

 

$val = $product->getPrice();

$product->setSpecialPrice($val);
$product->getResource()->saveAttribute($product, 'special_price');

 

here 'special_price' is an attribute similarly you can set your custom attribute.

if it works for you then accept this as solution and give kudos.

Re: Unable to update custom product attribute value programatically

This seems to work for me too! I've seen a million ways of people advising how to get this to work, and this is the only one that actually works.

Thanks a bunch!