1

For some purpose (something like popup) I'm trying to fetch cart details in minicart.phtml from Magento2\vendor\magento\module-checkout\view\frontend\templates\cart\minicart.phtml.

I directly modified core file just for checking purpose only. The point is cart details (from below code) are displaying only in cart page. It is not displaying in other pages like home page, product, category pages.I'm using default Magento EE 2.2.5 Luma theme. Any suggestion will be appreciated...

Below is my code (It's dirty, just for checking purpose),

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cart = $objectManager->get('\Magento\Checkout\Model\Cart'); 

// get quote items collection
$itemsCollection = $cart->getQuote()->getItemsCollection();

// get array of all items what can be display directly
$itemsVisible = $cart->getQuote()->getAllVisibleItems();

// get quote items array
$items = $cart->getQuote()->getAllItems();

foreach($items as $item) {
    echo 'ID: '.$item->getProductId().'<br />';
    echo 'Name: '.$item->getName().'<br />';
    echo 'Sku: '.$item->getSku().'<br />';
    echo 'Quantity: '.$item->getQty().'<br />';
    echo 'Price: '.$item->getPrice().'<br />';
    echo "<br />";            
}

I tried with block also through the constructor (like proper way) magento2ee\vendor\magento\module-checkout\Block\Onepage.php But same like working only in cart page. Not in other pages..

Update:

I'm just wondering below piece of code for fetching product details is working but not for cart.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
/** Apply filters here */
$collection = $productCollection->addAttributeToSelect('*')
            ->load();

foreach ($collection as $product){
     echo 'Name  =  '.$product->getName().'<br>';
     break;
}  
2
  • try with $objectManager->create instead of $objectManager->get
    – Keyur Shah
    Commented Jan 10, 2019 at 14:13
  • @KeyurShah Thanks for your comment, I tried with that also. But it doesn't work.
    – Elavarasan
    Commented Jan 11, 2019 at 7:26

3 Answers 3

2

Little Modifications in your code. Not sure but it should work I had same issue while ago.

Little modifications in your method.

You can give a try with Magento\Checkout\Model\Session.

1
  • Thanks for your comment, but I already tried with Session also. Still it doesn't work
    – Elavarasan
    Commented Jan 11, 2019 at 7:14
1

We are using below code on our custom phtml file which is located under

/view/frontend/templates/checkout/cart/custom.phtml

$om =   \Magento\Framework\App\ObjectManager::getInstance();

//Getting cart Item//

$quote = $om->create('Magento\Checkout\Model\Cart')->getQuote();
$quoteitems = $quote->getAllVisibleItems();

You can try this one.

2
  • I want to have with minicart.phtml so that it will available in all pages.
    – Elavarasan
    Commented Jan 11, 2019 at 9:03
  • The main reason of not showing the data on minicart at other pages are coming from cache and you need to alter the mini cart content by using standard Knockout JS concept. Cart page wont come from cache and system is fetching and showing your information on the fly of render. Please refer the below post which may give some better idea. webkul.com/blog/…
    – Thamo
    Commented Jan 11, 2019 at 12:26
1

Get cart items from customer local storage. And render it using knock out.

You can get Cart item details of the guest user also,

in your js file

define([
        'jquery',
        'knockout',
        'Magento_Customer/js/customer-data',
        'uiComponent'
    ], function ($, ko, customerData, Component) {
        return Component.extend({
            initialize: function () {
                self = this;
                self.cart = customerData.get('cart');

                let cartData = self.cart().items ? self.cart().items: [];

                cartData.map(function(item) {
                    console.log(item.product_id);
                    console.log(item.product_name);
                    console.log(item.product_sku);
                });

            },

        });
    });
4
  • Thanks for your comment. But I'm considering Guest users also.
    – Elavarasan
    Commented Jan 11, 2019 at 13:06
  • Bro you can get Cart item details of guest user also Commented Jan 11, 2019 at 13:27
  • Thanks it's working fine. And can you tell how to get quote id from this ? It's have Item id like 126 but quote id some thing like 8e57755dd722b331c26f2e500e61fc9a . Any Idea ?I need to get shipping estimates from API.. eg localhost:81/rest/default/V1/guest-carts/…
    – Elavarasan
    Commented Jan 14, 2019 at 7:12
  • Hi Elavarasan, if quote id is missing then add it by creating an after plugin of Magento\Checkout\CustomerData\Cart:: getSectionData Commented Jan 15, 2019 at 16:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.