Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get shopping cart details in Magento2

Tags:

magento2

I know in Magento 1 you can get the shopping cart details on any page with:

$cart = Mage::getModel('checkout/cart')->getQuote();
foreach ($cart->getAllItems() as $item) {
    $productId = $item->getProduct()->getId();
    $productPrice = $item->getProduct()->getPrice();
}

How do I do the same thing in Magento 2?

like image 429
Jimmery Avatar asked Apr 22 '16 08:04

Jimmery


3 Answers

protected $_checkoutSession;

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

public function execute(\Magento\Framework\Event\Observer $observer)
{
  $cartData = $this->_checkoutSession->getQuote()->getAllVisibleItems();
  $cartDataCount = count( $cartData );
}

you can get the quote data in observer

like image 89
user2617403 Avatar answered Oct 19 '22 00:10

user2617403


I figured this out by myself in the end:

<?php

$om =   \Magento\Framework\App\ObjectManager::getInstance();
$cartData = $om->create('Magento\Checkout\Model\Cart')->getQuote()->getAllVisibleItems();
$cartDataCount = count( $cartData );

?>
<div class="bagDrop" id="bagDrop">
    <h4><a href="<?php echo $block->getShoppingCartUrl(); ?>">Quote Basket</a></h4>
    <?php if( $cartDataCount > 1 ): ?>
        <a href="#" class="arr up off" id="bagDropScrollUp"></a>
    <?php endif; ?>
    <div class="bagDropWindow">
    <?php if( $cartDataCount > 0 ): ?>
        <div class="bagDropList" id="bagDropList">
            <?php foreach( $cartData as $item ): ?>
                <?php $product = $item->getProduct(); ?>
                <?php $image = $product['small_image'] == '' ? '/pub/static/frontend/Clear/usb2u/en_GB/images/default-category-image_1.png' : '/pub/media/catalog/product' . $product['small_image']; ?>
                <a href="<?php echo $product['request_path']; ?>" class="bagDropListItem">
                    <img src="<?php echo $image; ?>">
                    <p>
                        <span class="name"><?php echo $product['name']; ?></span><br>
                        <span class="qty">x <?php echo $item->getQty(); ?></span>
                    </p>
                </a>
            <?php endforeach; ?>
        </div>
    <?php else: ?>
        <div class="emptyList">No products in your basket.</div>
    <?php endif; ?>
    </div>
    <?php if( $cartDataCount > 1 ): ?>
        <a href="#" class="arr dn" id="bagDropScrollDown"></a>
    <?php endif; ?>
</div>
like image 34
Jimmery Avatar answered Oct 19 '22 00:10

Jimmery


Get Product Details in Checkout Page

 <?php
    namespace namespace\modulename\Block\xxx;

    class xxx extends \Magento\Framework\View\Element\Template {
        public function __construct(
             \Magento\Checkout\Model\Cart $cart,
             \namespace\modulename\Model\CrossSellFactory $crosssell,
             \Magento\Framework\View\Element\Template\Context $context,
             \Magento\Customer\Model\Session $customerSession,
             \Magento\Framework\ObjectManagerInterface $objectManager,
             array $data = []
        ) {
             parent::__construct($context, $data);
             $this->cart = $cart;
             $this->_crosssell = $crosssell;
             $this->customerSession = $customerSession;
             $this->_objectManager = $objectManager;
        }
        public function getProductIds()
        {
             $productInfo = $this->cart->getQuote()->getItemsCollection();
             foreach ($productInfo as $item) {
                 $item[] = $item->getProductId();
                 echo"<pre>";print_r($item->getProductId());
             }
                 return $item;
        } 
    }

Put the above .php file in your block and return the value in phtml file like i shown below.

    <?php
    $Productdetails = $block->getProductIds();
    echo"<pre>";print_r($Productdetails->getName());
    ?>
like image 27
chris Avatar answered Oct 18 '22 22:10

chris