2012-06-27 56 views
0

我需要能夠從我通過jQuery的發佈功能發佈的自定義PHP文件更新用戶購物車中的產品數量。我試圖使用Magento的功能,但我只得到一個空白頁面。有人知道如何更新購物車數據嗎?Magento - 從自定義PHP文件更新購物車

這是返回一個空白頁...

<?php 
include_once '/app/Mage.php'; 
$test = Mage::getModel('checkout/cart')->getQuote()->getData(); 
var_dump($test); 
?> 
+2

我不認爲你需要包括「/app/Mage.php」,除非真的Magento的安裝在你的根(「/」)。 – nachito

+0

Magento安裝在htdocs文件夾中。 – user1417156

+3

如果你的PHP腳本位於同一個文件夾中,那麼執行'require_once'app/Mage.php';' – nachito

回答

2

首先,試試這個腳本出你的Magento文件夾(其中的index.php所在的位置)內部,使得包括路徑是相對的,

其次:

<?php 
    require_once "app/Mage.php"; 
    Mage::app('default'); // initialize -the- app so you could access the singletons (like sessions, quotes, etc); 
    $test = Mage::getSingleton('checkout/cart')->getQuote()->getData(); // load the cart singleton 
    var_dump($test); 

    // take note that there isn't a closing PHP tag.. just observing the Zend Coding standards 

而且在處理你可能會想和還原的會話ID(如果知道的話)會話相關的單身人士(車,報價)。請記住,從您的自定義PHP文件進行測試並從實際的magento應用程序進行測試會得到不同的SID。你必須提供一種方法來以某種方式「共享」它。插入以下Mage:app();後:

$frontend = Mage::getSingleton('core/session', array('name' => 'frontend')); 
$frontend->setSessionId($sid); 
+0

謝謝。非常有用的答案。 – user1417156

+0

我遇到了getAllVisibleItems()問題,儘管在購物車中有物品,但仍然返回一個空數組。任何想法爲什麼? 'require_once「../../app/Mage.php」; \t Mage :: app('default'); \t $ frontend = Mage :: getSingleton('core/session',array('name'=>'frontend')); \t $ frontend-> setSessionId($ sid); \t \t \t $ cart = Mage :: getSingleton('checkout/cart'); \t $ cart-> init(); \t \t \t $ cartItems = $ cart-> getQuote() - > getAllVisibleItems();' – user1417156

+0

你真的把$ sid設置成了什麼嗎? –