<p>I'm guessing you're doing this in a plugin and not the front end using an input for a reason?</p>

<p>I struggled with this recently and had luck using the following:</p>

<pre><code>// 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;
</code></pre>

<p>If somebody can suggest an improvement to this I'd be grateful.</p>

CC BY-SA 3.0
2016-06-27T20:24:05.110Z
2016-06-27T20:24:05.110Z
Luke Pearce
<p>I’m trying to set a specific lineItem option. Here is the gist of my attempt:</p>

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

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

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

});
</code></pre>

<p>This returns an error:</p>

<blockquote>
  <p>Indirect modification of overloaded property…</p>
</blockquote>

<p>What’s the correct way to do this?</p>

<plugin-development><plugin-craftcommerce>
Set a lineItem option from within a plugin?
# Set a lineItem option from within a plugin?

(Asked by: username_0 on 2016-06-27)

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?

**Tags:** plugin-development, plugin-craftcommerce

---

## 1 answers

### Answer by username_1 on 2016-06-27. Score: 3 | Accepted answer

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.


---

