3

I’m trying to set a specific lineItem option. Here is the gist of my attempt:

craft()->on('commerce_lineItems.onPopulateLineItem', function($event){

    $lineItem = $event->params['lineItem'];

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

});

This returns an error:

Indirect modification of overloaded property…

What’s the correct way to do this?

2

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->params['lineItem'];

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

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

// Save the options to the line item
$item->options = $options;

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.