<?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>NemoPS &#187; multiple</title>
	<atom:link href="http://nemops.com/tag/multiple/feed/" rel="self" type="application/rss+xml" />
	<link>http://nemops.com</link>
	<description>Prestashop Tutorials, Modules and More!</description>
	<lastBuildDate>Wed, 05 Dec 2018 13:25:23 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.35</generator>
	<item>
		<title>Adding multiple products and quantities to Prestashop&#8217;s Cart</title>
		<link>http://nemops.com/prestashop-cart-multiple-products-quantities/</link>
		<comments>http://nemops.com/prestashop-cart-multiple-products-quantities/#comments</comments>
		<pubDate>Tue, 01 Oct 2013 10:41:53 +0000</pubDate>
		<dc:creator><![CDATA[Nemo]]></dc:creator>
				<category><![CDATA[Theming]]></category>
		<category><![CDATA[cart]]></category>
		<category><![CDATA[multiple]]></category>
		<category><![CDATA[prestashop]]></category>

		<guid isPermaLink="false">http://nemops.com/?p=1618</guid>
		<description><![CDATA[<p>In today&#8217;s tutorial, we will see how to enable a &#8216;Bulk add to cart&#8217; functionality in Prestashop, to allow customers to add multiple products and quantities to their cart with a single click! Compatibility: Prestashop 1.5 (all versions), Prestashop 1.4 with slight changes What we will do The main purpose of this tutorial will be [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://nemops.com/prestashop-cart-multiple-products-quantities/">Adding multiple products and quantities to Prestashop&#8217;s Cart</a> appeared first on <a rel="nofollow" href="http://nemops.com">NemoPS</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>In today&#8217;s tutorial, we will see how to enable a &#8216;Bulk add to cart&#8217; functionality in Prestashop, to allow customers to add multiple products and quantities to their cart with a single click!</p>
<p><span id="more-1618"></span></p>
<ul>
<li>Compatibility: <strong>Prestashop 1.5</strong> (all versions), <strong>Prestashop 1.4 with slight changes</strong></li>
</ul>
<h2>What we will do</h2>
<p>The main purpose of this tutorial will be to add checkboxes so that customers can select which products they want, and add them to cart with a single click (provided that they are on the same page!). However, since we are at it, we will also add a text input so that they can choose how many items they want to add as well. This new box will also work with the regular &#8216;Add to cart&#8217;, so feel free to follow the last step only if you want! Let&#8217;s get started.</p>
<div class="separator"></div>
<h2>The checkboxes and trigger button</h2>
<p>The very first thing we need to do is add a couple of checkboxes so that people can choose which products they want. Of course, after they do this they need to be able to add them to cart as well, therefore we also need a <strong>trigger</strong> button.</p>
<p>As always, I will be using the default theme for the demonstration, and line numbers and contents might change if you use a custom one.</p>
<p>Open up <strong>product-list.tpl</strong>, located in your theme&#8217;s folder. Choose a suitable place for the checkbox; I decided to add it right above the normal &#8216;Add to cart&#8217;. So, after line 58, or wherever else you want inside the block, add:</p>
<pre class="brush: php; html-script: true; title: ; notranslate">
&lt;span class=&quot;checktoadd&quot;&gt;
	{l s='Check to add to cart'} &lt;input type=&quot;checkbox&quot; value=&quot;{$product.id_product}&quot; class=&quot;add_me_to_cart&quot;/&gt;
&lt;/span&gt;
</pre>
<p>Save and refresh a category page, you should see something like this:</p>
<p><img src="http://nemops.com/wp-content/uploads/2013/10/FIRST.png" alt="Add multiple products to Prestashop&#039;s Cart in one click" width="540" height="503" class="alignnone size-full wp-image-1624" /></p>
<p>Yeah, it looks odd, but it&#8217;s only to demonstrate the purpose! As you can see, we are giving the checkbox a value equal to the product id, so that we know which ones are checked.</p>
<p>Let&#8217;s now add the trigger: At the very end of the file, right before this:</p>
<pre class="brush: xml; title: ; notranslate">

	&lt;!-- /Products list --&gt;
{/if}

</pre>
<p>Add this:</p>
<pre class="brush: php; html-script: true; title: ; notranslate">
	&lt;a href=&quot;javascript:void(0)&quot; class=&quot;multi_add button&quot;&gt;{l s='Add selected to cart'}&lt;/a&gt;
</pre>
<p>Nothing special here, it&#8217;s a normal button without any anchor link. We have our ground ready: let&#8217;s move on!</p>
<p>As a final touch, we need a piece of text to inform our customers if no item is checked. Add the following <strong>at the very end</strong> of product-list.tpl</p>
<pre class="brush: xml; title: ; notranslate">

&lt;script type='text/javascript'&gt;
	var noSelectionTxt = &quot;{l s='No items selected'}&quot;;
&lt;/script&gt;

</pre>
<div class="separator"></div>
<h2>The multiple &#8216;Add to cart&#8217; Javascript</h2>
<p>In order to add those products to cart at once, we need, needless to say, javascript. We will simply extend the core &#8216;Ajax add to cart&#8217; functionality; therefore, this method will not work for those who decided not to use ajax.</p>
<p>Depending on your theme, you might, or might not have the file we need in the theme&#8217;s folder. Reach it, then go to <em>js/modules</em> and look for <strong>blockcart</strong>. If it&#8217;s there already, access the folder and open <strong>ajax-cart.js</strong>. If not, as if you are using the default theme, go back to Prestashop&#8217;s root, then <em>modules/blockcart</em>, grab <strong>ajax-cart.js</strong>, copy and paste it in the previously mentioned folder. At the end, this is the file we need to edit: <strong>themes/THEMENAME/js/modules/blockcart/ajax-cart.js</strong>.</p>
<p>Open up the file, and locate the following snippet at the beginning of it:</p>
<pre class="brush: jscript; title: ; notranslate">
	//for every 'add' buttons...
	$('.ajax_add_to_cart_button').unbind('click').click(function(){
		...
	}
</pre>
<p>Right <strong>before</strong> it, let&#8217;s start adding our code:</p>
<pre class="brush: jscript; title: ; notranslate">
		$('.multi_add').unbind('click').click(function() {
				
			// get all checked items
			var checked_items = $('.add_me_to_cart:checked');

			if(checked_items.length == 0)
				alert(noSelectionTxt);
			else {
			}
			

		});
</pre>
<p>As a first step, target the click to out multi add button, and  store all check items&#8217; checkboxes inside a variable. If none is selected, we alert a message, else, let&#8217;s add them to cart!</p>
<pre class="brush: jscript; title: ; notranslate">
		$('.multi_add').unbind('click').click(function() {
				
			// get all checked items
			var checked_items = $('.add_me_to_cart:checked');

			if(checked_items.length == 0)
				alert(noSelectionTxt);
			else {
				$.each(checked_items, function(i, item) {
					 var id_prd = $(item).val(); // val of the checkbox!
					 ajaxCart.add(id_prd, null, false, $(item).parent().parent().find('.ajax_add_to_cart_button'));
					 // uncheck current element
					 $(item).removeAttr('checked');
				});
			}
			

		});
</pre>
<p>For each of the items stored in the variable, we grab the <strong>val()</strong> attribute of the checkbox, which is the product id. Then, we simply call the default  <strong>ajaxCart.add</strong> function to add them. Note the following:</p>
<pre class="brush: jscript; title: ; notranslate">

$(item).parent().parent().find('.ajax_add_to_cart_button')

</pre>
<p>We passed it as fourth parameter to the function. It is referring to the default ajax add to cart button, so that the base script can grab the product image for the &#8216;add to cart&#8217; animation.</p>
<p>In the end, we uncheck the box.</p>
<p>Save and test, we should already be done for this first step!</p>
<div class="separator"></div>
<h2>Adding multiple quantities to Prestashop&#8217;s cart</h2>
<p>As we are done with the first step, let&#8217;s take care of adding the quantity input boxes. It&#8217;s far easier than it looks. Once more, locate a suitable place for the new content. I will get rid of the &#8216;view&#8217; button of the default theme, to avoid overcrowding each product block.</p>
<p>Therefore, locate the following snippet in <strong>product-list.tpl</strong>:</p>
<pre class="brush: xml; title: ; notranslate">
	&lt;a class=&quot;button lnk_view&quot; href=&quot;{$product.link|escape:'htmlall':'UTF-8'}&quot; title=&quot;{l s='View'}&quot;&gt;{l s='View'}&lt;/a&gt;
</pre>
<p>And replace it with:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;input size=&quot;1&quot; type=&quot;text&quot; class=&quot;multi_product_quantity&quot; value=&quot;1&quot; /&gt;
</pre>
<p><img src="http://nemops.com/wp-content/uploads/2013/10/SECOND.png" alt="Add multiple product quantities to Prestashop&#039;s Cart in one click" width="534" height="543" class="alignnone size-full wp-image-1625" /></p>
<p>Messy. But once again, this is a simple demonstration.</p>
<p>Now, for the javascript part, go back to <strong>ajax-cart.js</strong> (if you haven&#8217;t followed the tutorial so far, refer back to the beginning of &#8220;The multiple &#8216;Add to cart&#8217; Javascript&#8221;). let&#8217;s first add the quantity variable to the default button, locate the following:</p>
<pre class="brush: jscript; title: ; notranslate">
	//for every 'add' buttons...
	$('.ajax_add_to_cart_button').unbind('click').click(function(){
		var idProduct =  $(this).attr('rel').replace('nofollow', '').replace('ajax_id_product_', '');
		if ($(this).attr('disabled') != 'disabled')
			ajaxCart.add(idProduct, null, false, this);
		return false;
	});
</pre>
<p>Store the chosen quantity as a variable right after the product id:</p>
<pre class="brush: jscript; title: ; notranslate">
		//for every 'add' buttons...
		$('.ajax_add_to_cart_button').unbind('click').click(function(){
			var idProduct =  $(this).attr('rel').replace('nofollow', '').replace('ajax_id_product_', '');
			var qty = $(this).parent().find('.multi_product_quantity').val();

			// if quantity is 0 or NaN, return;
			if(qty == 0 || isNaN(qty))
				return false;

			if ($(this).attr('disabled') != 'disabled')
				ajaxCart.add(idProduct, null, false, this);
			return false;
		});
</pre>
<p>If quantity is 0, or a non-numeric value, we simply do not perform the action. Lastly, add the quantity as fifth parameter to the ajaxCart.add method</p>
<pre class="brush: jscript; title: ; notranslate">
		//for every 'add' buttons...
		$('.ajax_add_to_cart_button').unbind('click').click(function(){
			var idProduct =  $(this).attr('rel').replace('nofollow', '').replace('ajax_id_product_', '');
			var qty = $(this).parent().find('.multi_product_quantity').val();

			// if quantity is 0 or NaN, return;
			if(qty == 0 || isNaN(qty))
				return false;

			if ($(this).attr('disabled') != 'disabled')
				ajaxCart.add(idProduct, null, false, this, qty);
			return false;
		});
</pre>
<p>And we are done! At this point, we can amend the previous section&#8217;s code, and <strong>allow adding multiple products and multiple quantities together</strong>.</p>
<pre class="brush: jscript; title: ; notranslate">

		$('.multi_add').unbind('click').click(function() {
				
			// get all checked items
			var checked_items = $('.add_me_to_cart:checked');

			if(checked_items.length == 0)
				alert(noSelectionTxt);
			else {
				$.each(checked_items, function(i, item) {
					 $(item).parent().parent().find('.ajax_add_to_cart_button').click();
				});
			}

		});

</pre>
<p>There is no point in calling the same code 2 times, therefore we can simply trigger the default ajax add to cart button for each product.</p>
<p>The post <a rel="nofollow" href="http://nemops.com/prestashop-cart-multiple-products-quantities/">Adding multiple products and quantities to Prestashop&#8217;s Cart</a> appeared first on <a rel="nofollow" href="http://nemops.com">NemoPS</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://nemops.com/prestashop-cart-multiple-products-quantities/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
	</channel>
</rss>
