Set a lineItem option from within a plugin? I’m trying to set a specific lineItem option. Here is the gist of my attempt:

craft()-&gt;on('commerce_lineItems.onPopulateLineItem', function($event){

    $lineItem = $event-&gt;params['lineItem'];

    if (something == true) {
        $lineItem-&gt;options['optionKey'] = 'newValue';
    }

});


This returns an error:


  Indirect modification of overloaded property…


What’s the correct way to do this?
I'm guessing you're doing this in a plugin and not the front end using an input for a reason?

I struggled with this recently and had luck using the following:

// Get the line item from the event
$item = $event-&gt;params['lineItem'];

// We get all the options from the post request
$options = craft()-&gt;request-&gt;getPost('options', []);

// Add the new value
$options['optionKey'] = 'newValue';

// Save the options to the line item
$item-&gt;options = $options;


If somebody can suggest an improvement to this I'd be grateful.