cancel
Showing results for 
Search instead for 
Did you mean: 

Magento 2 - custom price can not add to subtotal and grand total after add to cart

Magento 2 - custom price can not add to subtotal and grand total after add to cart

I want to add custom price for bundle products after adding to cart. I have used "checkout_cart_product_add_after" observer to do so. But it is adding custom price to product price, not in sub total and grand total.
Following is my code:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<!-- Event for add to cart -->
<event name="checkout_cart_product_add_after">
<observer name="customprice_observer_set_price_for_item_add" instance="Custom\Shop\Model\Observer\SetPriceForItem"/>
</event>
</config>

Observer class:

namespace Custom\Shop\Model\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Catalog\Model\Product\Type;

class SetPriceForItem implements ObserverInterface
{
public function execute(Observer $observer)
{
$item = $observer->getEvent()->getQuoteItem();
if ($item->getProduct()->getTypeId() == Type::TYPE_BUNDLE) {
foreach ($item->getQuote()->getAllItems() as $bundleitems) {
if ($bundleitems->getProduct()->getTypeId() == Type::TYPE_BUNDLE) {
$bundle_price = $bundleitems->getProduct()->getFinalPrice();
$fee = $bundleitems->getProduct()->getBuildInFee();
$final_price = $bundle_price + $fee;
$bundleitems->setCustomPrice($final_price);
$bundleitems->setOriginalCustomPrice($final_price);
}
}

$item->getProduct()->setIsSuperMode(true);
}
return $this;
}
}

 Is there anyone who can help me? Price is not adding to subtotal and
 grand total.

Following is the cart item, subtotal and grand total.

Capture.PNG

1 REPLY 1

Re: Magento 2 - custom price can not add to subtotal and grand total after add to cart

Please check this post from stackoverflow

 

https://magento.stackexchange.com/questions/92774/how-to-add-fee-to-order-totals-in-magento2

Suman Kar(suman.jis@gmail.com) Magento Certified Developer Plus Skype: sumanphptech Problem solved? Please give 'Kudos' and accept 'Answer as Solution'.