Add product to cart using code in Craft Commerce plugin
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);
}