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.
Magento – Get last added product in minicart magento2
addtocartcheckoutmagento2
Related Solutions
Have you overridden or extended your minicart.js
script?
2.1 introduced some changes to these files to resolve previous issues with the minicart values not updating on page load. Assuming you have overridden this file or extended some of its functions, you'll have to bring it in line with the changes that were introduced with 2.1.
Here's the commit with the relevant changes:
https://github.com/magento/magento2/commit/eec3cf17bc5deae71c83c797d1339de46cf6905f
Namely, cart()
was replaced with call to new function getCartParam()
.
Hope this helps resolve your issue!
For Magento 2
Pop up minicart when I add a product to the cart magento 2
I'm referring to Open header cart when product added to cart & https://stackoverflow.com/questions/13841390/toggle-dropdown-mini-cart-when-product-added-to-basket which are in Magento 1.
In Magento 2 we can create Event Observer for this
app\code\Custom\Module\etc\frontend\events.xml
<?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="controller_action_predispatch_checkout_cart_add">
<observer name="open_mini_cart" instance="Custom\Module\Observer\OpenMiniCartObserver" />
</event>
</config>
app\code\Custom\Module\Observer\OpenMiniCartObserver.php
namespace Custom\Module\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Framework\App\ObjectManager;
class OpenMiniCartObserver implements ObserverInterface {
protected $_urlManager;
protected $redirect;
public function __construct(
\Magento\Framework\UrlInterface $urlManager, \Magento\Framework\App\Response\RedirectInterface $redirect
) {
$this->_urlManager = $urlManager;
$this->redirect = $redirect;
}
public function execute(\Magento\Framework\Event\Observer $observer) {
$actionName = $observer->getEvent()->getRequest()->getFullActionName();
$controller = $observer->getControllerAction();
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cartUpdated = $objectManager->get('Magento\Checkout\Model\Session')->getCartWasUpdated(false);
if ($cartUpdated) {
// open mini cart
}
$this->redirect->redirect($controller->getResponse(), $this->redirect->getRefererUrl());
}
Still Some JS Stuff missing
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.