0

I want to add something with the product name dynamically while adding to 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 mini-cart

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;
    }
 }
7
  • Have you try that solution provided into that URL? Commented Sep 30, 2021 at 4:39
  • yes, I have tried but it's updated all the product name with the name not the updated the only last item
    – tanu
    Commented Sep 30, 2021 at 5:02
  • Can you share your code. Commented Sep 30, 2021 at 5:04
  • Dhiren I have updated the code please check
    – tanu
    Commented Sep 30, 2021 at 5:20
  • Have you debug that you will get productId data properly on this one? Commented Sep 30, 2021 at 5:27

0

Your Answer

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