Page 1 of 1

How to add ADD TO CART button on home page and catgory

Posted: Thu May 13, 2010 12:52 am
by mondy
b"h

1.) put this code at the end of the theme/defult/template/module/cart.tpl

Code: Select all

<?php if ($ajax) { ?>
<script type="text/javascript"><!--
$(document).ready(function () {
	$('.addtocart').replaceWith('<a onclick="" class="button addtocart">' + $('.addtocart').html() + '</a>');
	
	$('.addtocart').click(function () {
		var pid = this.parentNode.id;
		$.ajax({
			type: 'post',
			url: 'index.php?route=module/cart/callback',
			dataType: 'html',
			data: $('#'+this.parentNode.id+' :input'),
			success: function (html) {
				$('#module_cart .middle').html(html);
			},	
			complete: function () {
				var image = $('#img_'+pid).offset();
				var cart  = $('#module_cart').offset();
	
				$('#img_'+pid).before('<img src="' + $('#img_'+pid).attr('src') + '" id="temp" style="position: absolute; top: ' + image.top + 'px; left: ' + image.left + 'px;" />');
	
				params = {
					top : cart.top + 'px',
					left : cart.left + 'px',
					opacity : 0.0,
					width : $('#module_cart').width(),  
					heigth : $('#module_cart').height()
				};		
	
				$('#temp').animate(params, 'slow', false, function () {
					$('#temp').remove();
				});		
			}			
		});			
	});			
});
//--></script>
<?php } ?>
2.) change the page you want like:
theme/defult/template/common/home.tpl
theme/defult/template/product/category.tpl
theme/defult/template/product/special.tpl
theme/defult/template/product/search.tpl

search a little, about line 38:

Code: Select all

	<div class="heading"><?php echo $text_latest; ?></div>
    <table class="list">
      <?php for ($i = 0; $i < sizeof($products); $i = $i + 4) { ?>
      <tr>
        <?php for ($j = $i; $j < ($i + 4); $j++) { ?>
        <td style="width: 25%;"><?php if (isset($products[$j])) { ?>
          <?//to adding AddToCart Buttons MO.?>
		  <a href="<?php echo str_replace('&', '&', $products[$j]['href']); ?>"><img id="img_product_<?php echo $products[$j]['id']; ?>" src="<?php echo $products[$j]['thumb']; ?>" title="<?php echo $products[$j]['name']; ?>" alt="<?php echo $products[$j]['name']; ?>" /></a><br />
		  <?//End adding AddToCart Buttons MO.?>
and about line 48:

Code: Select all

		  <? // adding buttons add to cart MO. ?>
		  <form action="index.php?route=checkout/cart" method="post" enctype="multipart/form-data" id="product_<?php echo $products[$j]['id']; ?>">
            <input type="hidden" name="quantity" value="1" />
              <a onclick="$('#product_<?php echo $products[$j]['id']; ?>').submit();"  class="button addtocart"><span><?php echo $button_add_to_cart; ?></span></a>
            <input type="hidden" name="product_id" value="<?php echo $products[$j]['id']; ?>" />
         </form>
		  <? // adding buttons add to cart MO. ?>
          <?php if ($products[$j]['rating']) { ?>
chang the controller of the page like: catalog/controller/common/home.php
(the same for catgory, serach etc.)

about line 5

Code: Select all

				$this->language->load('common/home');
		$this->language->load('product/category'); // Mo add 10.5.2010
		$this->document->title = $this->config->get('config_title');
about line 26

Code: Select all

		
		$this->data['text_latest'] = $this->language->get('text_latest');
		// Mo adding 10.5.2010
		$this->data['button_add_to_cart'] = $this->language->get('button_add_to_cart');
		$this->data['ajax'] = $this->config->get('cart_ajax');
		// Mo END

about line 93

Code: Select all

				
          	$this->data['products'][] = array(
           	'id' 	   => $result['product_id'],  // Mo add 10.5.2010
			'name'    => $result['name'],
				'model'   => $result['model'],
            	'rating'  => $rating,
				'stars'   => sprintf($this->language->get('text_stars'), $rating),
				'thumb'   => $this->model_tool_image->resize($image, $this->config->get('config_image_product_width'), $this->config->get('config_image_product_height')),
            	'price'   => $price,
				'special' => $special,
				'href'    => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/product&product_id=' . $result['product_id'])
          	);
Good luck.

Mo

Re: How to add ADD TO CART button on home page and catgory

Posted: Fri May 14, 2010 3:53 pm
by dramony
can we a sample first? thanks!

Re: How to add ADD TO CART button on home page and catgory

Posted: Fri May 14, 2010 5:49 pm
by mondy
b"h

Here is how it is look like.
(pic is added).

Mo

Re: How to add ADD TO CART button on home page and catgory

Posted: Thu Jul 08, 2010 8:57 pm
by helenmarie
has anyone tested this? and I cant see the image above to see what it looks like? :)

Re: How to add ADD TO CART button on home page and catgory

Posted: Thu Jul 08, 2010 9:20 pm
by Qphoria
if you have 1.4.8 you won't need this as there are already add to cart buttons on those pages

Re: How to add ADD TO CART button on home page and catgory

Posted: Thu Jul 08, 2010 9:33 pm
by helenmarie
hiya,

Im running 1.4.7 at the moment. Is there a way to pinch the code from 1.4.8 to get it to work in 1.4.7 or are they completely different?

Thank you

Helen

Re: How to add ADD TO CART button on home page and catgory

Posted: Sat Mar 12, 2011 4:06 am
by stonehinged
I have this working on my 1.4.7 category page, see http://www.stonehinged.com/store/handcr ... -bracelets with one small problem. I am seeing this error:

2011-03-11 13:00:50 - PHP Notice: Undefined variable: button_add_to_cart in /home/content/s/t/l/stlockridge/html/store/catalog/view/theme/default/template/product/category.tpl on line 96

Obviously there's an issue with the button I'm still trying to debug, but if anyone knows the quick fix, that would certainly be appreciated.

Re: How to add ADD TO CART button on home page and catgory

Posted: Sat Mar 12, 2011 5:25 am
by stonehinged
UPDATE: Figured it out... these two lines

$this->data['button_add_to_cart'] = $this->language->get('button_add_to_cart');
$this->data['ajax'] = $this->config->get('cart_ajax');

were in the wrong place in my category.php file

Re: How to add ADD TO CART button on home page and catgory

Posted: Sun Jul 24, 2011 1:55 am
by Chitranjan
Hi Qphoria -

I have just luking out the solution how to add "Add to cart Button" in product listing.

i have read your statement :
"if you have 1.4.8 you won't need this as there are already add to cart buttons on those pages"

I have open cart 1.4.9.5

would request you if you could help me out in adding "Add to cart" button at home.tpl, category.tpl, special.tpl

would request you to share the code. step by step.

Thanks in advance