1

I am trying to get a collection of all items in the cart for current session but the array is returning [0].

My block:

<?php
namespace GGG\PaymentMethodsManagment\Block;
class GetProductsInCart extends \Magento\Framework\View\Element\Template
{    
    protected $_cart;    
    protected $_checkoutSession;    

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Checkout\Model\Cart $cart,
        \Magento\Checkout\Model\Session $checkoutSession,
        array $data = []
    )
    {
        $this->_cart = $cart;
        $this->_checkoutSession = $checkoutSession;

        parent::__construct($context, $data);
    }

    public function getCart()
    {        
        return $this->_cart;
    }

    public function getCheckoutSession()
    {
        return $this->_checkoutSession;
    }

}

In my template file:

<?php
    $quote = $block->getCheckoutSession()->getQuote();
    $items = $quote->getAllItems();
    var_dump($items);
?>

var_dump($items) is printing array(0) { }.

Can anyone help?

UPDATE:

All suggestions from the current answer:

<?php
    $quote = $block->getCheckoutSession()->getQuote();
    \Zend_Debug::dump($quote);
    $getItemsCollection = $quote->getItemsCollection();
    $itemCountgetItemsCollection = count( $getItemsCollection );
    echo "getItemsCollection():";
    \Zend_Debug::dump($itemCountgetItemsCollection);

    $getItems = $quote->getItems();
    $itemCountgetItems = count( $getItems );
    echo "getItems():";
    \Zend_Debug::dump($itemCountgetItems);

    $getAllItems = $quote->getItems();
    $itemgetAllItems = count( $getAllItems );
    echo "getAllItems():";
    \Zend_Debug::dump($itemgetAllItems);

    $getAllVisibleItems = $quote->getAllVisibleItems();
    $itemCountgetAllVisibleItems = count( $getAllVisibleItems );
    echo "getAllVisibleItems():";
    \Zend_Debug::dump($itemCountgetAllVisibleItems);

?>

All of the abode return int(0):

enter image description here

2 Answers 2

1

I think you should simply extend from Magento\Checkout\Block\Cart.

<?php
namespace GGG\PaymentMethodsManagment\Block;
class GetProductsInCart extends \Magento\Checkout\Block\Cart //\Magento\Framework\View\Element\Template
{ 

Now, you can simply call $this->getItems() and this should give you all the items.

Hope this helps.

10
  • Still can't get it to work... Can you give me a working example? :)
    – Greg
    Dec 9, 2018 at 22:45
  • Can you tell me what does it return on <?php echo get_class($block);?> in your template file? And also how is the template file called? Dec 9, 2018 at 22:59
  • It returns GGG\PaymentMethodsManagment\Block\GetProductsInCart also, the file is called using XML and added to content: <block class="GGG\PaymentMethodsManagment\Block\GetProductsInCart" name="get_products_in_cart" template="GGG_PaymentMethodsManagment::get_product_in_cart.phtml" /> --------- Any solution/tutorial/guide/method would be okey for me. I am struggling for 3 days now...
    – Greg
    Dec 9, 2018 at 23:06
  • 1
    Got it... I don't know why but when I navigate to http://127.0.0.1/magento2/PaymentMethodsManagment/Index/ it works, but I was looking at http://127.0.0.1/magento2/PaymentMethodsManagment/Index/Index... Spend hours trying to figure that out.
    – Greg
    Dec 9, 2018 at 23:33
  • 1
    I am not sure, but I guess it is something to do with layout file and block call. For each controller file/action, we need separate xml to reference properly. Dec 9, 2018 at 23:44
0

Block GetProductsInCart

<?php
namespace GGG\PaymentMethodsManagment\Block;
class GetProductsInCart extends \Magento\Framework\View\Element\Template
{    
    protected $_cart;    
    protected $_checkoutSession;    

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Checkout\Model\Cart $cart,
        \Magento\Checkout\Model\Session $checkoutSession,
        array $data = []
    )
    {
        $this->_cart = $cart;
        $this->_checkoutSession = $checkoutSession;

        parent::__construct($context, $data);
    }

    public function getCart()
    {        
        return $this->_cart;
    }

    public function getCheckoutSession()
    {
        return $this->_checkoutSession;
    }

}

Template:

<?php
    $quote = $block->getCheckoutSession()->getQuote();
    $items = $quote->getAllVisibleItems();
    $itemCount = count( $items );
    \Zend_Debug::dump($itemCount)
?>

You can retrieve the items in several ways, which is basically the same as in Magento 1:

  • $quote->getItemsCollection()
    

    Returns a quote item collection with all items associated to the current quote.

  • $quote->getItems()
    

    This is a shortcut for the method above, but if there is no quote it returns an empty array, so you cannot rely on getting a collection instance.

  • $quote->getAllItems()
    

    Loads the item collection, then returns an array of all items which are not marked as deleted (i.e. have been removed in the current request).

  • $quote->getAllVisibleItems()
    

    Loads the item collection, then returns an array of all items which are not marked as deleted AND do not have a parent (i.e. you get items for bundled and configurable products but not their associated children). Each array item corresponds to a displayed row in the cart page.

Choose what fits your needs best. In most cases the last method is what you need.

1
  • Thank you for your interest, please see my updated answer. They all seem to deliver nothing... :/
    – Greg
    Dec 9, 2018 at 20:17

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.