【问题标题】:Magento - Update Cart From Custom PHP FIleMagento - 从自定义 PHP 文件更新购物车
【发布时间】:2012-06-27 13:53:57
【问题描述】:

我需要能够从我通过 jQuery 的 post 函数发布到的自定义 PHP 文件更新用户购物车中的产品数量。我正在尝试使用 Magento 的功能,但我只得到一个空白页。有人知道如何更新购物车数据吗?

这就是返回一个空白页...

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

【问题讨论】:

  • 我认为你不想包含 '/app/Mage.php',除非 Magento 真的安装在你的根目录 ('/') 中。
  • Magento 安装在 htdocs 文件夹中。
  • 如果您的 PHP 脚本位于同一文件夹中,则执行 require_once 'app/Mage.php';

标签: jquery magento post cart


【解决方案1】:

首先,在你的 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);

【讨论】:

  • 尽管购物车中有商品,但我遇到了 getAllVisibleItems() 返回空数组的问题。任何想法为什么? require_once "../../app/Mage.php"; Mage::app('default'); $frontend = Mage::getSingleton('core/session', array('name' =&gt; 'frontend')); $frontend-&gt;setSessionId($sid); $cart = Mage::getSingleton('checkout/cart'); $cart-&gt;init(); $cartItems = $cart-&gt;getQuote()-&gt;getAllVisibleItems();
  • 你真的把 $sid 设置成什么了吗?
猜你喜欢
  • 1970-01-01
  • 2017-01-20
  • 1970-01-01
  • 2019-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-25
  • 2020-07-07
相关资源
最近更新 更多