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
-
Add the code where you are setting the attribute value.Hamendra Sunthwal– Hamendra Sunthwal2020年04月10日 12:19:47 +00:00Commented 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 errornihal malik– nihal malik2020年04月10日 12:21:27 +00:00Commented 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/…Mohit Patel– Mohit Patel2020年04月10日 12:46:43 +00:00Commented Apr 10, 2020 at 12:46
2 Answers 2
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);
Here is how you can set attribute value
$product = $this->productRepository->getById($productId);
$product->setData($attributeCode, $attributeValue);
$this->productRepository->save($product);
-
you are using $this->productRepository->getById($productId); so it means i can use Magento\Catalog\Model\ProductRepository dependency ??nihal malik– nihal malik2020年04月10日 12:27:29 +00:00Commented 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.Hamendra Sunthwal– Hamendra Sunthwal2020年04月10日 12:28:55 +00:00Commented 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.Hamendra Sunthwal– Hamendra Sunthwal2020年04月10日 13:16:45 +00:00Commented Apr 10, 2020 at 13:16
-
Worked or not ?Hamendra Sunthwal– Hamendra Sunthwal2020年04月11日 12:47:23 +00:00Commented 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 productnihal malik– nihal malik2020年04月12日 14:51:16 +00:00Commented Apr 12, 2020 at 14:51