Magento 2: After Customer Login Get Cart Information?
-
08-10-2020 - |
質問
Customer has already Items in Cart. He is logout.
I'm using customer_login
observer. It is correct?
How to get customers all cart information after his successfull login?
namespace Custom\Module\Observer;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\ObjectManager;
class CustomerLoginAfter implements ObserverInterface {
protected $checkoutSession;
protected $cart;
public function __construct(\Magento\Checkout\Model\Session $checkoutSession, \Magento\Checkout\Model\Cart $cart) {
$this->checkoutSession = $checkoutSession;
$this->cart = $cart;
}
public function execute(Observer $observer) {
$quoteItemsAll = $this->checkoutSession->getQuote()->getAllItems();
echo count($quoteItemsAll);
exit;
return $this;
}
}
It gives 0 count. While I already have 6 Items in Cart.
How to get customer's previous quote & it's items?
解決
First, you should add Magento_Checkout
as a depend module for your moule Custom_Module
.
This depend tag use for as at customer_login
event magento merge current quote(Quote which is created before login ) and already existing code(active quote which exists in customer account).
This depends make an <sequence>
tag make sequence in execution of this two module ,First fire checkout module then your module means you will get merge quote object
<sequence>
<module name="Magento_Checkout"/>
</sequence>
Then at your observer ,you can get item qty by below code:
$this->checkoutSession->getQuote()->getItemsQty() * 1
And to get all visible items:
$quoteItemsAll = $this->checkoutSession->getQuote()->getAllVisibleItems();
他のヒント
namespace Custom\Module\Observer;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\ObjectManager;
class CustomerLoginAfter implements ObserverInterface {
protected $quote;
protected $quoteItem;
public function __construct(\Magento\Quote\Model\Quote $quote, \Magento\Quote\Model\Quote\Item $quoteItem) {
$this->quote = $quote;
$this->quoteItem = $quoteItem;
}
public function execute(Observer $observer) {
$customer = $observer->getEvent()->getCustomer();
$customerId = $customer->getId();
$quoteAll = $this->quote->getCollection()->addFieldToFilter('customer_id', $customerId)->addFieldToFilter('is_active', 1)->getData();
echo "<pre>";
print_r($quoteAll);
$quoteItemAll = $this->quoteItem->getCollection()->addFieldToFilter('quote_id', $quoteAll[0]['entity_id'])->getData();
echo "<pre>";
print_r($quoteItemAll);
exit;
return $this;
}
}
Please let me know if have better solution
<?php
namespace Vendor\MagentoCustomer\Observer;
use Magento\Framework\Event\ObserverInterface;
class CustomerLogin implements ObserverInterface {
/**
* @param \Magento\Checkout\Model\Session
*/
protected $checkoutSession;
/**
* @var \Magento\Quote\Api\CartRepositoryInterface
*/
protected $quoteRepository;
public function __construct(
\Magento\Checkout\Model\Session $checkoutSession,
\Magento\Quote\Api\CartRepositoryInterface $quoteRepository
) {
$this->checkoutSession = $checkoutSession;
$this->quoteRepository = $quoteRepository;
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
$customer = $observer->getEvent()->getCustomer();
$customerId = $customer->getId();
if($customerId){
//method 1
$quote= $this->checkoutSession->getQuote()->loadByCustomer($customerId);
$cartItems1=$quote->getAllItems();
echo count(cartItems1);echo '<br>';
//(OR) method 2
$customerQuote = $this->quoteRepository->getForCustomer($customerId);
$cartItems2=$customerQuote->getAllItems();
echo count($cartItems2);echo '<br>';
//(OR) method 3
$quoteId = $this->checkoutSession->getQuote()->getId();
$quote = $this->checkoutSession->getQuote()->load($quoteId);
$cartItems3=$quote->getAllItems();
echo count($cartItems3);
}
}
}
$this->checkoutSession->getQuote()->loadByCustomer($customerId);
OR
$this->quoteRepository->getForCustomer($customerId);
OR
$quote = $this->checkoutSession->getQuote()->load($quoteId);
All above methods can help you to fetch current customer cart items. Hope this helps.
declare observer
Demo/CustomerQuote/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_postdispatch_customer_account_loginPost">
<observer name="quote_cart" instance="Demo\CustomerQuote\Observer\QuoteObserver"/>
</event>
</config>
add observer class
Demo/CustomerQuote/Observer/QuoteObserver.php
<?php
namespace Demo\CustomerQuote\Observer;
use Magento\Checkout\Helper\Cart;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
class QuoteObserver implements ObserverInterface
{
protected $cartHelper;
public function __construct(
Cart $cartHelper
)
{
$this->cartHelper = $cartHelper;
}
/**
* @param Observer $observer
* @return void
*/
public function execute(Observer $observer)
{
$items = $this->cartHelper->getCart()->getItems();
// Use this instead counting items
$countedCartItems = $this->cartHelper->getSummaryCount();
\Zend_Debug::dump(count($items));
\Zend_Debug::dump($countedCartItems);
die;
}
}