Post by peterban » Mon May 17, 2010 5:06 pm

Hi all! I've a problem with my cart update:
if in product page i click on "add to cart" button, animation work fine but the cart subtotal (in header) doesnt' update on click: i've to refresh the entire page to update subtotal.

Added line in header.php:

Code: Select all

$this->data['subtotal'] = $this->currency->format($this->cart->getTotal());
This is javascript in header.tpl:

Code: Select all

<script type="text/javascript"><!--
$(document).ready(function () {
	$('#inserisco_prodotto').replaceWith('<a onclick="" id="inserisco_prodotto" class="middle">' + $('#inserisco_prodotto').html() + '</a>');
	$('#inserisco_prodotto').click(function () {
		$.ajax({
			type: 'post',
			url: 'index.php?route=module/cart/callback',
			dataType: 'html',
			data: $('#product :input'),
			success: function (html) {
				$('#top_cart .middle').html(html);
			},	
			complete: function () {
				var image = $('#image').offset();
				var cart  = $('#top_cart').offset();
				$('#image').before('<img src="' + $('#image').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 : $('#top_cart').width(),  
					heigth : $('#top_cart').height()
				};		
				$('#temp').animate(params, 'slow', false, function () {
					$('#temp').remove();
				});		
			}			
		});			
	});
	
				
});
//--></script>
This is cart DIV in header.tpl

Code: Select all

			<div id="top_cart" onclick="location.href='<?php echo $cart; ?>';" style="cursor:pointer;" class="middle">
				<p id="top_cart_title">
					SPESA ATTUALE
				</p>
				<p id="top_cart_product">
					La tua spesa &egrave; di:
				</p>
				<p id="top_cart_product_number">
					&nbsp;<i><?php echo $subtotal; ?></i>
				</p>
			</div>
What's the problem?

User avatar
Newbie

Posts

Joined
Sat May 15, 2010 11:53 pm

Post by peterban » Tue May 18, 2010 6:32 pm

SOLVED

The problem was in ajax code and in catalog/controller/module/cart.php

For updated subtotal in header i added a new publicfunction in cart.php:

Code: Select all

public function subtotale() {
		$this->language->load('module/cart');

		$this->load->model('tool/seo_url');
		
		if ($this->request->server['REQUEST_METHOD'] == 'POST') {
			if (isset($this->request->post['option'])) {
				$option = $this->request->post['option'];
			} else {
				$option = array();	
			}
			
      		$this->cart->add($this->request->post['product_id'], $this->request->post['quantity'], $option);
			
			unset($this->session->data['shipping_methods']);
			unset($this->session->data['shipping_method']);
			unset($this->session->data['payment_methods']);
			unset($this->session->data['payment_method']);			
		}
				
		$output = '<table cellpadding="2" cellspacing="0" style="width: 100%;">';
		$output .= '</table>';
    	$output .= '&nbsp;<i>' .  $this->currency->format($this->cart->getTotal()) . '</i>';
		$this->response->setOutput($output, $this->config->get('config_compression'));
	} 
And i called the function in javascript:

Code: Select all

<script type="text/javascript"><!--
$(document).ready(function () {
	$('#inserisco_prodotto').replaceWith('<a onclick="" id="inserisco_prodotto" class="middle">' + $('#inserisco_prodotto').html() + '</a>');
	$('#inserisco_prodotto').click(function () {
		$.ajax({
			type: 'post',
			url: 'index.php?route=module/cart/subtotale',
			dataType: 'html',
			data: $('#product :input'),
			success: function (html) {
				$('#top_cart_product_number').html(html);
			},	
			complete: function () {
				var image = $('#image').offset();
				var cart  = $('#top_cart').offset();
				$('#image').before('<img src="' + $('#image').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 : $('#top_cart').width(),  
					heigth : $('#top_cart').height()
				};		
				$('#temp').animate(params, 'slow', false, function () {
					$('#temp').remove();
				});		
			}			
		});			
	});
	
				
});
//--></script>

Sito Web


User avatar
Newbie

Posts

Joined
Sat May 15, 2010 11:53 pm
Who is online

Users browsing this forum: No registered users and 15 guests