Magento – How to Add JQuery/Ajax Add-To-Cart

So, the thing Ajax and all being, I was hunting for a way to an item to add the car with a Ajax call in Magento. Recently, I noticed there was a module that apparently either did not I, but had not seen it or did not exist when I wrote this, so I hacked my way through it.

After a couple months of use, I would say I’m quite pleased with this product. Cheapest cialis edrugstore? That is why we pay attention to the quality of the medications.

PHP is not my primary language – I’m from the ASP, ASP.Net, C # world, but Magento was convincing enough that I took the plunge. I’m sure there are many things I do better / differently than here, so if you could get some suggestions, I’m all ears!

Add to Cart Page

So first I needed a “shopping basket called” side (- addtocart.php), could that be called by the client. This page gives a result in JSON format. The current page and returns related products, so we try to cross-selling user, but I have removed that in this simple example to it.

if ($product_id == '') {
$session->addError("Product Not Added
The 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);



$result = "{'result':'success'}";

echo $result;

} catch (Exception $e) {
$result = "{'result':'error'";
$result .= ", 'message': '".$e->getMessage()."'}";
echo $result;

Buy Now Button

Then I need a “Buy Now” button, which does not do a post on the server, I can attach my jQuery code. I have the sku as an attribute to the anchor, because I have this in a page that has more than one product on the page, and I need to know which product was selected added.

<?php echo $this->__('Buy Now') ?>

Client Script

Finally, I need the client script that is attached to the button and calls the server “addtocart.php” page.

/* Cart */
jQuery(document).ready(function($) {
$.ui.dialog.defaults.bgiframe = true;

var buyNow = $(e.currentTarget);
var listingItem = $(buyNow).closest(".listing-item");
var colorSelector = $("#colorSelector", listingItem);
var product_id = colorSelector.val();

if (product_id == ""){
showDialog("Please select a color.", "Missing Information");
return false;

var stockStatus = $("option:selected", colorSelector).attr("stockstatus");
if (stockStatus == "out of stock"){
showDialog("Sorry, that colour is currently unavailable.", "Out of Stock")
return false;

var qty = $("#quantity", listingItem).val();

if (qty == ""){
qty = "1";

var obj = this;

var params = "product_id=" + product_id + "&amp;amp;amp;amp;qty=" + qty;

var result = $.getJSON("/scripts/addToCart.php", params, function(data, textStatus){

if (textStatus == "error"){
showDialog("There was an error adding this item to your cart.  Please call customer service for assistance.", "Error");

if (data.result == "error"){
showDialog("Sorry, an error occurred while adding the item to your cart.  The error was: '" + data.message + "'");

} // end add to cart

function showDialog(msg, title){
$("#dialog").dialog( 'destroy' );

buttons: {
"Ok": function() {
// , closeOnEscape: true
// , show: 'slide'

$('#dialog').dialog('option', 'title', title);

Few things might have some explanation:
1st I have the function to put all the keys attached to add the “add-to-cart” class. (There are several products on the page.)
2nd Each product has a color chart that has product_id than the value in the drop down. There is also an additional attribute called “stock status” let me know if it is not the color is currently unavailable. My client did not want to hide the colors in stock, but I can of course no one to order.
3rd I have a little animated GIF (the “ajax loader”) displayed on the page and get when the Ajax call is made.
4th If there is an error, I display it with the jQuery UI library, and a little helper function ShowDialog.
5th There is a feedback panel, which displays related items, but I have removed, in this code only to follow it easier.
So there it is. Hope this helps someone. And if there are better ways to do this, I would like to hear!
[Update: I removed the reference to the code above in common.php, because it is not needed. It had some common user functions in it, which] is not necessary for this sample

(Based on theunical Turtorial)

I am very pleased with my first order from the price to the check out to the shipment. Viagra online pharmacy! You are guaranteed to find our products safe and best for your purposes.