Page 1 of 1

Animate adding product image to cart

Posted: Fri Jun 17, 2011 9:18 pm
by opencart-templates
I have had a few emails already about the latest Opencart release without the add to cart animating the product. So here is how to manually add it back.

Assuming you are using the cart module, otherwise you will need to change #module_cart to #cart

DEMO: http://demo.opencart-templates.co.uk/de ... od_classic

- EDIT: /catalog/view/theme/default/template/product/product.tpl
- FIND:

Code: Select all

$('html, body').animate({ scrollTop: 0 }, 'slow'); 
- ADD ABOVE:

Code: Select all

animateProduct( $("#image") , $("#module_cart") );  
- Edit: /catalog/view/javascript/common.js
- ADD inside the $(document).read

Code: Select all

 /* AddToCart */
   $('.addToCart').click(function() {
	  var tis = $(this);
	  $.ajax({
		 url: 'index.php?route=checkout/cart/update',
		 type: 'post',
		 data: 'product_id=' + tis.attr("data-id"),
		 dataType: 'json',
		 content: this,
		 success: $.proxy(function(json) {
			$('.success, .warning, .attention, .information, .error').remove();
			
			if (json['redirect']) {
			   location = json['redirect'];
			}
			
			if (json['error']) {
			   if (json['error']['warning']) {
				  $('#notification').html('<div class="warning" style="display: none;">' + json['error']['warning'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
			   }
			}   
					 
			if (json['success']) {
			   $('#notification').html('<div class="attention" style="display: none;">' + json['success'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
			   
			   $('.attention').fadeIn('slow');
			   
			   $('#cart_total').html(json['total']);
			   $('#module_cart .cart-module').html(json['output']);
			   
			   animateProduct(tis.parents().eq(2).find(".image img") , $("#module_cart"));
			   
			   $('html, body').animate({ scrollTop: 0 }, 'slow');
			}   
		 }, this)
	  });
	  
   });
- ADD to bottom:

Code: Select all

function animateProduct(image,cart){

   image.before('<img src="' + image.attr('src') + '" id="temp" style="position: absolute; top: ' + image.top + 'px; left: ' + image.left + 'px;" />');

   var cart_offset  = cart.offset();

   params = {
	  top : cart_offset.top + 'px',
	  left : cart_offset.left + 'px',
	  opacity : 0.0,
	  width : cart.width(), 
	  height : cart.height()
   };

   $('#temp').animate(params, 'slow', false, function () {
	  $('#temp').remove();
   });
}
I moved majority of the code to the common.js in the hope that this will be updated less than the product.tpl

- Seen as we are now using a function and not inline JS to animate the image, we can reuse this throughout the website. So I though why not show you how this could work with HTML5 data attribute and eventually this might be updated in the core.

- EDIT: /catalog/view/theme/default/template/product/category.tpl
- FIND:

Code: Select all

<div class="cart"><a onclick="addToCart('<?php echo $product['product_id']; ?>');" class="button"><span><?php echo $button_cart; ?></span></a></div>
- REPLACE WITH:

Code: Select all

<div class="cart"><a data-id="<?php echo $product['product_id']; ?>" class="button addToCart"><span><?php echo $button_cart; ?></span></a></div>

Re: [MOD][1.5.0]Animate adding product image to cart

Posted: Fri Sep 02, 2011 10:58 pm
by rph
Cool. Will have to try this out.

Re: Animate adding product image to cart

Posted: Sun Sep 04, 2011 10:30 am
by anti91
Perfect..

Re: Animate adding product image to cart

Posted: Tue Sep 27, 2011 7:50 am
by vickiowa
I tried this but it didn't work for me.

I'm using OC 1.5.1.1 and Shoppica Theme 1.0.8 (www.shoppica dotnet)

Any ideas or advice would be welcome. Thanks much.
Vick

Re: Animate adding product image to cart

Posted: Wed Sep 28, 2011 11:24 am
by crookedview
Looks great - will try this out!

Re: Animate adding product image to cart

Posted: Mon Dec 26, 2011 12:22 am
by trance
You've got an error in the instructions you wrote

Code: Select all

function animateProduct(image,cart){

   image.before('<img src="' + image.attr('src') + '" id="temp" style="position: absolute; top: ' + image.top + 'px; left: ' + image.left + 'px;" />');

   var cart_offset  = cart.offset();

   params = {
     top : cart_offset.top + 'px',
     left : cart_offset.left + 'px',
     opacity : 0.0,
     width : cart.width(), 
     height : cart.height()
   };

   $('#temp').animate(params, 'slow', false, function () {
     $('#temp').remove();
   });
}

should be so

Code: Select all

function animateProduct(image,cart){
	var image_offset  = image.offset();
   $("body").append('<img src="' + image.attr('src') + '" id="temp" style="position: absolute; top:'+image_offset.top+'px; left:'+image_offset.left+'px" />');

   var cart_offset  = cart.offset();
   params = {
	  top : cart_offset.top + 'px',
	  left : cart_offset.left + 'px',
	  opacity : 0.0,
	  width : cart.width(),
	  height : cart.height()
   };
	
  $('#temp').animate(params, 'slow', false, function () {
	  $('#temp').remove();
   });
}


Re: Animate adding product image to cart

Posted: Mon Dec 26, 2011 4:44 am
by opencart-templates
Yes thanks for spotting this, I upgraded the code so it works from a category page a while ago now and forgot to update this post.

Re: Animate adding product image to cart

Posted: Fri Apr 27, 2012 8:57 am
by har10
hallo,
could it be possible instead of product image to animate

we could animate cart button
there i put up a icon that could be animated


thanks in advance