Magento – Get last added product in minicart magento2

addtocartcheckoutmagento2

After product is added to cart, I want to open minicart and mark it with some styles. I found http://magento.stackexchange.com/questions/141507/magento-2-open-minicart-when-item-is-added how to open it. But dont know how to get last added.

Best Answer

I havent tried it out, just to give you a picture. you can try this out in your block:

This is to get the last product item added to cart.

public function __construct(
    \Magento\Checkout\Model\Session $checkoutSession,
    array $data = []
 ) {
    $this->_checkoutSession = $checkoutSession;
}  

$productID=$this->_checkoutSession->getLastAddedProductId(true);
echo $productID."<br>";  
Related Topic