Skip to main content
added 4 characters in body
Source Link

Create after plugin for Magento\Checkout\Controller\Cart\Add.php

namespace Vendor\Module\Plugin;

class AddToCart
{
public function __construct(
       \Magento\Checkout\Model\Session  $checkoutSession
    ) {
       $this->checkoutSession = $checkoutSession;
    }

public function afterExecute(\Magento\Checkout\Controller\Cart\Add $subject, $result)
    {
        $items = $this->checkoutSession->getQuote()->getAllItems();
        $max = 0;
        $lastItem = null;
        foreach ($items as $item){
            if ($item->getId() > $max) {
                $max = $item->getId();
                $lastItem = $item;
            }
        }
        if ($lastItem){
            $lastAddedProductName = $lastItem->getName();
        }

        return $result;

    }
}

Create after plugin for Magento\Checkout\Controller\Cart\Add

namespace Vendor\Module\Plugin;

class AddToCart
{
public function __construct(
       \Magento\Checkout\Model\Session  $checkoutSession
    ) {
       $this->checkoutSession = $checkoutSession;
    }

public function afterExecute(\Magento\Checkout\Controller\Cart\Add $subject, $result)
    {
        $items = $this->checkoutSession->getQuote()->getAllItems();
        $max = 0;
        $lastItem = null;
        foreach ($items as $item){
            if ($item->getId() > $max) {
                $max = $item->getId();
                $lastItem = $item;
            }
        }
        if ($lastItem){
            $lastAddedProductName = $lastItem->getName();
        }

        return $result;

    }
}

Create after plugin for Magento\Checkout\Controller\Cart\Add.php

namespace Vendor\Module\Plugin;

class AddToCart
{
public function __construct(
       \Magento\Checkout\Model\Session  $checkoutSession
    ) {
       $this->checkoutSession = $checkoutSession;
    }

public function afterExecute(\Magento\Checkout\Controller\Cart\Add $subject, $result)
    {
        $items = $this->checkoutSession->getQuote()->getAllItems();
        $max = 0;
        $lastItem = null;
        foreach ($items as $item){
            if ($item->getId() > $max) {
                $max = $item->getId();
                $lastItem = $item;
            }
        }
        if ($lastItem){
            $lastAddedProductName = $lastItem->getName();
        }

        return $result;

    }
}
Source Link

Create after plugin for Magento\Checkout\Controller\Cart\Add

namespace Vendor\Module\Plugin;

class AddToCart
{
public function __construct(
       \Magento\Checkout\Model\Session  $checkoutSession
    ) {
       $this->checkoutSession = $checkoutSession;
    }

public function afterExecute(\Magento\Checkout\Controller\Cart\Add $subject, $result)
    {
        $items = $this->checkoutSession->getQuote()->getAllItems();
        $max = 0;
        $lastItem = null;
        foreach ($items as $item){
            if ($item->getId() > $max) {
                $max = $item->getId();
                $lastItem = $item;
            }
        }
        if ($lastItem){
            $lastAddedProductName = $lastItem->getName();
        }

        return $result;

    }
}