Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 656219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:41:25+00:00 2026-05-13T22:41:25+00:00

REVISED QUESTION : We have tracked this down to a custom add to cart

  • 0

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

I am working on a site that is using Magento ver. 1.3.2.4 as its eCommerce platform. We have built a custom “Add To Cart” process which adds multiple items to the cart via an AJAX request. After this request, some postprocessing is done viw JavaScript in the browser before redirecting to the “View Cart” page. 99% of the time this process seems to function properly in Firefox and Safari but in IE8, the process fails. When adding an item to the cart, after being redirected to the “Your Cart” page, the shopping cart is empty.

Not all items on the site are added via this AJAX process. This issue only happens only when the cart is empty before adding the items via AJAX. That is to say, if an item that is added via the normal Magento process is added to the cat first, then the AJAX add to cart requests always succeed. Blu clearing cookies and then attempting to add via AJAX will fail consistently on IE8.

Server is an Apache/PHP server with PHP 5.2.9, eAccelerator and Suhosin. Please request any additional information and I’ll be happy to provide it. We are storing sessions in a MySQL Database.

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

Please don’t answer with “Move the code to the /app/code/local/ directory”. I understand that’s a better place for it, and will move it there in the future, but unless your answer will solve the issue, please just post a comment. In order to get a faster response I’m starting a bounty and want good answers to this specific issue, not just tips on better ways to integrate this code.

If there’s any information I can provide to assist please let me know. We’re under a tight deadline…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T22:41:25+00:00Added an answer on May 13, 2026 at 10:41 pm

    I’ve spent over 10 hours on this. For the moment I believe I have a partial solution. But I’m not sure why this solution works…

    It seems that Magento requires a redirect in order to complete the add to cart process. So instead of

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

    I store my JSON in the session and redirect to a new cart action:

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

    That action then dumps the JSON data

    public function postajaxaddAction()
    {
        $session = $this->_getSession();
    
        header('Content-Type: application/json',true);
        ob_end_clean();
        echo $this->_getSession()->getCartJsonResult();
        exit();
    }
    

    This still fails sometimes; however now my JavaScript code does not get the JSON data it was expecting and is able to repeat the request. The second request is successful more often than the first… However there are still cases when the AJAX requests fail no matter what.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay this is a revised question from earlier today, I have included code to
REVISED QUESTION I have revised the original question (as seen below) so that I
Revised Question the collection.reset method vs the collection.add method. Both take a list of
Edit: original question below, but I revise it now that I have some code
I followed RailsCasts authentication from scratch (http://railscasts.com/episodes/250-authentication-from-scratch-revised) using the bcrypt-ruby gem and have the
This is a revised/better written version of the question I asked earlier today --
REVISED QUESTION: have an xml document, i wish to change the qty of a
REVISED QUESTION I realize using the response to: $facebook->api('/me?access_token='.$access_token) will determine whether you need
I have a question about functional dependencies. My understanding was that, for example, if
I posted this on daniweb, but have revised my thoughts on the matter. Basically

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.