Magento custom add to cart process not working

REVISED QUESTION . We tracked this down to a custom add to cart method. I have completely revised this question.

I am working on a site using Magento ver. 1.3.2.4 as an e-commerce platform. We created a custom Add to Cart process that adds multiple items to the cart through an AJAX request. After this request, some post-processing is done in JavaScript in the browser before being redirected to the "View Cart" page. In 99% of cases, this process functions properly in Firefox and Safari, but in IE8 the process fails. When adding goods to the basket, after redirecting to the "Your basket" page, the shopping basket is empty.

Not all elements on the site are added through this AJAX process. This problem only occurs when the cart is empty before adding items via AJAX. That is, if an element that is added through the regular Magento process is first added to cat, then AJAX requests to the cart will always be successful. Blu-clearing cookies and then trying to add via AJAX will work consistently on IE8.

Server is an Apache / PHP server with PHP 5.2.9, eAccelerator and Suhosin. Please request additional information and I will be happy to provide it. We save sessions in the MySQL database.

Here is the code for our custom cart add method. This code is in /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();
}

, " /app/code/local/". , , , . , , .

- , , , . ...

+5
3

10 . , . , ...

, Magento , .

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

JSON :

$this->_getSession()->setCartJsonResult(json_encode($result));
$this->_redirect('checkout/cart/postajaxadd');

JSON

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

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

- ; JavaScript JSON, , . , ... , AJAX , .

+4

, , , JSON - " Magento/Zend" .

:

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

ob_end_clean();

echo json_encode($result);

exit();

:

$this->getResponse()->setHeader('Content-Type', 'application/json', true)->setBody(json_encode($result));
+3

We are having trouble adding things to the cart when the session storage ends and new sessions cannot be created. If you store sessions on disk or in memcache, make sure that you allocate enough space.

+1
source

All Articles