Jump to content
Sign in to follow this  
cha0s

Модуль ajax live update price with options

Recommended Posts

Наткнулся в интернете на один хороший бесплатный модуль ( http://www.opencart.com/index.php?route=extension/extension/info&extension_id=14760&filter_search=Live%20price&filter_license=0 ), разработчик забил..., с автоматической обновлением цены при выборе каких либо опций, или количества продукта, все хорошо но в нём есть бага не работают Checkbox почему-то, пытаюсь разобраться что к чему, код подверг уже модернизации со стороны php, то есть Checkbox отмеченный и его цены в php он видит, объясните почему ajax не срабатывает при этом.... с radio/select все супер работает, а вот с checkbox фэйл какой-то=(, я так полагаю что php и вовсе можно не смотреть, а только скорей сам низ где идет javascript, есть идеи вообще почему, не обновляется цена? ведь с Radio/select все отлично работает....

<?php 
class ControllerProductAjaxoptions5 extends Controller {
		
	public $options_container 			= '.results5'; 			// in default them it is 			".product-info"
	public $special_price_container 	= '.price-new'; 			// in default them it is 			".price-new"
	public $old_price_container		 	= '.price-old'; 			// in default them it is 			".price-old"
	public $tax_price_container 		= '.price-tax'; 			// in default them it is 			".price-tax'"
	public $price_container				= '#price_container5'; 		// by default this module sets 		"#price_container"
	public $use_cache					= true;						// set FALSE to disable caching (TRUE - enable)
	public $calculate_quantity			= true;						// calculate price with quantity
	
	private $error = array(); 
	
	public function index() { 
		
		$json = array();
		$update_cache = false;
		$options_makeup = 0;
		
		if (isset($this->request->get['product_id'])) {
			$product_id = (int)$this->request->get['product_id'];
		} else {
			$product_id = 0;
		}
		
		if ($this->calculate_quantity && isset($this->request->post['quantity'])) {
			$quantity = (int)$this->request->post['quantity'];
		} else {
			$quantity = 1;
		}
					
		$this->language->load('product/product');
		$this->load->model('catalog/product');
		
		// Cache name
		if (isset($this->request->post['option']) && is_array($this->request->post['option'])) {
			$options_hash = serialize($this->request->post['option']);
		} else {
			$options_hash = '';
		}
		
		$cache_key = 'ajax_options5_'. md5($product_id . $quantity. $options_hash . $this->currency->getCode() . $this->session->data['language']);
		
		if (!$this->use_cache || (!$json = $this->cache->get($cache_key))) {
				
			$product_info = $this->model_catalog_product->getProduct($product_id);			
			// Prepare data
			if ($product_info) {
			
				$update_cache = true;
							
				if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
					$this->data['price'] = $this->tax->calculate($product_info['price'], $product_info['tax_class_id'], $this->config->get('config_tax'));
				} else {
					$this->data['price'] = false;
				}
							
				if ((float)$product_info['special']) {
					$this->data['special'] = $this->tax->calculate($product_info['special'], $product_info['tax_class_id'], $this->config->get('config_tax'));
				} else {
					$this->data['special'] = false;
				}
				$i=0;
				// If some options are selected
				if (isset($this->request->post['option']) && $this->request->post['option']) {				
					foreach ($this->model_catalog_product->getProductOptions($this->request->get['product_id']) as $option) { 	
						foreach ($option['option_value'] as $option_value) {
							if (isset($this->request->post['option'][$option['product_option_id']]) && $this->request->post['option'][$option['product_option_id']] == $option_value['product_option_value_id']) {
								if (!$option_value['subtract'] || ($option_value['quantity'] > 0)) {
									
									if ((($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) && (float)$option_value['price']) {
										$price = $this->tax->calculate($option_value['price'], $product_info['tax_class_id'], $this->config->get('config_tax'));
									} else {
										$price = false;
									}
																		
									if ($price) {
										if ($option_value['price_prefix'] === '+') {
											$options_makeup = $options_makeup + (float)$price;
										} else {
											$options_makeup = $options_makeup - (float)$price;
										}
									}
								}
							}															
							if (isset($this->request->post['option'][$option['product_option_id']][$i]) && $this->request->post['option'][$option['product_option_id']][$i] == $option_value['product_option_value_id']) { 							
								$i++;														
								if (!$option_value['subtract'] || ($option_value['quantity'] > 0)) {
									
									if ((($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) && (float)$option_value['price']) {
										$price = $this->tax->calculate($option_value['price'], $product_info['tax_class_id'], $this->config->get('config_tax'));
									} else {
										$price = false;
									}
												
									if ($price) {
										if ($option_value['price_prefix'][$i] === '+') {
											$options_makeup = $options_makeup + (float)$price;
										} else {
											$options_makeup = $options_makeup - (float)$price;
										}
									}
								}
							}
							
							
							
							
						}
					}
				
				}
				
				if ($this->data['price']) {
					$this->data['price'] = 0;
					$json['new_price']['price'] = $this->currency->format(($this->data['price'] + $options_makeup) * $quantity);
				} else {
					$json['new_price']['price'] = false;
				}
				
				if ($this->data['special']) {
					$json['new_price']['special'] = $this->currency->format(($this->data['special'] + $options_makeup) * $quantity);
				} else {
					$json['new_price']['special'] = false;
				}
		
				if ($this->config->get('config_tax')) {
					$json['new_price']['tax'] = $this->language->get('text_tax').' '.$this->currency->format(((float)$product_info['special'] ? ($product_info['special'] + $options_makeup) : ($product_info['price'] + $options_makeup)) * $quantity );
				} else {
					$json['new_price']['tax'] = false;
				}
				
				$json['success'] = true;
				
			} else {
				$json['success'] = false;
			}
		
		}
		
		if ($update_cache && $this->use_cache) {
			$this->cache->set($cache_key, $json);
		}
		
		echo json_encode($json);
		exit;
  	}
	
	function js5() {
		
		header('Content-Type: application/javascript'); 
		
		if (isset($this->request->get['product_id'])) {
			$product_id = (int)$this->request->get['product_id'];
		} else {
			$product_id = 0;
		}
		
		if ($product_id == 0) {
			exit;
		}
		
		$js5 = <<<HTML
	
			
			var price_with_options_ajax_call5 = function() {
													$.ajax({
														type: 'POST',
														url: 'index.php?route=product/ajaxoptions5/index&product_id=$product_id',
														data: $('{$this->options_container} input[type=\'text\'], {$this->options_container} input[type=\'hidden\'], {$this->options_container} input[type=\'radio\']:checked, {$this->options_container} input[type=\'checkbox\']:checked, {$this->options_container} select, {$this->options_container} textarea'),
														dataType: 'json',
														beforeSend: function() {
															// you can add smth useful here
														},
														complete: function() {
															// you can add smth useful here
														},
														success: function(json) {
															if (json.success) {
																if ($('{$this->options_container} {$this->tax_price_container}').length > 0 && json.new_price.tax) {
																	animation_on_change_price_with_options5('{$this->options_container} {$this->tax_price_container}', json.new_price.tax);
																}
																if ($('{$this->options_container} {$this->old_price_container}').length > 0 && json.new_price.price) {
																	animation_on_change_price_with_options5('{$this->options_container} {$this->old_price_container}', json.new_price.price);
																}
																if ($('{$this->options_container} {$this->special_price_container}').length > 0 && json.new_price.special) {
																	animation_on_change_price_with_options5('{$this->options_container} {$this->special_price_container}', json.new_price.special);
																}
																if ($('{$this->options_container} {$this->price_container}').length > 0 && json.new_price.price) {
																	animation_on_change_price_with_options5('{$this->options_container} {$this->price_container}', json.new_price.price);
																}
															}
														},
														error: function(error) {
															console.log(error);
														}
													});
												}
			
			var animation_on_change_price_with_options5 = function(selector_class_or_id, new_html_content) {
				$(selector_class_or_id).fadeOut(150, function() {
					$(this).html(new_html_content).fadeIn(50);
				});
			}
			
			if ( jQuery.isFunction(jQuery.fn.on) ) 
				{
					$('{$this->options_container} input[type=\'text\'], {$this->options_container} input[type=\'hidden\'], {$this->options_container} input[type=\'radio\']:checked, {$this->options_container} input[type=\'checkbox\'], {$this->options_container} select, {$this->options_container} textarea, .product-info input[name=\'quantity\']').live('change', function() {
						price_with_options_ajax_call5();
					});
				} 
			else 
				{
					$('{$this->options_container} input[type=\'text\'], {$this->options_container} input[type=\'hidden\'], {$this->options_container} input[type=\'radio\']:checked, {$this->options_container} input[type=\'checkbox\'], {$this->options_container} select, {$this->options_container} textarea, .product-info input[name=\'quantity\']').live('change', function() {
						price_with_options_ajax_call5();
					});
				}	
HTML;

		echo $js5;
		exit;
	}
}

?>

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
You are posting as a guest. If you have an account, please sign in.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×

Important Information

On our site, cookies are used and personal data is processed to improve the user interface. To find out what and what personal data we are processing, please go to the link. If you click "I agree," it means that you understand and accept all the conditions specified in this Privacy Notice.