Magento 2: Get details of shopping cart all items, subtotal, grand total, billing and shipping address.

We can fetch details of all shopping cart items, subtotal and billing/shipping address from the current session.
Create Block file, ShoppingCart.php

<?php
namespace Rbj\Customer\Block;

class ShoppingCart extends \Magento\Framework\View\Element\Template
{
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Checkout\Model\Session $checkoutSession,
        array $data = []
    ) {
        $this->_checkoutSession = $checkoutSession;;
        parent::__construct($context, $data);
    }

    /**
     * Get quote object associated with cart. By default it is current customer session quote
     *
     * @return \Magento\Quote\Model\Quote
     */
    public function getQuoteData()
    {
        $this->_checkoutSession->getQuote();
        if (!$this->hasData('quote')) {
            $this->setData('quote', $this->_checkoutSession->getQuote());
        }
        return $this->_getData('quote');
    }
}

Call in template(.phtml) file, 

<?php
// Get all visible items in cart
$quote = $block->getQuoteData();

foreach($quote->getAllVisibleItems() as $_item) {
    //echo "<pre>";print_r($_item->debug());
    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 'Product Type: '.$_item->getProductType().'<br/>';
    echo 'Discount: '.$_item->getDiscountAmount();echo "<br/>";
    echo "<br/>";
}
// Get total items and total quantity in current cart
echo $totalItems = $quote->getItemsCount();echo "<br/>";
echo $totalQuantity = $quote->getItemsQty();echo "<br/>";

//Get subtotal and grand total of customer current cart
echo $subTotal = $quote->getSubtotal();echo "<br/>";
echo $grandTotal = $quote->getGrandTotal();echo "<br/>";

//Get billing and shipping addresses of current cart
$billingAddress = $quote->getBillingAddress();
echo "<pre>";print_r($billingAddress->getData());
$shippingAddress = $quote->getShippingAddress();
echo "<pre>";print_r($billingAddress->getData());

call function getQuoteData() and based on that we can get getAllVisibleItems() of cart.

Using getAllVisibleItems() we can get all the visible items data. For configurable product only main item data will be available, Child item data will be not display in for loop using getAllVisibleItems method.

If you want to get all the items, like for a configurable product, the main item with child item data, will be displayed if you use $quote->getAllItems() function. getAllItems() returns all the quote item data.

 

How to override cart/item/default.phtml in Magento 2?

For override default.phtml file in Magento 2, We need to create the plugin for it. We need to override Magento\Checkout\Block\Cart\AbstractCart Block file to override default.phtml in the module.
create the file at the location in your module.

I have created a module with Rbj/CartItem as {Namespace/Modulename},
app/code/Rbj/CartItem/etc/di.xml file,

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <!-- override cart/item/default.phtml file -->
    <type name="Magento\Checkout\Block\Cart\AbstractCart">
        <plugin name="item-test" type="Rbj\CartItem\Plugin\Cart\AbstractCart" sortOrder="1"/>
    </type>
</config>

Create plugin file at below location in your module,
app/code/Rbj/CartItem/Plugin/Cart/AbstractCart.php

<?php
namespace Rbj\CartItem\Plugin\Cart;
class AbstractCart
{
    /*
    *   Override cart/item/default.phtml file
    *   \Magento\Checkout\Block\Cart\AbstractCart $subject
    *   $result
    */
    public function afterGetItemRenderer(\Magento\Checkout\Block\Cart\AbstractCart $subject, $result)
    {
        $result->setTemplate('Rbj_CartItem::cart/item/default.phtml');
        return $result;
    }
}

Create template file in your module view folder,
app/code/Rbj/CartItem/view/frontend/templates/cart/item/default.phtml
Keep your content in default.phtml file to override Cart module default template.