1

This question already has an answer here:

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
  $item->getName();
  $item->getQty();
  number_format($item->getPrice(),2);
}

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:

<?php
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
    ) {
        $state->setAreaCode('frontend');
        $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()
    {
        $this->run();
        return $this->_response;
    }

    abstract public function run();

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

And getCart.php looks like:

<?php
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');
$bootstrap->run($app);

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.

marked as duplicate by Raphael at Digital Pianism, Amit Bera, Qaisar Satti, Keyul Shah, Marius May 17 '16 at 6:19

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1

I find a solution myself.

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

<?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');
$state->setAreaCode('frontend');

// 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);

}

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