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.