Skip to main content
4 of 5
Update Descriptions
Dhiren Vasoya
  • 9.5k
  • 12
  • 35
  • 59

Magento 2 : Update the product name while adding into the cart

I want to add something with product name dynamically while adding into the cart. Each product have different concatenation with product name. And that product name should be updated on everywhere like cart page, checkout, email, invoice, in sales grid etc. I have followed the step from below link. But product name has been updated in whole quote items not only the last added item. Magento 2 change productname adding to cart

namespace vendor\modulename\Plugin\Quote\Model\Quote;


class Item 

{
   public function afterSetProduct(
     \Magento\Quote\Model\Quote\Item $subject,
      $result
   ){

      $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
      $cookieManager = $objectManager->get('Magento\Framework\Stdlib\CookieManagerInterface');
    $product_name = $cookieManager->getCookie('proname');
    $product_Id = $cookieManager->getCookie('proid');
    $productId =  $subject->getProductId();
    if($productId == $product_Id){
     $subject->setName($product_name);
     return $subject;
    }
}

}

For minicart

namespace vendor\modulename\Plugin\Checkout\CustomerData;

 class DefaultItem    
 {

    public function aroundGetItemData(
     \Magento\Checkout\CustomerData\AbstractItem $subject,
        \Closure $proceed,
        \Magento\Quote\Model\Quote\Item $item
    ){
        
         $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $cookieManager = $objectManager->get('Magento\Framework\Stdlib\CookieManagerInterface');
        $product_name = $cookieManager->getCookie('proname');
        $pro_id = $cookieManager->getCookie('proid');
        $logger->info('text message11212 default');
        $data = $proceed($item);
        if($proceed($item)['product_id'] == $pro_id){       
         $data['product_name'] = $product_name;
        }else{
            $data['product_name'] = $data['product_name'];
        }       
        return $data;    
    }
}

For cart page

namespace vendor\modulename\Plugin\Checkout\Block\Cart\Item;

class Renderer

{

    public function afterGetProductName(

         \Magento\Checkout\Block\Cart\Item\Renderer $subject,
        $result
    ){

        $ $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $cookieManager = $objectManager->get('Magento\Framework\Stdlib\CookieManagerInterface');
        $product_name = $cookieManager->getCookie('proname');
        $pro_id = $cookieManager->getCookie('proid');       
        return $product_name;
    }
 }
tanu
  • 22
  • 5