PrestaShop product list and quantity field

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

product list quantity field prestashop

 

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&amp;id_product={$product.id_product|intval}&amp;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&amp;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&amp;id_product={$product.id_product|intval}&amp;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&amp;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:

  • <div> with inline style="" definiton where i used background param to define the background color (light gray) and other things like paddings and margin. You can personalize it with inline styles too, or just screate own stylesheet in the global.css file.
  • <input> - this is the quantity box with added inline css styles - it's for positioning reasons and related to the size of the field. You can of course personalize it as you want.

 

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.

author milos myszczuk
Article by Milosz Myszczuk PrestaShop expert, official PrestaShop community moderator. PHP developer, specialist in relative and spatial databases management, GIS Analyst, CEO & founder of VEKIA interactive agency. Read more about VEKIA company
If you like my articles and want much more valuable tips, feel free to send me donation
1.4 version 1.4.11 1.6 404 addon admin advertise ahref ajax alpha animation api app application authentication back office backup badge banner basics block bootstrap button cache carrier cart catalog category certificate changelog chat class clear client clip cms code colors columns comments configuration contact container content controller cookie counter country coupon css csv currency customer dashboard database debug default delete delivery desktop developer device disable discount displayNav displayTop download dynamic editor effect empty encrypt engine error exchange exclude export facebook faceshop fade fancoupon fancybox fanpage fatal feature feed field file fix fixed font footer free friendly url front ftp full gallery generate gift global godaddy google google+ gray grid groupon header help hide highlight homefeatured homepage hook hosting hover howto htaccess html html5 ID image import include input instagram installation integration iPhone issue javascript jquery kgb knowhow languages law left likebox link list livingsocial loading log login logo loyality mail mailing maintenance manufacturer marketing marquee mcrypt menu meta mobile modification module movie moving multilanguage multiupload must have mysql news newsletter notification number open graph order override page password performance PHP phpmyadmin picture pinterest plugin popup post prestashop prestashop 1.0 prestashop 1.1 prestashop 1.2 prestashop 1.3 prestashop 1.4 prestashop 1.5 price rules problem product profile promotion proslider purifier quantity query quick tip random rates register reinsurance release reporting reset responsive restore results ribbon rich text right sales search security seo service shadow share shipping shop shopmania slider smarty social networks SQL SSL statistics stock store style subcategory superuser support switcher tab tablet tag tax template text theme tinyMCE tips and tricks tpl tracking translations tree trends trigger tumblr tutorial twitter update upgrade upload variables video visits voucher vulnerability web2print wide widget width window wishlist wysiwyg youtube zip zopim