1

I would like to change the price of the bundle product items after its has been added to cart and recalculate the bundle product price and changing the bundle item price.

I was thinking of using the checkout_cart_product_add_after event but I am not sure how to modify the bundle item prices.

I have seen some of the answers here but they show how to modify the bundle product price not the bundle item prices

Shopping Cart Snapshot

5

So finally I was able to achieve this, first we need a events.xml

For my requirement I needed this event observer for both frontend and adminhtml so the events.xml was create at app/code/Foo/CustomPrice/etc/events.xml.

<?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="foo_customprice_observer_set_price_for_item_add" instance="Foo\CustomPrice\Model\Observer\SetPriceForItem"/>
    </event>
    <!-- Event for update add to cart -->
    <event name="checkout_cart_product_update_after">
        <observer name="foo_customprice_observer_set_price_for_item_update" instance="Foo\CustomPrice\Model\Observer\SetPriceForItem"/>
    </event>
</config>

After that need a observer at app/code/Foo/CustomPrice/Model/Observer/SetPriceForItem.php

<?php
namespace Foo\CustomPrice\Model\Observer;

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

class SetPriceForItem implements ObserverInterface
{
    /**
     * Add Special Price on add to cart.
     *
     * @param Observer $observer
     * @return SetPriceForItem
     *
     */
     public function execute(Observer $observer)
     {
        /** @var $item \Magento\Quote\Model\Quote\Item */
        $item = $observer->getEvent()->getQuoteItem();
        if ($item->getProduct()->getTypeId() == Type::TYPE_BUNDLE) {
            foreach ($item->getQuote()->getAllItems() as $bundleitems) {
                /** @var $bundleitems\Magento\Quote\Model\Quote\Item */
                //Skip the bundle product
                if ($bundleitems->getProduct()->getTypeId() == Type::TYPE_BUNDLE) {
                    continue;
                }
                $bundleitems->setCustomPrice(1.00);
                $bundleitems->setOriginalCustomPrice(1.00); 
                $bundleitems->getProduct()->setIsSuperMode(true);   

            }
            $item->getProduct()->setIsSuperMode(true);
        }
        return $this;
     }//end execute()
}
  • Can we change bundle items price ? suppose I want to change "Sprite Foam Roller" price from $19.00 to $20.00 at the time of addtocart. So it can be possible ? – Krupali Oct 3 '16 at 6:18
  • @Krupali Yes the code in the answer only updates the bundle item prices and not the bundle product itself. – Atish Goswami Oct 3 '16 at 11:55
  • Actually I am doing same thing in M1. There I got the value of "$item->getProduct()->getTypeId()" as a 'simple' .But as per your logic it has to get Bundle as I am adding a bundle product. Means it is getting last selected bundle item of Parent bundle product. – Krupali Oct 3 '16 at 12:13
  • @AtishGoswami Can we change bundle items price ? suppose I want to change "Sprite Foam Roller" price from $19.00 to $20.00 at the time of addtocart. So it can be possible ? Can you please clarify how to achieve it? – Abhishek Panchal Jan 2 '18 at 22:03
  • we change price of simple product by using this code.But in my case,This product is part of bundle product.After that,when we add bundle product in cart it show error "We can't add this item to your shopping cart right now. ". @AtishGoswami – Lovely Setia Nov 22 '18 at 7: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.