Page 1 of 1

$output doubled after ajax add to cart

Posted: Sat Jan 29, 2011 3:53 am
by neosdesign
Im trying to get the ajax add to cart feature working on my 1.4.9.1 site. I upgraded from 1.4.8 and some things haven't seemed to work properly. Currently when I add a product to the cart, ajax will update the cart and count correctly but the $output as defined in cart.php appears to be executed twice. I get "8 item(s) in cart:8item(s) in cart:" instead of just "8 item(s) in cart:"

The output section of my cart.php looks like this:

Code: Select all

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

		$this->load->model('tool/seo_url');
		
		unset($this->session->data['shipping_methods']);
		unset($this->session->data['shipping_method']);
		unset($this->session->data['payment_methods']);
		unset($this->session->data['payment_method']);	
		
		if ($this->request->server['REQUEST_METHOD'] == 'POST') {
			
			if (isset($this->request->post['remove'])) {
	    		$result = explode('_', $this->request->post['remove']);
          		$this->cart->remove(trim($result[1]));
      		} else {
				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);
			}
		}
				
		$output = '';
		
		if ($this->cart->getProducts()) {
		
    		foreach ($this->cart->getProducts() as $product) {
        		
				$output .= '<a href="index.php?route=checkout/cart">' . $this->cart->countProducts() . ' ' . $this->language->get('text_shoppingbag') . '</a>';
				
	            }
			
    		
    		$total = 0;
			$taxes = $this->cart->getTaxes();
			 
			$this->load->model('checkout/extension');
			
			$sort_order = array(); 
			
			$view = HTTP_SERVER . 'index.php?route=checkout/cart';
			$checkout = HTTPS_SERVER . 'index.php?route=checkout/shipping';
			
			$results = $this->model_checkout_extension->getExtensions('total');
			
			foreach ($results as $key => $value) {
				$sort_order[$key] = $this->config->get($value['key'] . '_sort_order');
			}
			
			array_multisort($sort_order, SORT_ASC, $results);
			
			foreach ($results as $result) {
				$this->load->model('total/' . $result['key']);

				$this->{'model_total_' . $result['key']}->getTotal($total_data, $total, $taxes);
			}
			
			$sort_order = array(); 
		  
			foreach ($total_data as $key => $value) {
      			$sort_order[$key] = $value['sort_order'];
    		}

    		array_multisort($sort_order, SORT_ASC, $total_data);
    	    		
          		foreach ($total_data as $total) {
      			
      			   if ($total['title'] != 'Sub-Total:') {
            $output .= '<div class="product_total">' . $total['text'] . '</div>';
        }

      			      		}
      	
		} else {
			$output .= '<div style="text-align: center;">' . $this->language->get('text_empty') . '</div>';
		}
		
		$this->response->setOutput($output, $this->config->get('config_compression'));
	} 	
}
?>
Thank you so much for reading and helping!

Re: $output doubled after ajax add to cart

Posted: Sat Jan 29, 2011 4:02 am
by Chones
A link to your site might help
Always include a link if you are having a store front issue!
It could, for instance, be a template problem, but we can't tell.

Re: $output doubled after ajax add to cart

Posted: Thu Jul 07, 2011 11:36 pm
by arash123arash
i think its something to do with header.tpl, something to do with the following!


$(document).ready(function () {
$('#add_to_cart').removeAttr('onclick');

$('#add_to_cart').click(function () {
$.ajax({
type: 'post',
url: 'index.php?route=module/cart/minicart',
dataType: 'html',
data: $('#product :input'),
success: function (html) {
$('#header_cart .center').html(html);
}
});
});
});

Re: $output doubled after ajax add to cart

Posted: Thu Jul 07, 2011 11:47 pm
by Qphoria
Yes, your theme uses its own minicart in the header. The opencart core uses the cart sidebox to add that as well.
You need to disable the cart sidebox if using the minicart in the header

Re: $output doubled after ajax add to cart

Posted: Tue Jul 31, 2012 12:38 am
by davidfeldt
The issue has to do with JQuery's removeAttr() and "onclick"- this doesn't fire in IE.

In your ajax_add.js file:

Replace $('#add_to_cart').removeAttr('onclick');
With $('#add_to_cart').prop('onclick',false);

This now works in IE (7,8,9), Chrome and Firefox.