0

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?

user2355278
  • 335
  • 5
  • 14

1 Answers1

1

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

Community
  • 1
  • 1
Renon Stewart
  • 17,426
  • 2
  • 44
  • 61
  • Unfortunately we are still on 1.7.2 on here so that didnt work! – user2355278 Oct 08 '14 at 15:40
  • I just test the code on v1.7.0 and it does work. What error are you getting? Did you add `echo $cartItemsCount` at the end ? – Renon Stewart Oct 08 '14 at 15:51
  • Yes - a 0 is what I get for some reason :( all other integrations are working fine with products/category it's just the cart/session bit.. makes me thing it's something to with the cookie – user2355278 Oct 08 '14 at 15:55
  • Is the domain that this code is been run the same at the magento site domain name? – Renon Stewart Oct 08 '14 at 15:56
  • Yes exact same domain but the magento install is within a /shop directory and the PHP file is in the domain root – user2355278 Oct 08 '14 at 15:56
  • Try moving the file to /shop and see if that make any different – Renon Stewart Oct 08 '14 at 15:59
  • That's a good test, and it works perfectly well within the /shop directory – user2355278 Oct 08 '14 at 16:29
  • Take a look at http://www.wikihow.com/Move-Magento-from-Subdirectory-to-Root-Directory-After-Upgrade-with-Cart2Cart cookie path is set to the root folder of your site and that the domain is correct – Renon Stewart Oct 08 '14 at 16:32
  • Yeah Cookie domain is www.mydomain.com (www. if that makes a difference?) and cookie path is just / – user2355278 Oct 08 '14 at 16:56
  • That should only make a difference on a subdomain level not on a subfolder level ... Take a look at http://stackoverflow.com/questions/576535/cookie-path-and-its-accessibility-to-subfolder-pages – Renon Stewart Oct 08 '14 at 17:05
  • What I would recommend is to accept this answer since it work and then ask a new question about setting cookie from a subfolder – Renon Stewart Oct 08 '14 at 18:06