Post by neosdesign » Sat Jan 29, 2011 3:53 am

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!

New member

Posts

Joined
Tue Aug 17, 2010 5:44 pm

Post by Chones » Sat Jan 29, 2011 4:02 am

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.

http://scarletandjones.com/
http://sharpdressedman.co.uk/
http://coffincompany.co.uk/
http://horsesculptures.co.uk/
If I've helped you out, why not buy me a beer? http://craigmurray.me.uk


User avatar
Active Member

Posts

Joined
Wed Mar 24, 2010 9:07 pm
Location - London

Post by arash123arash » Thu Jul 07, 2011 11:36 pm

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);
}
});
});
});

Newbie

Posts

Joined
Thu Jul 07, 2011 11:34 pm

Post by Qphoria » Thu Jul 07, 2011 11:47 pm

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

Image


User avatar
Administrator

Posts

Joined
Tue Jul 22, 2008 3:02 am

Post by davidfeldt » Tue Jul 31, 2012 12:38 am

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.

Newbie

Posts

Joined
Tue Mar 29, 2011 10:01 am
Who is online

Users browsing this forum: No registered users and 79 guests