2

How to do this? I am using checkout_cart_product_add_after event.

EDIT:

After I did it like this

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
  <event name="sales_quote_item_set_product">
      <observer name="changesku" instance="Vdndor\ProductChanger\Observer\ChangeSKU" />
  </event>
</config>

and Observer as

<?php

namespace Vendor\ProductChanger\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;


class ChangeSKU implements ObserverInterface
{
    
      /**
     * Below is the method that will fire whenever the event runs!
     *
     * @param Observer $observer
     */
    public function execute(Observer $observer)
    {
        
        $event = $observer->getEvent();
        $quoteItem = $event->getQuoteItem();

        $quoteItem->setSKU('SAMPLE');
        $quoteItem->setName("SAMPLE NAME");

        
    }
}

But still it is changing the SKU and NAME in quote_item table perfectly when I add it to cart. But whenever I go the cart page or I place the order the old NAME and SKU are saving back. any help?

2 Answers 2

2

Use the following one for checkout_cart_product_add_after event

class Mynamespace_Model_Observer
{
    public function salesQuoteItemSetProduct(Varien_Event_Observer $observer)
    {
        /* @var $item Mage_Sales_Model_Quote_Item */
        $item = $observer->getQuoteItem();

        //only change the item you want by product id
        if($item->getProductId() == "abc"){
            $item->setName('Custom Product Name');
        }

        return $this;
    }
}
1
  • Thanks @Mitul Vadalia... I tried ur approach for the same event you suggested too. but it is not changing the name permanently. That is the problem Commented Oct 13, 2022 at 10:33
0

You can try to add an around plugin on the save method of the following repositories:

  • \Magento\Quote\Api\CartItemRepositoryInterface
  • \Magento\Quote\Api\GuestCartItemRepositoryInterface

Something like:

public function aroundSave(
        \Magento\Quote\Api\CartItemRepositoryInterface $cartItemRepository, 
        callable $proceed, 
        \Magento\Quote\Api\Data\CartItemInterface $cartItem
    ) {
        $cartItem->setSku('YOUR SKU');
        return $proceed;
    }

Another solution is to add a preference for the same repositories, inherit original class and add a new implementation for the save method.

2

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.