Certain products I have in my store have an attribute called setup_fee_sku.

If the product has this attribute I grab the value (which is a sku) load it, and add it to the cart programatically.

But the price is always set to 0 the first time its added, no matter what kind of sku it is.


After the item is in the cart for the first time, any other time it is added it contains the correct price.


I am doing my logic in an observer for event checkout_cart_add_product_complete

This is what my codes looks like

$setupSku = $product->getSetupFeeSku();
//Add setup fee to cart if it exists
if($setupSku != null){
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $setupProduct = $objectManager->create('Magento\Catalog\Api\ProductRepositoryInterface')->get($setupSku);
    $formKey = $objectManager->create('\Magento\Framework\Data\Form\FormKey')->getFormKey();

    $params = array(
        'formKey' => $formKey,
        'product' => $setupProduct->getId(),
        'qty' => 100

    $this->cart->addProduct($setupProduct, $params);

You can try using checkout_cart_product_add_after event.


<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="checkout_cart_product_add_after">
        <observer name="sr_checkout_cart_product_add_after" instance="SR\MagentoCommunity\Observer\CheckoutCartProductAddAfter" />


namespace SR\MagentoCommunity\Observer;

use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;

class CheckoutCartProductAddAfter implements ObserverInterface
     * @var ProductRepositoryInterface
    private $productRepository;

     * CheckoutCartProductAddAfter constructor.
     * @param ProductRepositoryInterface $productRepository
    public function __construct(
        ProductRepositoryInterface $productRepository
    ) {
        $this->productRepository = $productRepository;

     * @param EventObserver $observer
     * @return $this
    public function execute(EventObserver $observer)
        try {
            $product = $observer->getEvent()->getData('product');
            $setupSku = $product->getSetupFeeSku();
            if ($setupSku) {
                $quoteItem = $observer->getEvent()->getData('quote_item');
                $quote = $quoteItem->getQuote();
                $product = $this->productRepository->get($setupSku);
                $requestInfo = [
                    'product' => $product->getId(),
                    'qty' => 1
                $request = new \Magento\Framework\DataObject($requestInfo);
                $quote->addProduct($product, $request);

        } catch (\Exception $e) {
        return $this;
  • 2
    @PrathapGunasekaran What's wrong with this answer? Check my answer for avoiding 0 for first time. – Sohel Rana Mar 14 '19 at 4:30
  • Amazing! Thank you @SohelRana – Alex Donnelly Mar 14 '19 at 15:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.