1

I've made a product attribute "inbound" and it has two options, yes and no, I have assigned "no" to it initially but when I'm checking out I'm using an observer to change its value to "yes" programmatically if product quantity less then 100, everything is working fine but can't able to change its value to yes.

Thanks in advance

Kishor Thummar
  • 3,000
  • 1
  • 9
  • 18
nihal malik
  • 491
  • 4
  • 12
  • Add the code where you are setting the attribute value. – Hamendra Sunthwal Apr 10 '20 at 12:19
  • i just want to know how to set attribute value programatically,cant show the code bt ihave tried " setCustomAttribute($attributeCode, $attributeValue) " function using Magento\Framework\Api\CustomAttributesDataInterface dependency its giving error – nihal malik Apr 10 '20 at 12:21
  • Please refer this link :- https://magento.stackexchange.com/questions/192160/magento-2-set-custom-product-attribute-value-programmatically-on-product-save 2> https://community.magento.com/t5/Magento-2-x-Technical-Issues/Unable-to-update-custom-product-attribute-value-programatically/td-p/112739 – Mohit Patel Apr 10 '20 at 12:46

2 Answers2

2

Here is how you can set attribute value $product = $this->productRepository->getById($productId); $product->setData($attributeCode, $attributeValue); $this->productRepository->save($product);

Hamendra Sunthwal
  • 2,488
  • 10
  • 39
1

Thanks everyone for the answers, it really helps, i got the right way to do that, rather using setcustomasttribute function cause its throwing errors.you can just simply do this.

$product = $this->productRepository->getById($productId);
$product->setInbound($attributeValue);
$product->setData('store_id', 0);//remember to set store id other wwise data 
mismatch occurs//
$this->productRepository->save($product);
nihal malik
  • 491
  • 4
  • 12