Magento自定义添加到购物车过程无法正常工作

Jos*_*osh 5 session shopping-cart magento

修订后的问题:我们已将此跟踪到自定义添加到购物车方法.我已完全修改了这个问题.

我正在使用Magento ver的网站上工作.1.3.2.4作为其电子商务平台.我们已经构建了一个自定义的"添加到购物车"流程,该流程通过AJAX请求将多个商品添加到购物车.在此请求之后,在浏览器中使用JavaScript完成一些后处理,然后重定向到"查看购物车"页面.99%的时间此过程似乎在Firefox和Safari中正常运行,但在IE8中,该过程失败.将商品添加到购物车后,重定向到"您的购物车"页面后,购物车为空.

并非所有网站上的项目都是通过此AJAX流程添加的.只有在通过AJAX添加项目之前购物车为空时才会出现此问题.也就是说,如果通过正常的Magento进程添加的项目首先添加到cat中,那么AJAX添加到购物车请求总是成功.Blu清除cookie然后尝试通过AJAX添加将在IE8上一致地失败.

Server是一个Apache/PHP服务器,带有PHP 5.2.9,eAccelerator和Suhosin.请索取任何其他信息,我很乐意提供.我们将会话存储在MySQL数据库中.

这是我们的自定义添加到购物车方法的代码.此代码位于/app/code/core/Mage/Checkout/controllers/CartController.php:

public function ajaxaddAction()
{
    $result = array('success' => true);

    try
    {
        $session = $this->_getSession();
        $cart = $this->_getCart();

        $products = json_decode($_POST['products'],true);

        if(!is_array($products))
        {
            throw new Exception("Products data not sent");
        }

        foreach ($products as $product_data)
        {
            $product = $this->_initProduct($product_data['id']);

            if(!$product)
                throw new Exception("Product id {$product_data['id']} not found");

            $info = array('qty' => $product_data['qty']);

            if($product_data['options'])
                $info['options'] = $product_data['options'];

            $cart->addProduct($product,$info);
        }

        $cart->save();

        $this->_getSession()->setCartWasUpdated(true);

        /**
         * @todo remove wishlist observer processAddToCart
         */
        Mage::dispatchEvent('checkout_cart_add_product_complete',
            array('product' => $products[0], 'request' => $this->getRequest(), 'response' => $this->getResponse())
        );

        $cartItems = $cart->getQuote()->getAllItems();

        $result['cart'] = array();

        foreach($cartItems as $item)
            $result['cart'][] = json_decode($item->toJson());
    }
    catch (Mage_Core_Exception $e)
    {
        if ($this->_getSession()->getUseNotice(true)) {
            $this->_getSession()->addNotice($e->getMessage());
        } else {
            $messages = array_unique(explode("\n", $e->getMessage()));
            foreach ($messages as $message) {
                $this->_getSession()->addError($message);
            }
        }
        $result['success'] = false;
        $result['exception'] = $e->getMessage();
    }

   catch (Exception $e) {
        $this->_getSession()->addException($e, $this->__('Can not add item to shopping cart'));
        $result['success'] = false;
        $result['exception'] = $e->getMessage();
    }

    header('Content-Type: application/json',true);

    ob_end_clean();

    echo json_encode($result);

    exit();
}
Run Code Online (Sandbox Code Playgroud)

请不要回答"将代码移动到/app/code/local/目录".我知道这是一个更好的地方,并将在未来移动它,但除非你的答案将解决问题,请发表评论.为了获得更快的响应,我开始获得赏金,并希望得到这个特定问题的良好答案,而不仅仅是有关更好地集成此代码的方法的提示.

如果有任何我可以提供帮助的信息,请告诉我.我们的时间紧迫......

Jos*_*osh 4

我在这上面花了10多个小时。目前我相信我有一个部分解决方案。但我不确定为什么这个解决方案有效......

看来 Magento 需要重定向才能完成添加到购物车过程。所以而不是

header('Content-Type: application/json',true);
ob_end_clean();
echo json_encode($result);
exit();
Run Code Online (Sandbox Code Playgroud)

我将 JSON 存储在会话中并重定向到新的购物车操作:

$this->_getSession()->setCartJsonResult(json_encode($result));
$this->_redirect('checkout/cart/postajaxadd');
Run Code Online (Sandbox Code Playgroud)

然后该操作转储 JSON 数据

public function postajaxaddAction()
{
    $session = $this->_getSession();

    header('Content-Type: application/json',true);
    ob_end_clean();
    echo $this->_getSession()->getCartJsonResult();
    exit();
}
Run Code Online (Sandbox Code Playgroud)

有时这仍然会失败;但是现在我的 JavaScript 代码没有获得它期望的 JSON 数据,并且能够重复请求。第二个请求比第一个请求更容易成功...但是无论如何,仍然存在 AJAX 请求失败的情况。