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,0301 gold badge11 silver badges19 bronze badges
asked Apr 10, 2020 at 12:19
3
  • Add the code where you are setting the attribute value. Commented Apr 10, 2020 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 Commented Apr 10, 2020 at 12:21
  • Please refer this link :- magento.stackexchange.com/questions/192160/… 2> community.magento.com/t5/Magento-2-x-Technical-Issues/… Commented Apr 10, 2020 at 12:46

2 Answers 2

3

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);
answered Apr 14, 2020 at 12:26
2

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

answered Apr 10, 2020 at 12:24
8
  • you are using $this->productRepository->getById($productId); so it means i can use Magento\Catalog\Model\ProductRepository dependency ?? Commented Apr 10, 2020 at 12:27
  • Yes you can set product attribute , by loading product's object and then use set method to set attribute value and then save. Commented Apr 10, 2020 at 12:28
  • Try and let us know if its helpful and works , give upvote on the answer if it works 4 you, so it will help to others in Magento community. Commented Apr 10, 2020 at 13:16
  • Worked or not ? Commented Apr 11, 2020 at 12:47
  • hey there, my attribute is multiselect and its adding a new row for the attribute.if a product already has a attribute value selected like "no" if i tried to make it "yes" using your code its working but its not eliminating the "no" it sets both values yes and no for the product Commented Apr 12, 2020 at 14:51

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.