Post by twuncher » Sun Oct 24, 2010 6:48 am

Hi,

I currently don't use the cart module and have just got the number of items and total price in the header, I want to get it updated automatically with ajax once a product is purchased like in the cart module, can anyone please help? my code is below.


Code: Select all

<div class="topmenu" id="module_cart">
	
			 <?php echo $text_greeting; ?>
 			 <?php if (!$logged) { ?>
 					 <a href="<?php echo $login; ?>"><?php echo $text_login; ?></a> - <a href="<?php echo $create; ?>"><?php echo $text_create; ?></a>
 				 <?php } else { ?>
 					 <a href="<?php echo $logout; ?>"><?php echo $text_logout; ?></a> - <a href="<?php echo $account; ?>"><?php echo $text_account; ?></a>
 				 <?php } ?>
		
		    <?php if ($products) { ?>    
		        <a href="index.php?route=checkout/cart">(<?php echo $this->cart->countProducts(); ?>)   
		       <?php 
		       $count=$this->cart->countProducts();
				   if($count >1)
				       {
			                        echo $text_shoppingbag; echo 's';
					  }
		        				else echo $text_shoppingbag ?>   
		        <?php			
		               			$count=count($totals);
					        $cartinfo = $totals[$count-1]['text'];           
					        echo $cartinfo;  ?>       
                                                </a>
		         
		        <?php } else { ?>  
		         <a href="index.php?route=checkout/cart">(<?php echo $this->cart->countProducts(); ?>)                   
                         <?php echo $text_shoppingbag; ?></a>   
		        <?php		        
		        foreach ($totals as $total) { 
		        if ($total['title'] != 'Sub-Total:') {
		                echo $total['text'];
		            }
		           }     
		        }       ?>           
		       		        
		        <?php if ($ajax) { ?>
			<script type="text/javascript" src="catalog/view/javascript/jquery/ajax_add.js"></script>
			<?php } ?>
                        </div>
Cheers

Newbie

Posts

Joined
Sat May 15, 2010 7:42 pm

Post by Maansy » Sun Oct 24, 2010 9:24 pm

In add_to_cart.js you need to specify which div should be updated when you click add to cart

ALL Templates :: 1.5.1+ Templates :: 50%-75% PRICE DROP ONLY at OpencartStuff.com


User avatar
Active Member

Posts

Joined
Thu Jun 24, 2010 6:04 am


Post by twuncher » Mon Oct 25, 2010 6:03 am

Hi Mansy,

thanks for that, I only wanted to show the number of items + total value, I had to edit the controller/module/cart.php and edit the callback function. after some trial and error and help from other threads I changed the callback function and managed to output what i wanted, i'll post the code incase its of any help to others :)

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()) {
			$this->language->load('module/cart');
			$this->data['text_shoppingbag'] = $this->language->get('text_shoppingbag');
    		$total = 0;
			$taxes = $this->cart->getTaxes();

			$this->load->model('checkout/extension');
			$this->data['text_checkout'] = $this->language->get('text_checkout');


			$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:') {



				$count=$this->cart->countProducts();

				$count=count($total_data);
					        $cartinfo = $total_data[$count-1]['text'];
					$output .= '(';
					$output .= $this->cart->countProducts();
					$output .= ') ';
								   if($count >1) {
								   		$output .= 'Items';
								   }

								   else $output .= 'Item';
				$output .= ' ';
		        $output .= $total['text'];
     		}
      	   }
		}

		$this->response->setOutput($output, $this->config->get('config_compression'));
	}

twuncher

Newbie

Posts

Joined
Sat May 15, 2010 7:42 pm

Post by twuncher » Mon Oct 25, 2010 6:56 am

think I spoke to early.. works in Firefox but not in IE8 :(

Newbie

Posts

Joined
Sat May 15, 2010 7:42 pm

Post by twuncher » Mon Oct 25, 2010 7:06 am

found out what the issue was, in theme/mytheme/template/common/header.tpl

needed to place

<?php if ($ajax) { ?>
<script type="text/javascript" src="catalog/view/javascript/jquery/ajax_add.js"></script>
<?php } ?>

it before the <div id="module_cart"> , previously it was inside it, which made firefox work but ie8 not!

Newbie

Posts

Joined
Sat May 15, 2010 7:42 pm
Who is online

Users browsing this forum: No registered users and 2 guests