I want display the amount of items in the cart outside of magento. Thus I created this script inside a scripts folder in the magento directory:
<?php require __DIR__ . '/../../app/bootstrap.php'; $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER); /** @var \Magento\Framework\App\Http $app */ $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'); echo json_encode(['items' => $helper->getItemsQty()]);
But for an unknown reason this suddenly just returns zero. I did not update the version so I think it might be some setting maybe?
How do I get the amount of items in the cart outside of magento?
PS: I know there are several answers already but I tried them all and they did not work either for different reasons like errors or so.
This seems to be some sort of Magento problem. After a night I called the page again and there is this strange behavior:
- the cart amount on the magento page still shows a number and the popover shows my items although I did not setup persistent cart but I set the session time to a high value
- if I go to the cart it is empty
- the script still shows zero
Now if I clear my browser data and call magento and the script again both display zero (as there is nothing in the cart for real). If I then add items to the cart both display the correct value.