<p>You <em>could</em> write a custom Craft plugin (generate the boilerplate for it at <a href="https://pluginfactory.io" rel="nofollow noreferrer">https://pluginfactory.io</a>) - and in that listen to the <code>onPopulateLineItem</code> event.</p>

<p><a href="https://craftcommerce.com/docs/events-reference#commerce_lineitems.onpopulatelineitem" rel="nofollow noreferrer">https://craftcommerce.com/docs/events-reference#commerce_lineitems.onpopulatelineitem</a></p>

<p>Then, check the <code>purchasableId</code> of the item being added to the cart - if it is the TV, then add the cable to the cart at the same time.  Something like this (untested):</p>

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

    $purchasable= $event-&gt;params['purchasable'];
    $cart =  craft()-&gt;commerce_cart-&gt;getCart();

    if($purchasable-&gt;getPurchasableId() == 123){
        craft()-&gt;commerce_cart-&gt;addToCart($cart, 456);
    }
}
</code></pre>

<p>(See <code>addToCart()</code> in plugins/commerce/services/Commerce_CartService.php for the full function definition).</p>

<p>But the bigger question is <em>should</em> you do this....</p>

<p>Be aware that customers don't <em>generally</em> like this sort of behaviour in cart systems, and you should make sure that if you do do this, you at least take care to remove the accessories when they remove the main item from the cart.</p>

<p>Note that it soon gets complex, because can you be sure the accessory was added this way etc?</p>

<p>My advice is to instead use MultiAdd (<a href="https://github.com/engram-design/MultiAdd" rel="nofollow noreferrer">https://github.com/engram-design/MultiAdd</a>) to build a form that gives them the <em>option</em> of adding the accessories at the same time as the main product.  Like we do here for example:
<a href="https://imagescience.com.au/products/monitors/eizo-coloredge-cs2730" rel="nofollow noreferrer">https://imagescience.com.au/products/monitors/eizo-coloredge-cs2730</a></p>

CC BY-SA 3.0
2017-05-17T08:55:45.660Z
That makes sence. I know that you shouldn't force a product on a customer, but I was just to get an idea.
Beuschau
CC BY-SA 3.0
2017-05-17T09:07:50.877Z
That makes sense. I know that you shouldn't force a product on a customer, but I was just to get an idea. The actual idea is that I have a product that should include an insurance. So that product1 has an insurance1 and product2 has and insurance2 etc. The total price of the product should be product1+insurrence1. The customer should not see the added insurance product. But it would be nice that I could get a complete overview of the sold insurence products in the Cp. Does that make sense?
Beuschau
CC BY-SA 3.0
2017-05-17T09:12:55.300Z
It does but it's kind of a different question :) you could hide the extra products when showing the cart easily enough...and I guess show the summed price as if it's just the price for the main product. The code above hopefully helps you get started at least.
Jeremy Daalder
CC BY-SA 3.0
2017-05-17T09:14:19.287Z
yes - I'll give it a go. Thanks a million for your quick response :-)
Beuschau
CC BY-SA 3.0
2017-05-17T23:51:01.460Z
No probs.  Don't forget to accept the answer if it essentially solves your query.
Jeremy Daalder
CC BY-SA 3.0
2017-05-17T08:23:13.633Z
2017-05-17T08:23:13.633Z
Jeremy Daalder
<p>What would be the best way to automatically add a product when another product is added to the cart?</p>

<p>For example, if I where to add a TV to the cart, I would like to automatically add a HDMI cable as well. </p>

<plugin-craftcommerce>
Automatically add an insurrance for a product
# Automatically add an insurrance for a product

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

What would be the best way to automatically add a product when another product is added to the cart?

For example, if I where to add a TV to the cart, I would like to automatically add a HDMI cable as well.

**Tags:** plugin-craftcommerce

---

## 1 answers

### Answer by username_1 on 2017-05-17. Score: 2 | Accepted answer

You could write a custom Craft plugin (generate the boilerplate for it at https://pluginfactory.io) - and in that listen to the onPopulateLineItem event.
https://craftcommerce.com/docs/events-reference#commerce_lineitems.onpopulatelineitem
Then, check the purchasableId of the item being added to the cart - if it is the TV, then add the cable to the cart at the same time.  Something like this (untested):
craft()->on('commerce_lineItems.onPopulateLineItem', function($event){

    $purchasable= $event->params['purchasable'];
    $cart =  craft()->commerce_cart->getCart();

    if($purchasable->getPurchasableId() == 123){
        craft()->commerce_cart->addToCart($cart, 456);
    }
}

(See addToCart() in plugins/commerce/services/Commerce_CartService.php for the full function definition).
But the bigger question is should you do this....
Be aware that customers don't generally like this sort of behaviour in cart systems, and you should make sure that if you do do this, you at least take care to remove the accessories when they remove the main item from the cart.
Note that it soon gets complex, because can you be sure the accessory was added this way etc?
My advice is to instead use MultiAdd (https://github.com/engram-design/MultiAdd) to build a form that gives them the option of adding the accessories at the same time as the main product.  Like we do here for example:
https://imagescience.com.au/products/monitors/eizo-coloredge-cs2730

> **Comments:**
> **username_0**: That makes sence. I know that you shouldn't force a product on a customer, but I was just to get an idea.  
> **username_0**: That makes sense. I know that you shouldn't force a product on a customer, but I was just to get an idea. The actual idea is that I have a product that should include an insurance. So that product1 has an insurance1 and product2 has and insurance2 etc. The total price of the product should be product1+insurrence1. The customer should not see the added insurance product. But it would be nice that I could get a complete overview of the sold insurence products in the Cp. Does that make sense?  
> **username_1**: It does but it's kind of a different question :) you could hide the extra products when showing the cart easily enough...and I guess show the summed price as if it's just the price for the main product. The code above hopefully helps you get started at least.  
> **username_0**: yes - I'll give it a go. Thanks a million for your quick response :-)  
> **username_1**: No probs.  Don't forget to accept the answer if it essentially solves your query.  

---

