2

I created one group product. In this, I have added 3 simple products. Now, I want to add 10$ plus in all 3 products when add to the cart. How can I do that?

I used this event checkout_cart_product_add_after

But, It's only updated 1 product. How can I update all product prices?

<?php

namespace Vendor\Module\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Event\Observer;
 
class UpdateAddToCart implements ObserverInterface
{     
    public function execute(Observer $observer)
    {
        /* Code here */
        $item = $observer->getEvent()->getData('quote_item');
        $item = ( $item->getParentItem() ? $item->getParentItem() : $item );
        $price = 100;
        $item->setCustomPrice($price);
        $item->setOriginalCustomPrice($price);
        $item->getProduct()->setIsSuperMode(true);
    }
}

Please help me. Thanks.

2 Answers 2

0

Try the following code

<?php

namespace Vendor\Module\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Event\Observer;
 
class UpdateAddToCart implements ObserverInterface
{     
    public function execute(Observer $observer)
    {
        $item = $observer->getEvent()->getQuoteItem();
        if ($item->getProduct()->getTypeId() == "grouped") {
            foreach ($item->getQuote()->getAllItems() as $groupeditems) {
                /** @var $groupeditems\Magento\Quote\Model\Quote\Item */
                //Skip the grouped product
                if ($groupeditems->getProduct()->getTypeId() == "grouped") {
                    continue;
                }
                $price = 100;
                $groupeditems->setCustomPrice($price);
                $groupeditems->setOriginalCustomPrice($price); 
                $groupeditems->getProduct()->setIsSuperMode(true);   

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

I haven't tested this code but I hope this will help you, let me know if you have queries

0

I also faced the same issue & to fix the issue, I have used below event.

checkout_cart_add_product_complete

The above event is providing the added associated items during add to cart.

Here is the event execution code. The code is updating the product custom attribute value. But in the same way you can update the associated products price.

  try {
        $quoteId = $this->checkoutSession->getQuoteId();
        $product = $observer->getProduct();
        $productId = $product->getId();
        $globalConfigFee = $this->carbonHelper->getGlobalCarbonFee();
        $params = $this->request->getParams();

        // Grouped Product
        $product = $observer->getProduct();
        if ($product->getTypeId() == "grouped") {
            $childOptions = $params['super_group'];
            foreach ($childOptions as $productId => $optionQty) {
                $where = ['quote_id = ?' => (int) $quoteId, 'product_id = ?' => (int) $productId];
                if ($optionQty > 0) {
                    $groupedProduct = $this->carbonHelper->initProduct($productId);
                    if ($groupedProduct->getCarbonFee() > 0) {
                        $this->resourceConnection->getConnection()->update("quote_item", ["carbon_fee" => $groupedProduct->getCarbonFee()], $where);
                    } else {
                        $this->resourceConnection->getConnection()->update("quote_item", ["carbon_fee" => $globalConfigFee], $where);
                    }
                }
            }
        }
    } catch (\Exception $e) {
        $this->logger->critical("During group product carbon charges addition: " . $e->getMessage());
    }

Note: I have used resource connection because this is the fastest way to update the values without extra objects creation & extra loops.

Also the above code is working & fully tested in Magento 2.4.6

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.