user2355278
user2355278

Reputation: 395

magento - access cart in external PHP file

Trying to access the magento cart from an external php file, I have loaded Mage and am able to access products & categories but for some reason I'm unable to access the cart information.

Shop is located in www.domain.com/shop/ PHP File is located in www.domain.com/file.php Magento cookie setting is set to '/'

I have looked at and tried many example of how to get the information and none of them have worked, the code I have at the moment is:

<?php
require_once '/home/admin/public_html/shop/appMage.php';                                                        
Mage::app();
Mage::getSingleton('checkout/cart', array('name' => 'frontend'));               
$cartItemsCount = Mage::getSingleton('checkout/cart')->getItemsCount();
$cartTotal = Mage::helper('checkout')->formatPrice(Mage::getSingleton('checkout/cart')->getQuote()->getGrandTotal()); 

echo 'You have '. $cartItemsCount . ' item(s) in your cart. <a class="cartgo" href="'.Mage::helper('checkout/cart')->getCartUrl().'">Checkout</a>';
if($cartTotal > 0){ echo '<span>[&pound;'.$cartTotal.']</span>'; }
echo '</a>';
?>

It works perfectly fine within the magento site but not from this external file for some reason. It returns a 0 even though there is a product in the cart.

Any pointers?

Upvotes: 0

Views: 1044

Answers (1)

MagePal Extensions
MagePal Extensions

Reputation: 17656

Try

// Mage init
require_once '../../app/Mage.php'; 
umask(0);  
Mage::init('default');
Mage::getSingleton('core/session', array('name' => 'frontend'));  

// Get customer session
$session = Mage::getSingleton('customer/session'); 

// Get cart instance
$cart = Mage::getSingleton('checkout/cart'); 
$cart->init();

$cartItemsCount = $cart->getItemsCount();

see magento 1.8 add product to cart using php

Upvotes: 1

Related Questions