1

I have created product programmatically but when i try to update attribute value with code:

$product->setData($attributeCode, '')->save();

It won't change. The update will work only after i have saved the product in Magento admin GUI. What might be the reason of that?

Code I use to create product:

  $_product->setName($productName)
    ->setId($productID)
    ->setAttributeSetId($attributeSetId)
    ->setStoreId($storeId)
    ->setVisibility(4)
    ->setStatus(1)
    ->setWebsiteIds(array(1)) 
    ->setSku($Sku)
    ->setPrice($price)
    ->setDescription($description)
    ->setShortDescription($shortDescription)
    ->setStockData(array(
         'use_config_manage_stock' => 0,
         'is_in_stock' => $stock,
         'qty' => $qty
    ))
    ->setUrlKey($urlKey)
    ->setCategoryIds(array($CategoryId))
    ->setTypeId("simple")
    ->setData($attributeCode, 5)
    ->save();

Editing script

`$_product->load($productID)->setStoreId($storeId)->setData($attributeCode, 'Blue')->save();`

$_product is an instance of \Magento\Catalog\Model\Product.

1 Answer 1

0

The reason for that may your attribute code isn't correct. You should check the attribute code again: STORES > Attributes > Product. You also need to reindex the database and clear your Magento Cache to see the result after updating the attribute.

8
  • How is it possible that when i am creating the product all seems to be ok and the attribute appears in the right place? The problem is about updating the attribute value. Sep 10, 2016 at 7:47
  • You created new product via php script also? Sep 10, 2016 at 7:52
  • Yes, of course. Sep 10, 2016 at 7:54
  • Please, update your question again with the scripts for creating and editing? Sep 10, 2016 at 7:55
  • Please, update your all code lines on your question above, that because it's easy for community following your question. Sep 10, 2016 at 8:08

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.