0

I am using Porto theme. In addition to the available mini cart sidebar, I'd like to create a custom block on the right side with a sticky cart, so customers would be able to see what they add and what they wanna remove from the there cart (without clicking on the mini cart icon in the header). Similar to this: enter image description here

I tried to use the solution recommended here: Magento 2 get cart items in block class I don't know why it is not working. I'm attaching the code below.

1) BDK/Mycartblock/Block/ShoppingCart.php

<?php
namespace BDK\Mycartblock\Block;
use \Magento\Framework\View\Element\Template;

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');
    }
}

2) BDK/Mycartblock/etc/module.xml

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="BDK_Mycartblock" schema_version="1.0.0" setup_version="1.0.0" >
    </module>
</config>

3) BDK/Mycartblock/view/frontend/layout/default.xml

<?xml version="1.0"?>                                                     
 <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
        <referenceContainer name="content">
           <block class="BDK\Mycartblock\Block\ShoppingCart" name="mycartblock" template="BDK_Mycartblock::my-template.phtml">
            </block>
        </referenceContainer>
    </body>
</page>

4) BDK/Mycartblock/view/frontend/templates/my-template.phtml

<?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/>";
}
?>

Pls, help find the solution. Thanks to all in advance!

  • Exactly where you want to display this? – Ranganathan Sep 30 at 4:28
  • I created CMS page and listed different groups of products by using product list widget. I’d like to locate the sticky cart on the right side of this CMS page. I am going to create a sticky sidebar and insert this block into it. – arman_p Sep 30 at 5:46
0

you were called your my-template.phtml in default.xml...instead of this...call you phtml below the widget in cms page as below code

 {{block class="BDK\Mycartblock\Block\ShoppingCart" Template="BDK_Mycartblock::my-template.phtml"}} 
  • Yes, this is exactly how I tried to call this block after installing it. But it didn’t work. I added couple of items to the cart, they have appeared in my mini cart in the header, but not in the place where I put this widget. Didn’t show any errors either, just empty. – arman_p Sep 30 at 6:16
  • Is your widget add to cart is AJAX add to cart? – Ranganathan Sep 30 at 6:21
  • yes, it supports ajax. here is an example how I listed products: {{block class="Magento\Catalog\Block\Product\ListProduct" name="category.products.list" as="product_list" category_id="27" template="product/list.phtml"}} – arman_p Sep 30 at 15:37
  • Did you got product in your sidebar on page load? – Ranganathan Oct 1 at 4:31
  • Do you mean the mini cart sidebar in the header? Yes, all the products appear in the mini cart in the header as soon as they got added to the cart. It is only mycartblock (created with the code above) which is not working and not reflecting any products added. Do you have any idea why it is not working? – arman_p Oct 1 at 16:07

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.