I'm working on several cart displays and to do this i usually (in magento 1) make a small script outside of Magento load up the magento app and fetch a collection with the data.

In magento2 this became a lot harder. I finally managed to get the cart contents inside of a sidebar phtml block with the following code:

$helper = $this->helper('\Magento\Checkout\Helper\Cart');
$quote = $helper->getQuote();
echo $quote->getId();
$quoteitems = $quote->getAllItems();
$cart= $helper->getCart();  
echo $quote->getExtShippingInfo();

$subtotal = $quote->getBaseSubtotal();

foreach ($quoteitems as $item){
  // Code to get contents per product

Pretty simple, but now i made a script outside of magento (just in a subfolder of the magento installation.

I made a folder scripts and added 2 files abstract.php and getCart.php

scripts/abstract.php looks likes this:

use \Magento\Framework\AppInterface as AppInterface;
use \Magento\Framework\App\Http as Http;

use Magento\Framework\ObjectManager\ConfigLoaderInterface;
use Magento\Framework\App\Request\Http as RequestHttp;
use Magento\Framework\App\Response\Http as ResponseHttp;
use Magento\Framework\Event;
use Magento\Framework\Filesystem;
use Magento\Framework\App\AreaList as AreaList;
use Magento\Framework\App\State as State;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Checkout\Helper\Cart as Cart;

abstract class AbstractApp implements AppInterface
    var $helper;

    public function __construct(
        \Magento\Framework\ObjectManagerInterface $objectManager,
        Event\Manager $eventManager,
        AreaList $areaList,
        RequestHttp $request,
        ResponseHttp $response,
        ConfigLoaderInterface $configLoader,
        Magento\Framework\App\State $state,
        Filesystem $filesystem,
        Cart $helper,
        \Magento\Framework\Registry $registry
    ) {
        $this->_objectManager = $objectManager;
        $this->_eventManager = $eventManager;
        $this->_areaList = $areaList;
        $this->_request = $request;
        $this->_response = $response;
        $this->_configLoader = $configLoader;
        $this->_state = $state;
        $this->_filesystem = $filesystem;
        $this->registry = $registry;

    public function launch()
        return $this->_response;

    abstract public function run();

    public function catchException(\Magento\Framework\App\Bootstrap $bootstrap, \Exception $exception)
        return false;

And getCart.php looks like:

require dirname(__FILE__) . '/../app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
require dirname(__FILE__) . '/abstract.php';

class getCart extends AbstractApp

    public function run()

            $cart= $helper->getCart();  


$app = $bootstrap->createApplication('getCart');

When i run this i get errors like

Area code not set: Area code must be set before starting a session.

I can not get this to work. The reason i did not add that to the title since i am also open for other solutions.

Adding content like: $state->setAreaCode('frontend'); in the constructor did not seem to help at all.


1 Answer 1


I find a solution myself.

I created a file in the root of the magento dir /scripts/externalCart.php

use Magento\Framework\App\Bootstrap;
require __DIR__ . '/../app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);

$obj = $bootstrap->getObjectManager();

// Set the state (not sure if this is neccessary)
$state = $obj->get('Magento\Framework\App\State');

// Getting the object managers dependencies 
$quote = $obj->get('Magento\Checkout\Model\Session')->getQuote();
$helper = $obj->get('\Magento\Checkout\Helper\Cart');

// Get quote and cart items collection
$quote = $helper->getQuote();
$quoteitems = $quote->getAllItems();

// Get cart contents
$cart= $helper->getCart();  

// Getting the subtotal of the cart
$subtotal = $quote->getBaseSubtotal();
foreach ($quoteitems as $item)

    echo $item->getName();
    echo $item->getQty();
    echo number_format($item->getPrice(),2);

  • Sir the above method only return me the data of cart on cart page on other pages it return me NULL... how can I get data on other pages as well
    – Asad Khan
    Oct 8, 2019 at 11:12

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