2014-10-08 55 views
0

試圖從外部php文件訪問magento購物車,我已加載Mage,並且能夠訪問產品&類別,但由於某種原因,我無法訪問購物車信息。magento - 在外部PHP文件中訪問購物車

店位於www.domain.com/shop/ PHP文件位於www.domain.com/file.php 的Magento的cookie設置爲「/」

我已經看過並嘗試瞭如何獲取信息的許多例子,他們都沒有工作,我此刻的代碼是:

<?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>'; 
?> 

它完美罰款Magento站點內而不是從某些原因,這個外部文件。即使購物車中有產品,它也會返回0。

任何指針?

回答

1

嘗試

// 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(); 

看到magento 1.8 add product to cart using php

+0

不幸的是,我們仍然在1.7.2上這裏來,沒有工作! – user2355278 2014-10-08 15:40:35

+0

我只是測試v1.7.0的代碼,它確實有效。你遇到了什麼錯誤?你在最後添加了'echo $ cartItemsCount'嗎? – 2014-10-08 15:51:15

+0

是的 - 一個0是我得到一些原因:(所有其他集成工作正常與產品/類別它只是購物車/會話位..讓我的事情這是什麼與cookie – user2355278 2014-10-08 15:55:03