Advertisement
Guest User

Untitled

a guest
Mar 28th, 2016
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.28 KB | None | 0 0
  1. <?php
  2.  
  3. // Name: Live Price Update with Options
  4. // Version: 1.0
  5. // Author: OpenCart FACTORY
  6. // Website: www.ocfactory.net
  7.  
  8. class ControllerProductLivepriceupdate extends Controller {
  9. /* ====================================================================================
  10.  
  11. SETTINGS
  12.  
  13. Below you can find five variables that relate to DOM the structure of the template product/product.tpl.
  14. The default values correspond to a default OpenCart theme.
  15. If you use customized theme, these containers might have other class or id. In this case you need to clarify their value.
  16.  
  17. ==================================================================================== */
  18.  
  19. public $options_container = '#product'; // in default them it is ".product-info"
  20. public $old_price_container = '#price_old'; // in default them it is ".price-old"
  21. public $tax_price_container = '#price_tax'; // in default them it is ".price-tax'"
  22. public $special_price_container = '#price_special'; // by default this module sets "#price_container"
  23. public $use_cache = true; // set FALSE to disable caching (TRUE - enable)
  24. public $calculate_quantity = true; // calculate price with quantity
  25.  
  26. private $error = array();
  27.  
  28. public function index() {
  29.  
  30. $json = array();
  31. $update_cache = false;
  32. $options_makeup = 0;
  33.  
  34. if (isset($this->request->post['product_id'])) {
  35. $product_id = (int)$this->request->post['product_id'];
  36. } else {
  37. $product_id = 0;
  38. }
  39.  
  40. if ($this->calculate_quantity && isset($this->request->post['quantity'])) {
  41. $quantity = (int)$this->request->post['quantity'];
  42. } else {
  43. $quantity = 1;
  44. }
  45.  
  46. $this->language->load('product/product');
  47. $this->load->model('catalog/product');
  48.  
  49. // Cache name
  50. if (isset($this->request->post['option']) && is_array($this->request->post['option'])) {
  51. $options_hash = serialize($this->request->post['option']);
  52. } else {
  53. $options_hash = '';
  54. }
  55.  
  56. $cache_key = 'live_price_update'. md5($product_id . $quantity. $options_hash . $this->currency->getCode() . $this->session->data['language']);
  57.  
  58. if (!$this->use_cache || (!$json = $this->cache->get($cache_key))) {
  59.  
  60. $product_info = $this->model_catalog_product->getProduct($product_id);
  61.  
  62. // Prepare data
  63. if ($product_info) {
  64.  
  65. $update_cache = true;
  66.  
  67. if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
  68. //$data['price'] = $this->tax->calculate($product_info['price'], $product_info['tax_class_id'], $this->config->get('config_tax'));
  69. $data['price'] = $product_info['price'];
  70. } else {
  71. $data['price'] = false;
  72. }
  73.  
  74. if ((float)$product_info['special']) {
  75. //$data['special'] = $this->tax->calculate($product_info['special'], $product_info['tax_class_id'], $this->config->get('config_tax'));
  76. $data['special'] = $product_info['special'];
  77. } else {
  78. $data['special'] = false;
  79. }
  80.  
  81. // If some options are selected
  82. if (isset($this->request->post['option']) && $this->request->post['option']) {
  83. foreach ($this->model_catalog_product->getProductOptions($product_id) as $option) {
  84. foreach ($option['product_option_value'] as $option_value) {
  85. //If options checkbox
  86. if(isset($this->request->post['option'][$option['product_option_id']]) && is_array($this->request->post['option'][$option['product_option_id']])) {
  87. array_filter($this->request->post['option'][$option['product_option_id']]);
  88. foreach($this->request->post['option'][$option['product_option_id']] as $checked_option) {
  89. if ($checked_option == $option_value['product_option_value_id']) {
  90. if (!$option_value['subtract'] || ($option_value['quantity'] > 0)) {
  91. if ((($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) && (float)$option_value['price']) {
  92. //$price = $this->tax->calculate($option_value['price'], $product_info['tax_class_id'], $this->config->get('config_tax'));
  93. $price = $option_value['price'];
  94. } else {
  95. $price = false;
  96. }
  97. if ($price) {
  98. if ($option_value['price_prefix'] === '+') {
  99. $options_makeup = $options_makeup + (float)$price;
  100. } else {
  101. $options_makeup = $options_makeup - (float)$price;
  102. }
  103. }
  104. }
  105. }
  106. }
  107. }
  108.  
  109. //If options not checkbox
  110. if (isset($this->request->post['option'][$option['product_option_id']]) && $this->request->post['option'][$option['product_option_id']] == $option_value['product_option_value_id']) {
  111. if (!$option_value['subtract'] || ($option_value['quantity'] > 0)) {
  112. if ((($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) && (float)$option_value['price']) {
  113. //$price = $this->tax->calculate($option_value['price'], $product_info['tax_class_id'], $this->config->get('config_tax'));
  114. $price = $option_value['price'];
  115. } else {
  116. $price = false;
  117. }
  118. if ($price) {
  119. if ($option_value['price_prefix'] === '+') {
  120. $options_makeup = $options_makeup + (float)$price;
  121. } else {
  122. $options_makeup = $options_makeup - (float)$price;
  123. }
  124. }
  125. }
  126. }
  127. }
  128. unset($price);
  129. }
  130. }
  131.  
  132. if ($data['price']) {
  133. $json['new_price']['price'] = $this->currency->format($this->tax->calculate(($data['price'] + $options_makeup), $product_info['tax_class_id'], $this->config->get('config_tax')) * $quantity);
  134. } else {
  135. $json['new_price']['price'] = false;
  136. }
  137.  
  138. if ($data['special']) {
  139. $json['new_price']['special'] = $this->currency->format($this->tax->calculate(($data['special'] + $options_makeup), $product_info['tax_class_id'], $this->config->get('config_tax')) * $quantity);
  140. } else {
  141. $json['new_price']['special'] = false;
  142. }
  143.  
  144. if ($this->config->get('config_tax')) {
  145. $json['new_price']['tax'] = $this->currency->format(((float)$product_info['special'] ? ($product_info['special'] + $options_makeup) : ($product_info['price'] + $options_makeup)) * $quantity );
  146. } else {
  147. $json['new_price']['tax'] = false;
  148. }
  149.  
  150. $json['success'] = true;
  151.  
  152. } else {
  153. $json['success'] = false;
  154. }
  155.  
  156. }
  157.  
  158. if ($update_cache && $this->use_cache) {
  159. $this->cache->set($cache_key, $json);
  160. }
  161.  
  162. echo json_encode($json);
  163. exit;
  164. }
  165.  
  166. function js() {
  167.  
  168. header('Content-Type: application/javascript');
  169.  
  170. $js = <<<HTML
  171.  
  172. var price_with_options_ajax_call = function() {
  173. $.ajax({
  174. type: 'POST',
  175. url: 'index.php?route=product/livepriceupdate/index',
  176. 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'),
  177. dataType: 'json',
  178. beforeSend: function() {
  179. // you can add smth useful here
  180. },
  181. complete: function() {
  182. // you can add smth useful here
  183. },
  184. success: function(json) {
  185. if (json.success) {
  186. animation_on_change_price_with_options('{$this->special_price_container}', json.new_price.special);
  187. animation_on_change_price_with_options('{$this->tax_price_container}', json.new_price.tax);
  188. animation_on_change_price_with_options('{$this->old_price_container}', json.new_price.price);
  189. }
  190. },
  191. error: function(error) {
  192. //console.log(error);
  193. }
  194. });
  195. }
  196.  
  197. var animation_on_change_price_with_options = function(selector_class_or_id, new_html_content) {
  198. $(selector_class_or_id).fadeOut(150, function() {
  199. $(this).html(new_html_content).fadeIn(50);
  200. });
  201. }
  202.  
  203. if ( jQuery.isFunction(jQuery.fn.on) )
  204. {
  205. $('{$this->options_container} input[type=\'text\'], {$this->options_container} input[type=\'hidden\'], {$this->options_container} input[type=\'radio\'], {$this->options_container} input[type=\'checkbox\'], {$this->options_container} select, {$this->options_container} textarea, {$this->options_container} input[name=\'quantity\']').on('change', function() {
  206. price_with_options_ajax_call();
  207. });
  208. }
  209. else
  210. {
  211. $('{$this->options_container} input[type=\'text\'], {$this->options_container} input[type=\'hidden\'], {$this->options_container} input[type=\'radio\'], {$this->options_container} input[type=\'checkbox\'], {$this->options_container} select, {$this->options_container} textarea, {$this->options_container} input[name=\'quantity\']').on('change', function() {
  212. price_with_options_ajax_call();
  213. });
  214. }
  215.  
  216. HTML;
  217.  
  218. echo $js;
  219. exit;
  220. }
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement