In the latest article I described how to add quantity box to default prestashop home featured module. Today I want to show you how to add the same quantity box to the each product listing in your shop based on PrestaShop engine. You will have to change the same script as i described in latest article, but no worries - I will repeat it here once again. Okay, so let's start.
Quantity field for each product "add to cart" button on product list page
1. Modify the product list template
First step in this tutorial is strictly related to the template of the product listing in your store. So in this case, we will edit product-list.tpl template file. You can find it in your theme root directory. Open it. You've got there something like:
{if ($product.allow_oosp || $product.quantity > 0)} {if isset($static_token)} <a class="button ajax_add_to_cart_button exclusive" rel="ajax_id_product_{$product.id_product|intval}" href="{$link->getPageLink('cart',false, NULL, "add=1&id_product={$product.id_product|intval}&token={$static_token}", false)}" title="{l s='Add to cart'}"><span></span>{l s='Add to cart'}</a> {else} <a class="button ajax_add_to_cart_button exclusive" rel="ajax_id_product_{$product.id_product|intval}" href="{$link->getPageLink('cart',false, NULL, "add=1&id_product={$product.id_product|intval}", false)}" title="{l s='Add to cart'}"><span></span>{l s='Add to cart'}</a> {/if} {else}
This is the definition of the add to cart button. All that we have to do is to change code above to:
{if ($product.allow_oosp || $product.quantity > 0)} <div style="margin-left:6px; display:block; clear:both; margin-top:10px; position:relative; padding:5px; background:#eee; overflow:hidden;"> <input style="position:absolute; margin-top:3px; left:6px; padding: 0 3px; text-align:center; height: 20px; border: 1px solid #ccc; display:inline-block; float:left; width:16px;" type="text" name="qty" id="quantity_to_cart_{$product.id_product|intval}" value="1"/> {if isset($static_token)} <a class="button ajax_add_to_cart_button exclusive" rel="ajax_id_product_{$product.id_product|intval}" href="{$link->getPageLink('cart',false, NULL, "add=1&id_product={$product.id_product|intval}&token={$static_token}", false)}" title="{l s='Add to cart'}"><span></span>{l s='Add to cart'}</a> {else} <a class="button ajax_add_to_cart_button exclusive" rel="ajax_id_product_{$product.id_product|intval}" href="{$link->getPageLink('cart',false, NULL, "add=1&id_product={$product.id_product|intval}", false)}" title="{l s='Add to cart'}"><span></span>{l s='Add to cart'}</a> {/if} </div> {else}
Modification is very simple. I added two things:
2. Changing the javascript function
Now we have to change the ajax-cart.js file, exactly the same as in article related to the quantity box in default prestashop home featured module. So if you made this changes - you don't have to do this once again. It mean that your quantity field should works well. If you haven't changed this file, please follow instructions below:
As I said - we have to edit the javascript add to cart function (related to the AJAX add to cart method). No worries, it will be very easy :) We must change the ajax-cart.js file located in the blockcart module. So open the file: /modules/blockcart/ajax-cart.js file.
line 40 looks like:
ajaxCart.add(idProduct, null, false, this);
change it to:
ajaxCart.add(idProduct, null, false, this, $('#quantity_to_cart_'+idProduct+'').val());
That's all
Now quantity field on your product listing pages should work. Before you will test it - make sure that you recompiled the template. If you've got any questions or concerns about this method - feel free to start discussion in comments field below.