Today i want to show you how to create notification bar which will appear right after successfully "add to cart" action in your store based on PrestaShop engine. I use the same effect here, ony MyPresta.eu website, you can test it now - just add product to cart, you will see it at the bottom of website.
Product sucessfully added to cart notification bar
How to create notification bar
Read this guide carefully, you have to follow all steps described below. What we will do? We will modify 3 files: header.tpl (your theme file), ajax-cart.js file (a part of blockcart module) and your store global stylesheet file (global.css). So, we can start, let's go ;)
1. header.tpl file modification
In this step we have to define "Product successfully added to cart" message. We must give possibility to translate this message due to the fact, that today many of stores uses many languages. In this case in header.tpl file located in your theme directory add this simple code:
var addtocartconfirmation = '{l s='Product successfully added to cart'}';
to the script with javascript variables definition, like i show below:
<script type="text/javascript"> var baseDir = '{$content_dir|addslashes}'; var baseUri = '{$base_uri|addslashes}'; var static_token = '{$static_token|addslashes}'; var token = '{$token|addslashes}'; var priceDisplayPrecision = {$priceDisplayPrecision*$currency->decimals}; var priceDisplayMethod = {$priceDisplay}; var roundMode = {$roundMode}; var addtocartconfirmation = '{l s='Product successfully added to cart'}'; </script>
We defined javascript variable which contains string "Product sucessfully added to cart". If you want to translate it to other languages, just go to localization > translations > front office translations section in your back office, and just translate it.
2. ajax-cart.js file modification
Now it's time to add javascript action to display this message right after someone add product to cart. In this case, we must edit the ajax-cart.js file located in your blockcart module directory. The path to file looks like: /modules/blockcart/ajax-cart.js but in some cases this file is overrided by theme, in this case you must open /themes/your_theme/modules/blockcart/ajax-cart.js instead of original one.
by default, near the 208 line, there is a code with "add to cart" action, right after
success: function(jsonData,textStatus,jqXHR){
add this code:
$("body").append("<div class='addtocartconfirmation'><span>"+addtocartconfirmation+"</span></div>"); $(".addtocartconfirmation").fadeOut(8000);
so full code of add function should looks like (i highlighted line with new code)
// add a product in the cart via ajax add : function(idProduct, idCombination, addedFromProductPage, callerElement, quantity, wishlist){ if (addedFromProductPage && !checkCustomizations()) { alert(fieldRequired); return ; } emptyCustomizations(); //disabled the button when adding to not double add if user double click if (addedFromProductPage) { $('#add_to_cart input').attr('disabled', true).removeClass('exclusive').addClass('exclusive_disabled'); $('.filled').removeClass('filled'); } else $(callerElement).attr('disabled', true); if ($('#cart_block_list').hasClass('collapsed')) this.expand(); //send the ajax request to the server $.ajax({ type: 'POST', headers: { "cache-control": "no-cache" }, url: baseUri + '?rand=' + new Date().getTime(), async: true, cache: false, dataType : "json", data: 'controller=cart&add=1&ajax=true&qty=' + ((quantity && quantity != null) ? quantity : '1') + '&id_product=' + idProduct + '&token=' + static_token + ( (parseInt(idCombination) && idCombination != null) ? '&ipa=' + parseInt(idCombination): ''), success: function(jsonData,textStatus,jqXHR) { $("body").append("<div class='addtocartconfirmation'><span>"+addtocartconfirmation+"</span></div>"); $(".addtocartconfirmation").fadeOut(8000); // add appliance to wishlist module if (wishlist && !jsonData.errors) WishlistAddProductCart(wishlist[0], idProduct, idCombination, wishlist[1]); // add the picture to the cart var $element = $(callerElement).parent().parent().find('a.product_image img,a.product_img_link img'); if (!$element.length) $element = $('#bigpic'); var $picture = $element.clone(); var pictureOffsetOriginal = $element.offset(); pictureOffsetOriginal.right = $(window).innerWidth() - pictureOffsetOriginal.left - $element.width(); if ($picture.length) { $picture.css({ position: 'absolute', top: pictureOffsetOriginal.top, right: pictureOffsetOriginal.right }); } var pictureOffset = $picture.offset(); var cartBlock = $('#cart_block'); if (!$('#cart_block')[0] || !$('#cart_block').offset().top || !$('#cart_block').offset().left) cartBlock = $('#shopping_cart'); var cartBlockOffset = cartBlock.offset(); cartBlockOffset.right = $(window).innerWidth() - cartBlockOffset.left - cartBlock.width(); // Check if the block cart is activated for the animation if (cartBlockOffset != undefined && $picture.length) { $picture.appendTo('body'); $picture .css({ position: 'absolute', top: pictureOffsetOriginal.top, right: pictureOffsetOriginal.right, zIndex: 4242 }) .animate({ width: $element.attr('width')*0.66, height: $element.attr('height')*0.66, opacity: 0.2, top: cartBlockOffset.top + 30, right: cartBlockOffset.right + 15 }, 1000) .fadeOut(100, function() { ajaxCart.updateCartInformation(jsonData, addedFromProductPage); $(this).remove(); }); } else ajaxCart.updateCartInformation(jsonData, addedFromProductPage); }, error: function(XMLHttpRequest, textStatus, errorThrown) { alert("Impossible to add the product to the cart.\n\ntextStatus: '" + textStatus + "'\nerrorThrown: '" + errorThrown + "'\nresponseText:\n" + XMLHttpRequest.responseText); //reactive the button when adding has finished if (addedFromProductPage) $('#add_to_cart input').removeAttr('disabled').addClass('exclusive').removeClass('exclusive_disabled'); else $(callerElement).removeAttr('disabled'); } }); },
3. global.css styles
Now it's time to define CSS styles for our new block addtocartconfirmation (block with our confirmation message). We must add styles to global stylesheet file. By default it's a global.css file located in your theme directory /themes/your_theme/css/global.css. Add there these styles:
.addtocartconfirmation { background: #D34F2B; display:block; position:fixed; bottom:0px; left:0px; text-align:center; padding:10px 0px; width:100%; z-index:99999; } .addtocartconfirmation span { color:#fff; text-shadow:1px 1px 0px #000; font-size:18px; font-weight:bold; }
Of course you can use own colors for this notification bar. You can customize it exactly as you want to fit your theme colors. Just change params above. Final effect: