0

I have the following code in an external file(same domain) to get the current cart details, and I'm getting all details without any issues.

The problem is, when I go to the checkout page using 'Go to checkout' and press browser back button this cart gets empty. I mean only on this external file.

When I click the 'view and edit cart' and uses the browser back button the cart is fine.

Any help would be appreciated!

<?php
ini_set("display_errors", true);
ini_set("error_reporting", E_ALL);

require '/var/www/html/app/bootstrap.php';

$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$app = $bootstrap->createApplication('Magento\Framework\App\Http');
$obj = \Magento\Framework\App\ObjectManager::getInstance();
$obj->get('Magento\Framework\App\State')->setAreaCode('frontend');

$storeManager = $obj->create("\\Magento\\Store\\Model\\StoreManagerInterface");
$storeManager->setCurrentStore(1);

$state = $obj->get('Magento\Framework\App\State');

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

$imagehelper = $obj->get('\Magento\Catalog\Helper\Image');

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

// Getting cart 
$cart= $shoppingcart->getCart();  

// Getting the subtotal of the cart
$subtotal = number_format($quote->getBaseSubtotal(), 2);
$qty = 0;

$arrProducts = array();
$mediaurl= $storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
$baseurl = $storeManager->getStore()->getBaseUrl();

$j = 0;
foreach ($quoteitems as $item)
{
    $arrProducts[$j]['productID'] = $item->getProductId();
    $arrProducts[$j]['productName'] = $item->getName();
    $arrProducts[$j]['productQty'] = $item->getQty();
    $arrProducts[$j]['productPrice'] = number_format($item->getPrice(),2);

    $qty += $item->getQty();

    $_product = $obj->get('Magento\Catalog\Model\Product')->load($item->getProductId());
    $arrProducts[$j]['productUrl'] = $_product->getUrlKey();
    $arrProducts[$j]['productImage'] = $mediaurl.'catalog/product'.$_product->getSmallImage();

    $j++;
} ?> 

<div class="CustomMiniCart">
<?php 
    if($qty > 0 && !empty($arrProducts)){ ?>
        <span class="miniCartWrap">
            Cart Subtotal: <b>$<?php echo $subtotal; ?></b>
            <span class="numItems"><b><?php echo count($arrProducts);?></b> Item(s) in Cart</span>
            <a href="https://example.com/checkout/" class="chk">GO TO CHECKOUT</a>
        <?php 
        foreach($arrProducts as $product){ ?>
            <span class="proWrap">
                <span class="left"><a href="https://example.com/<?php echo $product['productUrl'] ?>/"><img src="<?php echo $product['productImage'] ?>" /></a></span>
                <span class="right"><a href="https://example.com/<?php echo $product['productUrl']?>/"><?php echo$product['productName']?></a><br><br>
                    <b>$<?php echo$product['productPrice']?></b><br><br>Qty: <?php echo $product['productQty']?>
                </span>
            </span>
    <?php } ?>
        <span class="viewcart"><a href="https://example.com/checkout/cart/">VIEW AND EDIT CART</a></span>
        </span>
    <?php }else{ ?>
        <span class="emptyCart">You have no items in your shopping cart.</span>
   <?php  } ?>
</div>
0

In that case, your cache is Enable, so you can't get values in an external file.

Once you disable the cache, it will work fine

  • It returns to respective pages, but the cart is getting empty – ASR Apr 11 at 7:35
  • I can go to checkout page with cart and I placed an order. – ARUNPRABAKARAN M Apr 11 at 7:39
  • example.com/checkout/cart check your site address – ARUNPRABAKARAN M Apr 11 at 7:40
  • I'm not using example.com, using my site address. when you are on the checkout page try the browser back button, then the cart gets empty. Only on that particular page! – ASR Apr 11 at 7:44
  • The problem is, when I go to the checkout page using 'Go to checkout' and press browser back button this cart gets empty. I mean only on this external file. When I click the 'view and edit cart' and uses the browser back button the cart is fine. Read my Question please.. – ASR Apr 11 at 7:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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