0

I'm having this issue when i am making an order programatically and i am setting them to 0 the price, this can be made with simple, virtual and even configurable products with this code:

$cartItem = $cart->addProduct($product, $objParam);
$cartItem->setCustomPrice(0.0);
$cartItem->setOriginalCustomPrice(0.0);
$cartItem->getProduct()->setIsSuperMode(true);
$cartItem->save();

The issue is that from here i can't have with bundles the subtotals with 0: enter image description here

As i was saying... the subtotal sets normally with 0 with simples, virtuals and configurables, but with Bundles is not happening :( But when I do that with Bundle Products is not setting to 0... Does anyone can help me with this issue please ? I'll appreciated it

Thanks,

1 Answer 1

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

After that need an observer at

app/code/Namespace/CustomPrice/Model/Observer/SetPriceForItem.php

<?php
namespace Namespace\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) {
                    $bundleitems->setCustomPrice(1.00);
                    $bundleitems->setOriginalCustomPrice(1.00); 
                    $bundleitems->getProduct()->setIsSuperMode(true);
                }   
            }
            $item->setCustomPrice(0);
            $item->setOriginalCustomPrice(0);
            $item->getProduct()->setIsSuperMode(true);
        }
        return $this;
     }
}
8
  • HI there, i added the observer and also in events but i still have the subtotal not with 0 in the orders: Product Name SKU Price Qty Subtotal Sprite Yoga Companion Kit 24-WG080-24-WG081-blue-24-WG084-24-WG085-24-WG088 $0.00 $42.09 Commented Aug 27, 2018 at 15:07
  • can you add a screenshot of your cart page and code of your observer file? Commented Aug 27, 2018 at 15:08
  • The idea here is have the subtotal with 0 but its not happening :( Commented Aug 27, 2018 at 15:08
  • I've updated the question Abhishek ... with the bundles in Orders view there you have it the screenshot Commented Aug 27, 2018 at 15:12
  • My observer is like you did, but the difference is this: if ($bundleitems->getProduct()->getTypeId() == Type::TYPE_BUNDLE) { $bundleitems->setCustomPrice(0); $bundleitems->setOriginalCustomPrice(0); $bundleitems->getProduct()->setIsSuperMode(true); } Commented Aug 27, 2018 at 15:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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