<p>In Twig, you can do something like this to add purchasable id (n.b. not product id) - <code>123</code> to a cart:</p>
<pre><code>{% set cart = craft.commerce.carts.cart(true) %}
{% set lineItem = craft.commerce.lineitems.createLineItem(cart.id, 123, {&quot;note&quot;:&quot;Test note&quot;}, 2, &quot;&quot;) %}
{% do cart.addLineItem(lineItem) %}
{%  do craft.app.elements.saveElement(cart) %}
</code></pre>
<p>Thus the PHP equivalent should thus be (n.b. not actually tested):</p>
<pre><code>        // You need the cart, and actual purchasable Id of what you want to add...
        // Here was assume a product without variants...so get the default variant's id

        $cart = craft\commerce\Plugin::getInstance()-&gt;getCarts()-&gt;getCart();
        $product = \craft\commerce\elements\Product::find()-&gt;id([123])-&gt;one();
        $purchasableId = $product-&gt;getDefaultVariant()-&gt;id;

        // Create a new line item &amp; add it to the cart
    
        $newLineItem = \craft\commerce\Plugin::getInstance()-&gt;getLineItems()-&gt;createLineItem($cart-&gt;id, $purchasableId, [], 1, 'some note');
        $cart-&gt;addLineItem($newLineItem);

        // Save the updated cart...

        Craft::$app-&gt;getElements()-&gt;saveElement($cart);
</code></pre>

CC BY-SA 4.0
2020-09-18T02:45:44.123Z
2020-09-18T23:47:50.027Z
2020-09-18T23:47:50.027Z
Jeremy Daalder
<p>I managed to figure this out. Below is the code:</p>
<pre><code>$theOrder = $event-&gt;sender;
$myProduct = \craft\commerce\elements\Product::find()-&gt;id([$giftProductId])-&gt;one();
if($myProduct){
    $myPurchasable = $myProduct-&gt;getDefaultVariant();
    $newOrExistingLineItem = \craft\commerce\Plugin::getInstance()-&gt;getLineItems()-&gt;resolveLineItem($theOrder-&gt;id, $myPurchasable-&gt;id, []);
    $theOrder-&gt;addLineItem($newOrExistingLineItem);
}
</code></pre>

CC BY-SA 4.0
2020-09-18T02:42:15.133Z
2020-09-18T02:42:15.133Z
aslamdoctor
<p>I want to add a specific product to a cart using its ID. I have created a module which runs on <code>Order::EVENT_BEFORE_ADD_LINE_ITEM</code> event. Here I am able to capture the current cart using the below code</p>
<pre><code>$cart = craft\commerce\Plugin::getInstance()-&gt;getCarts()-&gt;getCart();
</code></pre>
<p>I can also get a Product using the below code</p>
<pre><code>$product = \craft\commerce\elements\Product::find()-&gt;id([123]); // here 123 is product id
</code></pre>
<p>But I am not sure how to add this product to the existing cart using a code.</p>

CC BY-SA 4.0
2020-09-17T19:24:33.690Z
I don't have a ready to use solution but you can look into the [`craft\commerce\models\LineItem` model](https://docs.craftcms.com/commerce/api/v3/craft-commerce-models-lineitem.html) and the [`craft\commerce\services\LineItems::createLineItem()` method](https://docs.craftcms.com/commerce/api/v3/craft-commerce-services-lineitems.html#method-createlineitem).
The cart is an `Order` instance and contains a collection of `LineItem`s. Each `LineItem` has a `Purchasable` id and `Product` implements `Purchasable`.
nstCactus
<craft3><plugin-craftcommerce>
Add product to cart using code in Craft Commerce plugin
# Add product to cart using code in Craft Commerce plugin

(Asked by: username_0 on 2020-09-17)

I want to add a specific product to a cart using its ID. I have created a module which runs on Order::EVENT_BEFORE_ADD_LINE_ITEM event. Here I am able to capture the current cart using the below code
$cart = craft\commerce\Plugin::getInstance()->getCarts()->getCart();

I can also get a Product using the below code
$product = \craft\commerce\elements\Product::find()->id([123]); // here 123 is product id

But I am not sure how to add this product to the existing cart using a code.

**Tags:** craft3, plugin-craftcommerce
> **Comments:**
> **username_1**: I don't have a ready to use solution but you can look into the [`craft\commerce\models\LineItem` model](https://docs.craftcms.com/commerce/api/v3/craft-commerce-models-lineitem.html) and the [`craft\commerce\services\LineItems::createLineItem()` method](https://docs.craftcms.com/commerce/api/v3/craft-commerce-services-lineitems.html#method-createlineitem). The cart is an `Order` instance and contains a collection of `LineItem`s. Each `LineItem` has a `Purchasable` id and `Product` implements `Purchasable`.  

---

## 2 answers

### Answer by username_2 on 2020-09-18. Score: 2

In Twig, you can do something like this to add purchasable id (n.b. not product id) - 123 to a cart:
{% set cart = craft.commerce.carts.cart(true) %}
{% set lineItem = craft.commerce.lineitems.createLineItem(cart.id, 123, {"note":"Test note"}, 2, "") %}
{% do cart.addLineItem(lineItem) %}
{%  do craft.app.elements.saveElement(cart) %}

Thus the PHP equivalent should thus be (n.b. not actually tested):
        // You need the cart, and actual purchasable Id of what you want to add...
        // Here was assume a product without variants...so get the default variant's id

        $cart = craft\commerce\Plugin::getInstance()->getCarts()->getCart();
        $product = \craft\commerce\elements\Product::find()->id([123])->one();
        $purchasableId = $product->getDefaultVariant()->id;

        // Create a new line item & add it to the cart
    
        $newLineItem = \craft\commerce\Plugin::getInstance()->getLineItems()->createLineItem($cart->id, $purchasableId, [], 1, 'some note');
        $cart->addLineItem($newLineItem);

        // Save the updated cart...

        Craft::$app->getElements()->saveElement($cart);


---

### Answer by username_0 on 2020-09-18. Score: 1

I managed to figure this out. Below is the code:
$theOrder = $event->sender;
$myProduct = \craft\commerce\elements\Product::find()->id([$giftProductId])->one();
if($myProduct){
    $myPurchasable = $myProduct->getDefaultVariant();
    $newOrExistingLineItem = \craft\commerce\Plugin::getInstance()->getLineItems()->resolveLineItem($theOrder->id, $myPurchasable->id, []);
    $theOrder->addLineItem($newOrExistingLineItem);
}


---

