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
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&id_product={$product.id_product}&token={$static_token}&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&id_product={$product.id_product}&token={$static_token}&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