Skip to content

Magento 2 | Adding values to the multiple select attribute of the product

I have created a product attribute in Magento2 which is of type multiple select.
Right now it is empty for all products and has no options created.
With my php code I am trying to add a random value to each product via foreach code.
This is the code:

foreach($simple_collection as $simple_product){
                            
                            $randomNumber = rand($minValue, $maxValue);
                            $simple_product->setdiscountprice($randomNumber);
                            $productResource->saveAttribute($simple_product, 'discountprice');
                            
                            echo 'discountprice aggiornato per: '.$simple_product->getSku().'<br/>';
                            
                        }

Why is it not working and not adding any kind of value to the multiple select attribute of the product?