3

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.

https://imgur.com/a/WpXD8GH

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

https://imgur.com/a/5TqPfpL

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);
    $this->cart->save();
}
1

You can try using checkout_cart_product_add_after event.

app/code/SR/MagentoCommunity/etc/frontend/events.xml

<?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" />
    </event>
</config>

app/code/SR/MagentoCommunity/Observer/CheckoutCartProductAddAfter.php

<?php
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) {
            error_log($e->getMessage());
        }
        return $this;
    }
}
  • 2
    @PrathapGunasekaran What's wrong with this answer? Check my answer for avoiding 0 for first time. – Sohel Rana Mar 14 at 4:30
  • Amazing! Thank you @SohelRana – Alex Donnelly Mar 14 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.