Add product to cart using code in Craft Commerce plugin 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()-&gt;getCarts()-&gt;getCart();

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

But I am not sure how to add this product to the existing cart using a code.
I managed to figure this out. Below is the 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);
}