<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>NY INTERACTIVE AGENCY « iFUEL INTERACTIVE / AGENCY212 &#187; jQuery</title>
	<atom:link href="http://blogs.ifuelinteractive.com/tag/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.ifuelinteractive.com</link>
	<description>A NY INTERACTIVE AGENCY</description>
	<lastBuildDate>Fri, 02 Jul 2010 15:38:25 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>jQuery, Magento and Ajax Add to Cart Redux</title>
		<link>http://blogs.ifuelinteractive.com/2009/10/15/jquery-magento-and-ajax-add-to-cart-redux/</link>
		<comments>http://blogs.ifuelinteractive.com/2009/10/15/jquery-magento-and-ajax-add-to-cart-redux/#comments</comments>
		<pubDate>Thu, 15 Oct 2009 14:49:16 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Magento]]></category>
		<category><![CDATA[Add to Cart]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://blogs.ifuelinteractive.com/?p=81</guid>
		<description><![CDATA[My previous post on adding to the cart with ajax in Magento has generated enough interest &#8211; and pointed out enough flaws in my overly complex code &#8211; that I&#8217;ve decided to put together a simplified version, so here goes:
ajaxAddToCart.zip
Step 1: Create the server side script. 
My sample script is called &#8220;addToCartTest.php&#8221; and I put [...]]]></description>
			<content:encoded><![CDATA[<p>My previous post on adding to the cart with ajax in Magento has generated enough interest &#8211; and pointed out enough flaws in my overly complex code &#8211; that I&#8217;ve decided to put together a simplified version, so here goes:</p>
<p><a href="http://blogs.ifuelinteractive.com/wp-content/uploads/2009/10/ajaxAddToCart.zip">ajaxAddToCart.zip</a></p>
<p><strong>Step 1: Create the server side script. </strong><br />
My sample script is called &#8220;addToCartTest.php&#8221; and I put it in a /scripts folder in the root of my Magento installation.</p>
<pre>
<pre class="brush: php">

&lt;?php
include_once &#039;../app/Mage.php&#039;;
Mage::app();

try{
     // usage /scripts/addToCartTest.php?product_id=838&amp;amp;amp;amp;amp;amp;qty=1
     $product_id = &#039;&#039;;

     // get query string
     if (!isset($_GET[&#039;product_id&#039;])) { $product_id = &#039;&#039;; } else { $product_id = $_GET[&#039;product_id&#039;]; }
     if (!isset($_GET[&#039;qty&#039;])) { $qty = &#039;1&#039;; } else { $qty = $_GET[&#039;qty&#039;]; }

     $product = Mage::getModel(&#039;catalog/product&#039;)-&gt;load($product_id);

     $session = Mage::getSingleton(&#039;core/session&#039;, array(&#039;name&#039;=&gt;&#039;frontend&#039;));
     $cart = Mage::helper(&#039;checkout/cart&#039;)-&gt;getCart();

     $cart-&gt;addProduct($product, $qty);

     $session-&gt;setLastAddedProductId($product-&gt;getId());
     $session-&gt;setCartWasUpdated(true);

     $cart-&gt;save();

     $result = &quot;{&#039;result&#039;:&#039;success&#039;}&quot;;
     echo $result;

} catch (Exception $e) {
     $result = &quot;{&#039;result&#039;:&#039;error&#039;&quot;;
     $result .= &quot;, &#039;message&#039;: &#039;&quot;.$e-&gt;getMessage().&quot;&#039;}&quot;;
     echo $result;
}
</pre>
</pre>
<p><span id="more-81"></span><br />
<strong>Step 2: Create the &#8216;html&#8217; page<br />
</strong>Obviously, you&#8217;d be putting this code into your product detail page or wherever you&#8217;re trying to add the item to the cart from.</p>
<pre>
<pre class="brush: html">
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=ISO-8859-1&quot;&gt;
&lt;title&gt;Ajax add to cart sample&lt;/title&gt;
&lt;script src=&quot;/scripts/jquery-1.3.2.min-noconflict.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
jQuery(document).ready(function($) {
     $(&quot;#buyNowButton&quot;).click(function(){
          var qty = $(&quot;#qty&quot;).val();
          var product_id = $(&quot;#buyNowButton&quot;).attr(&quot;product_id&quot;);

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

          var result = $.getJSON(&quot;/scripts/addToCartTest.php&quot;, params, function(data, textStatus){
               if (textStatus == &quot;error&quot;){
                    alert(&quot;There was an error adding this item to your cart.  Please call customer service for assistance.&quot;, &quot;Error&quot;);
                    return;
               }

               if (data.result == &quot;error&quot;){
                    alert(&quot;Sorry, an error occurred while adding the item to your cart.  The error was: &#039;&quot; + data.message + &quot;&#039;&quot;);
                    return;
               }

               alert(&quot;Thanks!  The item has been added to your cart!&quot;)
          });
     });
});
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
     &lt;input type=&quot;text&quot; id=&quot;qty&quot; value=&quot;1&quot; /&gt;
     &lt;a id=&quot;buyNowButton&quot; product_id=&quot;972&quot; href=&quot;#&quot;&gt;Buy Now&lt;/a&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
</pre>
<p>That&#8217;s it!  </p>
<p>Prerequisites:<br />
1. jQuery (Because Magento uses prototype and scriptaculous, you have to use jQuery in &#8220;no conflict&#8221; mode);<br />
I&#8217;ve included the source code, along with the no-conflict version of jQuery in the zip file attached.</p>
<p>Let me know if you&#8217;re using this code (helps my ego)!</p>
<p>=============================<em><br />
 “e-commerce done right</em>”<br />
 <a href="http://www.ifuelinteractive.com/" target="_blank">http://www.ifuelinteractive.com</a></pre>
]]></content:encoded>
			<wfw:commentRss>http://blogs.ifuelinteractive.com/2009/10/15/jquery-magento-and-ajax-add-to-cart-redux/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Adding to the Cart with a jQuery Ajax Call in Magento</title>
		<link>http://blogs.ifuelinteractive.com/2009/09/10/adding-to-the-cart-with-a-jquery-ajax-call-in-magento/</link>
		<comments>http://blogs.ifuelinteractive.com/2009/09/10/adding-to-the-cart-with-a-jquery-ajax-call-in-magento/#comments</comments>
		<pubDate>Thu, 10 Sep 2009 14:05:53 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Magento]]></category>
		<category><![CDATA[Add to Cart]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://blogs.ifuelinteractive.com/?p=9</guid>
		<description><![CDATA[So, Ajax being the thing and all, I was hunting for a way to add an item to the cart using an Ajax call in Magento.  Recently, I noticed there was a module that apparently does this, but either I hadn&#8217;t seen that or it didn&#8217;t exist yet when I wrote this, so I hacked [...]]]></description>
			<content:encoded><![CDATA[<p>So, Ajax being the thing and all, I was hunting for a way to add an item to the cart using an Ajax call in Magento.  Recently, I noticed there was a module that apparently does this, but either I hadn&#8217;t seen that or it didn&#8217;t exist yet when I wrote this, so I hacked my way through it.</p>
<p>PHP isn&#8217;t my primary language &#8211; I come from the ASP, ASP.Net, C# world, but Magento was compelling enough that I&#8217;ve taken the leap.  I&#8217;m sure there are lots of things I could be doing better/differently here so if you&#8217;ve got some suggestions, I&#8217;m all ears!</p>
<h2>Add to Cart Page</h2>
<p>So first I needed an &#8220;Add to Cart&#8221; page (called &#8211; addToCart.php) that could be called from the client.  This page returns a result in JSON format.  The actual page also returns related items so we can try to cross sell the user, but I&#8217;ve removed that in this sample to make it simpler.</p>
<p><span id="more-9"></span></p>
<pre class="brush: php">

&lt;?php

include_once &#039;../app/Mage.php&#039;;

Mage::app();

try{
// usage /scripts/addToCart.php?product_id=838&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;qty=1
// product_id OR sku is required

// get query string
if (!isset($_GET[&#039;sku&#039;])) { $sku = &#039;&#039;; } else { $sku = $_GET[&#039;sku&#039;]; }
if (!isset($_GET[&#039;product_id&#039;])) { $product_id = &#039;&#039;; } else { $product_id = $_GET[&#039;product_id&#039;]; }
if (!isset($_GET[&#039;qty&#039;])) { $qty = &#039;1&#039;; } else { $qty = $_GET[&#039;qty&#039;]; }

if ($sku != &quot;&quot;){
$product_id = Mage::getModel(&#039;catalog/product&#039;)-&gt;getIdBySku(&quot;$sku&quot;);
if ($product_id == &#039;&#039;) {
$session-&gt;addError(&quot;&lt;strong&gt;Product Not Added&lt;/strong&gt;&lt;br /&gt;The SKU you entered ($sku) was not found.&quot;);
}
}

$request = Mage::app()-&gt;getRequest();

$product = Mage::getModel(&#039;catalog/product&#039;)-&gt;load($product_id);

$session = Mage::getSingleton(&#039;core/session&#039;, array(&#039;name&#039;=&gt;&#039;frontend&#039;));
$cart = Mage::helper(&#039;checkout/cart&#039;)-&gt;getCart();

$cart-&gt;addProduct($product, $qty);

$session-&gt;setLastAddedProductId($product-&gt;getId());
$session-&gt;setCartWasUpdated(true);

$cart-&gt;save();

$result = &quot;{&#039;result&#039;:&#039;success&#039;}&quot;;

echo $result;

} catch (Exception $e) {
$result = &quot;{&#039;result&#039;:&#039;error&#039;&quot;;
$result .= &quot;, &#039;message&#039;: &#039;&quot;.$e-&gt;getMessage().&quot;&#039;}&quot;;
echo $result;
}
</pre>
<h2>Buy Now Button</h2>
<p>Then I need a &#8220;Buy Now&#8221; button that doesn&#8217;t do a post to the server that I can attach my jQuery code to.  I&#8217;ve added 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 has been selected.</p>
<pre class="brush: html">

&lt;a href=&quot;#&quot; sku=&quot;&lt;?php echo $this-&gt;__($product-&gt;sku) ?&gt;&quot;&gt;&lt;img src=&quot;/media/upload/image/product-details/buy-now.jpg&quot; border=0 alt=&quot;&lt;?php echo $this-&gt;__(&#039;Buy Now&#039;) ?&gt;&quot;&gt;&lt;/a&gt;
</pre>
<h2>Client Script</h2>
<p>Finally, I need the client script that gets attached to the button and calls the server &#8220;addToCart.php&#8221; page.</p>
<pre class="brush: js">

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

$(&quot;.add-to-cart&quot;).click(function(e){
var buyNow = $(e.currentTarget);
var listingItem = $(buyNow).closest(&quot;.listing-item&quot;);
var colorSelector = $(&quot;#colorSelector&quot;, listingItem);
var product_id = colorSelector.val();

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

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

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

if (qty == &quot;&quot;){
qty = &quot;1&quot;;
}

$(this).siblings(&quot;.ajax-loader&quot;).show();
var obj = this;

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

var result = $.getJSON(&quot;/scripts/addToCart.php&quot;, params, function(data, textStatus){
$(obj).siblings(&quot;.ajax-loader&quot;).hide();

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

if (data.result == &quot;error&quot;){
showDialog(&quot;Sorry, an error occurred while adding the item to your cart.  The error was: &#039;&quot; + data.message + &quot;&#039;&quot;);
return;
}

// SHOW FEEDBACK, ERRORS AND RELATED ITEMS
} // end add to cart

function showDialog(msg, title){
$(&quot;#dialog&quot;).dialog( &#039;destroy&#039; );
$(&quot;#dialog&quot;).html(msg);

$(&quot;#dialog&quot;).dialog({
buttons: {
&quot;Ok&quot;: function() {
$(this).dialog(&quot;close&quot;);
}
}
// , closeOnEscape: true
// , show: &#039;slide&#039;
});

$(&#039;#dialog&#039;).dialog(&#039;option&#039;, &#039;title&#039;, title);
$(&quot;#dialog&quot;).dialog(&#039;open&#039;);
}
});
</pre>
<p>Few things probably need some explanation:</p>
<p>1. I&#8217;ve attached the function to ALL add to cart buttons using the &#8220;add-to-cart&#8221; class.  (There are multiple products on the page.)</p>
<p>2. Each product has a color selector that has the product_id as the value in the drop down.  There&#8217;s also an additional attribute called &#8220;stockstatus&#8221; that will let me know if the color is out of stock.  My customer didn&#8217;t want to hide the out of stock colors, but I obviously can&#8217;t let anyone order them.</p>
<p>3. I put a little animated gif (the &#8220;ajax loader&#8221;) on the page and that gets displayed when the ajax call is being made.</p>
<p>4. If there is an error, I display it using the jQuery UI library and a little showdialog helper function.</p>
<p>5. There&#8217;s a feedback panel that shows related items, but I&#8217;ve removed that in this code just to make it easier to follow.</p>
<p>So there it is.  Hope this helps someone.  And if there are better ways to do this, I&#8217;d love to hear them!</p>
<p>[Update:  I removed the reference to common.php in the code above because it's not needed.  It had some common user functions in it that aren't necessary for this sample]</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.ifuelinteractive.com/2009/09/10/adding-to-the-cart-with-a-jquery-ajax-call-in-magento/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>
