Magento add product to shopping cart using ajax

In this blog we are going to discuss on how to add product to shopping cart using ajax. After little search i found some valuable information on how to add product to the cart without reloading the page. The different approach you can try are as follows.

Method 1: Create out of box script and call that script file using ajax call.
– It’s good if you have simple product only. create scripts script in root folder parallel to app folder and create one php file, copy code mentioned bellow and call this file and pass parameter using get method.

include_once '../app/Mage.php';
    $result = array();
    if(!isset($_GET['sku'])) {
        $sku = '';
    else {
        $sku = $_GET['sku'];
    if(!isset($_GET['product_id'])) {
        $product_id = '';
    else {
        $product_id = $_GET['product_id'];
    if(!isset($_GET['qty'])) {
        $qty = '1';
    else {
        $qty = $_GET['qty'];
    if ($sku != ""){
        $product_id = Mage::getModel('catalog/product')->getIdBySku("$sku");
        if ($product_id == '') {
            $session->addError("Product Not AddedThe SKU you entered ($sku) was not found.");
    $request = Mage::app()->getRequest();
    $product = Mage::getModel('catalog/product')->load($product_id);
    $session = Mage::getSingleton('core/session', array('name'=>'frontend'));
    $cart = Mage::helper('checkout/cart')->getCart();
    $cart->addProduct($product, $qty);
    $items_in_cart = Mage::helper('checkout/cart')->getSummaryCount();
    $result['items_in_cart'] = "$items_in_cart";
    echo json_encode($result);
catch (Exception $e) {
    $result['result'] = 'error';
    $result['message'] =  $e->getMessage();
    echo json_encode($result);

Method 2: In second method we can push product using ajax call get method (that is all the parameters are part of url.).
I have tested this for simple product, configurable product and bundle product. You have to just make sure that attribute and value pare is composed in proper format. I have composed the sample url, this will help you to understand on what should be the url format for while adding the product to the shopping cart using ajax.

Example 1: Add Simple product using following format.[id]&qty=[qty]

Example 2: Add configurable product using following format.$id&qty=$qty&options[$id]=$value

Example 3: Add bundle product using following format.[12]=57

Note: In case of configurable or bundle product you may have to check the response html for error message, if any.

Share this nice post:

25 thoughts on “Magento add product to shopping cart using ajax

  1. An exciting discourse is worth report. I think that you should write many on this theme, it power not be a inhibition issue but mostly fill are not enough to talk on such topics. To the succeeding. Cheers like your Where Can I Find Tutorials For Beginners.

  2. I will be subscribing to your feed. Interesting points were made here. Thanks for sharing.

  3. It’s a really very informative information. It’s very useful and knowledgeable for me. I will bookmark this page and come soon to see the updates.

Comments are closed.