<p>Commerce will by default combine line items that have the same hash value for the options (that is, the values of the items chosen options are run through <code>md5</code> to get a hash - if the hash matches, the lineItems quantity is increased as the item is seen as the same item.</p>

<p>If you want line items with the same options to remain as separate line items, then you can simply add another unique option to the product as you add it to the cart (you don't need to display this of course).</p>

<p><code>&lt;input type="hidden" name="options['whatevs']" value="somethingRandom"&gt;</code></p>

<p>Twig isn't actually ideal at generating random things I believe, I'd personally create a template variable in my Business Logic plugin that returns a random value, e.g. something like:</p>

<pre><code>public function random(){
    srand(time());
    return rand(1,time()); 
}
</code></pre>

<p>You can e.g. call the current timestamp in twig but there's always a chance that might not end up as random I suppose?</p>

<p><code>&lt;input type="hidden" name="options['time']" value="{{ date().timestamp }}"&gt;</code></p>

<p>(Calling twigs own random (<a href="https://twig.sensiolabs.org/doc/2.x/functions/random.html" rel="nofollow noreferrer">https://twig.sensiolabs.org/doc/2.x/functions/random.html</a>) - doesn't seem ideal as that's inclusive so you might see the edge values returned more than you'd like).</p>

CC BY-SA 3.0
2017-06-01T09:17:38.107Z
Thanks, this is working and does create new line items rather than combine them. However, if I set `$lineItem->price` dynamically each time using the  `onPopulateLineItem` event, it updates the price for that line item and the line items already in place linked to that product. I need the line items already in place to keep the price set on them when they were added and not inherit from the latest line item. Hopefully that makes sense.
Rich Mud
CC BY-SA 3.0
2017-06-01T10:21:35.237Z
I think you might need to chat to Luke Holder on that one. Probably means the hashing is happening for this scenario at the wrong time I'd guess. Not sure you'll be able to solve that without Luke... At least he'll have some good advice on it, I'm sure.
Jeremy Daalder
CC BY-SA 3.0
2017-06-01T04:16:59.267Z
2017-06-01T05:43:34.350Z
2017-06-01T05:43:34.350Z
Jeremy Daalder
<p>I'm using <strong>onPopulateLineItem</strong> in my plugin to dynamically set the price of products added to the cart based on user input on the front-end -</p>

<pre><code>$lineItem = $event-&gt;params['lineItem'];

$size = craft()-&gt;request-&gt;getPost('size');
$colour = craft()-&gt;request-&gt;getPost('colour');
$finish = craft()-&gt;request-&gt;getPost('finish');

$options = array(
  'size' =&gt; $size,
  'colour' =&gt; $colour,
  'finish' =&gt; $finish
);

$lineItem-&gt;options = $options;
$lineItem-&gt;price = $dynamicPrice;
</code></pre>

<p>This is working fine, however, if the same product item is added to the cart but with different options (and therefor a different price) the existing line item is overwritten with the new values/price.</p>

<p>Is there a way I can create a new line item each time, rather than overwrite the existing one? Do I need to use a different event or create some kind of custom controller?</p>

<p>Thanks!</p>

<plugin-craftcommerce>
(Commerce) Multiple line items with the same purchasable ID?
# (Commerce) Multiple line items with the same purchasable ID?

(Asked by: username_0 on 2017-05-31)

I'm using onPopulateLineItem in my plugin to dynamically set the price of products added to the cart based on user input on the front-end -
$lineItem = $event->params['lineItem'];

$size = craft()->request->getPost('size');
$colour = craft()->request->getPost('colour');
$finish = craft()->request->getPost('finish');

$options = array(
  'size' => $size,
  'colour' => $colour,
  'finish' => $finish
);

$lineItem->options = $options;
$lineItem->price = $dynamicPrice;

This is working fine, however, if the same product item is added to the cart but with different options (and therefor a different price) the existing line item is overwritten with the new values/price.
Is there a way I can create a new line item each time, rather than overwrite the existing one? Do I need to use a different event or create some kind of custom controller?
Thanks!

**Tags:** plugin-craftcommerce


---

## 1 answers

### Answer by username_1 on 2017-06-01. Score: 4 | Accepted answer

Commerce will by default combine line items that have the same hash value for the options (that is, the values of the items chosen options are run through md5 to get a hash - if the hash matches, the lineItems quantity is increased as the item is seen as the same item.
If you want line items with the same options to remain as separate line items, then you can simply add another unique option to the product as you add it to the cart (you don't need to display this of course).

Twig isn't actually ideal at generating random things I believe, I'd personally create a template variable in my Business Logic plugin that returns a random value, e.g. something like:
public function random(){
    srand(time());
    return rand(1,time()); 
}

You can e.g. call the current timestamp in twig but there's always a chance that might not end up as random I suppose?

(Calling twigs own random (https://twig.sensiolabs.org/doc/2.x/functions/random.html) - doesn't seem ideal as that's inclusive so you might see the edge values returned more than you'd like).

> **Comments:**
> **username_0**: Thanks, this is working and does create new line items rather than combine them. However, if I set `$lineItem->price` dynamically each time using the  `onPopulateLineItem` event, it updates the price for that line item and the line items already in place linked to that product. I need the line items already in place to keep the price set on them when they were added and not inherit from the latest line item. Hopefully that makes sense.  
> **username_1**: I think you might need to chat to Luke Holder on that one. Probably means the hashing is happening for this scenario at the wrong time I'd guess. Not sure you'll be able to solve that without Luke... At least he'll have some good advice on it, I'm sure.  

---

