Quantity field in featured products module

Today I want to show you the way for adding quantity field to home featured products module. By default this module hasn't got Add to cart button. But you can easily turn this button on, just read this tutorial: enable add to cart button in homefeatured module. But if you don't want to read additional article, no worries - I will show you the guide here. Okay, so now we can start.

 

Home featured module with quantity and add to cart button

homefeatured module with quantity field prestashop

 

 

1. Enable the add to cart button

In this case we must to edit the homefeatured module CSS styles. Open the file: /modules/homefeatured/homefeatured.css. At the bottom of this file you've got definition of the css styles for "add to cart" button:

/*
#featured-products_block_center li .ajax_add_to_cart_button {display:none;}
#featured-products_block_center li span.exclusive {display:none;}
*/ 

As you can see, button has got display:none; param. Just remove it. If you dont want to remove this field (maybe in the future you will want to use it) just comment the code with the comment tags: /* */ - exactly as i show above. Now the button will be visible. But there is one additional thing to change. You have to increase main <li> element height value. In this case you can use height: 280px;  so please use it instead the height:240px:

#featured-products_block_center li {
	margin-right:10px;
	padding:10px 0;
	width:126px;
	height:240px;
}

Now your add to cart button is visible and each product has got correct height value. 

 

2. Create quantity field and personalize the button

Now it's time to create quantiy field and to personalization of the "add to cart" button. We will change the homefeatured module template file. In this case open /modules/homefeatured/homefeatured.tpl file. Remember that this file is overrided sometimes by your theme. In this case check file /themes/modules/homefeatured/homefeatured.tpl file. If this file exist - you have to append changes there.

 

This is the default code for add to cart button:

{if ($product.quantity > 0 OR $product.allow_oosp)}
    <a class="exclusive ajax_add_to_cart_button" rel="ajax_id_product_{$product.id_product}" href="{$link->getPageLink('cart')}?qty=1&amp;id_product={$product.id_product}&amp;token={$static_token}&amp;add" title="{l s='Add to cart' mod='homefeatured'}">{l s='Add to cart' mod='homefeatured'}</a>
{else}

change it to:

{if ($product.quantity > 0 OR $product.allow_oosp)}
    <div style="display:block; clear:both; margin-top:10px; position:relative; padding:5px; background:#eee; overflow:hidden;">
        <input style="position:absolute;  top:7px; left:7px; padding: 0 3px; text-align:center; height: 20px; border: 1px solid #ccc; display:inline-block; float:left; width:22px;" type="text" name="quantity_to_cart_{$product.id_product|intval}" id="quantity_to_cart_{$product.id_product|intval}" value="1"/>
        <a style="width:60px; display:inline-block; float:right" class="exclusive ajax_add_to_cart_button" rel="ajax_id_product_{$product.id_product}" href="{$link->getPageLink('cart')}?qty=1&amp;id_product={$product.id_product}&amp;token={$static_token}&amp;add" title="{l s='Add to cart' mod='homefeatured'}">{l s='Add to cart' mod='homefeatured'}</a>
    </div>
{else}

After this your homefeatured module will looks like on the picture above.

 

Let me explain the code. The first <div> element is a gray box around the quantity field and add to cart button. I added there inline css styles (in style="" tag) but you can of course use own styles defined in homefeatured.css stylesheet file. Everything depends on you. Second code that I added is a quantity <input> field. I also added there inline styles. And last thing changed in the code: styles for add to cart button.

 

3. Changing the javascript function

And now it's time for last and most important thing in whole modification. 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());

 

Thats all.

Now quantity field should work well. Don't forget to recompile the template before you will try to test it. If you've got any questions or concerns related to this case - feel free to start discussion below, in the comments field

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