0

I have created block for sidebar.

I have added below code in my block for compare products. It's working fine

<div class="block widget block-compare">
    <div class="block-title">
    </div>
    {{block class="Magento\Framework\View\Element\Template" name="catalog.compare.sidebar" template="Magento_Catalog::product/compare/sidebar.phtml" /}}
</div>

I got output like this: http://prntscr.com/n4jcsj

Now i want to show shopping cart items there above compare. Can anyone help me with this.

I also try with minicart but it is not working as per my requirement. It gives me only link of minicart. I need to show content of minicart. I used this code in block.

<div class="minicart">
    <div> 
        {{block class="Magento\Checkout\Block\Cart\Sidebar" name="mini_cart" template="Magento_Checkout::cart/minicart.phtml"}}
    </div>
</div>

Which gives me output like this : https://prnt.sc/n3h35j

Please help me if anyone has some idea about this.

2

Create Block file

<?php
namespace Vendor\Module\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 '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/>";
}
  • i think issue in implementing – Rakesh Donga Mar 30 at 6:35
  • Hello.. your code works fine when i disable page cache. But when page cache is enable it doesn't show any item in cart. – Khooshbu Patel Mar 30 at 9:34
  • You can create extension for that and create block file also phtml file create like above my code – Rakesh Donga Mar 30 at 9:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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