shop.balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

cart.php (17264B)


      1 <?php
      2 class ControllerCheckoutCart extends Controller {
      3 	public function index() {
      4 		$this->load->language('checkout/cart');
      5 
      6 		$this->document->setTitle($this->language->get('heading_title'));
      7 
      8 		$data['breadcrumbs'] = array();
      9 
     10 		$data['breadcrumbs'][] = array(
     11 			'href' => $this->url->link('common/home'),
     12 			'text' => $this->language->get('text_home')
     13 		);
     14 
     15 		$data['breadcrumbs'][] = array(
     16 			'href' => $this->url->link('checkout/cart'),
     17 			'text' => $this->language->get('heading_title')
     18 		);
     19 
     20 		if ($this->cart->hasProducts() || !empty($this->session->data['vouchers'])) {
     21 			if (!$this->cart->hasStock() && (!$this->config->get('config_stock_checkout') || $this->config->get('config_stock_warning'))) {
     22 				$data['error_warning'] = $this->language->get('error_stock');
     23 			} elseif (isset($this->session->data['error'])) {
     24 				$data['error_warning'] = $this->session->data['error'];
     25 
     26 				unset($this->session->data['error']);
     27 			} else {
     28 				$data['error_warning'] = '';
     29 			}
     30 
     31 			if ($this->config->get('config_customer_price') && !$this->customer->isLogged()) {
     32 				$data['attention'] = sprintf($this->language->get('text_login'), $this->url->link('account/login'), $this->url->link('account/register'));
     33 			} else {
     34 				$data['attention'] = '';
     35 			}
     36 
     37 			if (isset($this->session->data['success'])) {
     38 				$data['success'] = $this->session->data['success'];
     39 
     40 				unset($this->session->data['success']);
     41 			} else {
     42 				$data['success'] = '';
     43 			}
     44 
     45 			$data['action'] = $this->url->link('checkout/cart/edit', '', true);
     46 
     47 			if ($this->config->get('config_cart_weight')) {
     48 				$data['weight'] = $this->weight->format($this->cart->getWeight(), $this->config->get('config_weight_class_id'), $this->language->get('decimal_point'), $this->language->get('thousand_point'));
     49 			} else {
     50 				$data['weight'] = '';
     51 			}
     52 
     53 			$this->load->model('tool/image');
     54 			$this->load->model('tool/upload');
     55 
     56 			$data['products'] = array();
     57 
     58 			$products = $this->cart->getProducts();
     59 
     60 			foreach ($products as $product) {
     61 				$product_total = 0;
     62 
     63 				foreach ($products as $product_2) {
     64 					if ($product_2['product_id'] == $product['product_id']) {
     65 						$product_total += $product_2['quantity'];
     66 					}
     67 				}
     68 
     69 				if ($product['minimum'] > $product_total) {
     70 					$data['error_warning'] = sprintf($this->language->get('error_minimum'), $product['name'], $product['minimum']);
     71 				}
     72 
     73 				if ($product['image']) {
     74 					$image = $this->model_tool_image->resize($product['image'], $this->config->get('theme_' . $this->config->get('config_theme') . '_image_cart_width'), $this->config->get('theme_' . $this->config->get('config_theme') . '_image_cart_height'));
     75 				} else {
     76 					$image = '';
     77 				}
     78 
     79 				$option_data = array();
     80 
     81 				foreach ($product['option'] as $option) {
     82 					if ($option['type'] != 'file') {
     83 						$value = $option['value'];
     84 					} else {
     85 						$upload_info = $this->model_tool_upload->getUploadByCode($option['value']);
     86 
     87 						if ($upload_info) {
     88 							$value = $upload_info['name'];
     89 						} else {
     90 							$value = '';
     91 						}
     92 					}
     93 
     94 					$option_data[] = array(
     95 						'name'  => $option['name'],
     96 						'value' => (utf8_strlen($value) > 20 ? utf8_substr($value, 0, 20) . '..' : $value)
     97 					);
     98 				}
     99 
    100 				// Display prices
    101 				if ($this->customer->isLogged() || !$this->config->get('config_customer_price')) {
    102 					$unit_price = $this->tax->calculate($product['price'], $product['tax_class_id'], $this->config->get('config_tax'));
    103 					
    104 					$price = $this->currency->format($unit_price, $this->session->data['currency']);
    105 					$total = $this->currency->format($unit_price * $product['quantity'], $this->session->data['currency']);
    106 				} else {
    107 					$price = false;
    108 					$total = false;
    109 				}
    110 
    111 				$recurring = '';
    112 
    113 				if ($product['recurring']) {
    114 					$frequencies = array(
    115 						'day'        => $this->language->get('text_day'),
    116 						'week'       => $this->language->get('text_week'),
    117 						'semi_month' => $this->language->get('text_semi_month'),
    118 						'month'      => $this->language->get('text_month'),
    119 						'year'       => $this->language->get('text_year')
    120 					);
    121 
    122 					if ($product['recurring']['trial']) {
    123 						$recurring = sprintf($this->language->get('text_trial_description'), $this->currency->format($this->tax->calculate($product['recurring']['trial_price'] * $product['quantity'], $product['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']), $product['recurring']['trial_cycle'], $frequencies[$product['recurring']['trial_frequency']], $product['recurring']['trial_duration']) . ' ';
    124 					}
    125 
    126 					if ($product['recurring']['duration']) {
    127 						$recurring .= sprintf($this->language->get('text_payment_description'), $this->currency->format($this->tax->calculate($product['recurring']['price'] * $product['quantity'], $product['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']), $product['recurring']['cycle'], $frequencies[$product['recurring']['frequency']], $product['recurring']['duration']);
    128 					} else {
    129 						$recurring .= sprintf($this->language->get('text_payment_cancel'), $this->currency->format($this->tax->calculate($product['recurring']['price'] * $product['quantity'], $product['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']), $product['recurring']['cycle'], $frequencies[$product['recurring']['frequency']], $product['recurring']['duration']);
    130 					}
    131 				}
    132 
    133 				$data['products'][] = array(
    134 					'cart_id'   => $product['cart_id'],
    135 					'thumb'     => $image,
    136 					'name'      => $product['name'],
    137 					'model'     => $product['model'],
    138 					'option'    => $option_data,
    139 					'recurring' => $recurring,
    140 					'quantity'  => $product['quantity'],
    141 					'stock'     => $product['stock'] ? true : !(!$this->config->get('config_stock_checkout') || $this->config->get('config_stock_warning')),
    142 					'reward'    => ($product['reward'] ? sprintf($this->language->get('text_points'), $product['reward']) : ''),
    143 					'price'     => $price,
    144 					'total'     => $total,
    145 					'href'      => $this->url->link('product/product', 'product_id=' . $product['product_id'])
    146 				);
    147 			}
    148 
    149 			// Gift Voucher
    150 			$data['vouchers'] = array();
    151 
    152 			if (!empty($this->session->data['vouchers'])) {
    153 				foreach ($this->session->data['vouchers'] as $key => $voucher) {
    154 					$data['vouchers'][] = array(
    155 						'key'         => $key,
    156 						'description' => $voucher['description'],
    157 						'amount'      => $this->currency->format($voucher['amount'], $this->session->data['currency']),
    158 						'remove'      => $this->url->link('checkout/cart', 'remove=' . $key)
    159 					);
    160 				}
    161 			}
    162 
    163 			// Totals
    164 			$this->load->model('setting/extension');
    165 
    166 			$totals = array();
    167 			$taxes = $this->cart->getTaxes();
    168 			$total = 0;
    169 			
    170 			// Because __call can not keep var references so we put them into an array. 			
    171 			$total_data = array(
    172 				'totals' => &$totals,
    173 				'taxes'  => &$taxes,
    174 				'total'  => &$total
    175 			);
    176 			
    177 			// Display prices
    178 			if ($this->customer->isLogged() || !$this->config->get('config_customer_price')) {
    179 				$sort_order = array();
    180 
    181 				$results = $this->model_setting_extension->getExtensions('total');
    182 
    183 				foreach ($results as $key => $value) {
    184 					$sort_order[$key] = $this->config->get('total_' . $value['code'] . '_sort_order');
    185 				}
    186 
    187 				array_multisort($sort_order, SORT_ASC, $results);
    188 
    189 				foreach ($results as $result) {
    190 					if ($this->config->get('total_' . $result['code'] . '_status')) {
    191 						$this->load->model('extension/total/' . $result['code']);
    192 						
    193 						// We have to put the totals in an array so that they pass by reference.
    194 						$this->{'model_extension_total_' . $result['code']}->getTotal($total_data);
    195 					}
    196 				}
    197 
    198 				$sort_order = array();
    199 
    200 				foreach ($totals as $key => $value) {
    201 					$sort_order[$key] = $value['sort_order'];
    202 				}
    203 
    204 				array_multisort($sort_order, SORT_ASC, $totals);
    205 			}
    206 
    207 			$data['totals'] = array();
    208 
    209 			foreach ($totals as $total) {
    210 				$data['totals'][] = array(
    211 					'title' => $total['title'],
    212 					'text'  => $this->currency->format($total['value'], $this->session->data['currency'])
    213 				);
    214 			}
    215 
    216 			$data['continue'] = $this->url->link('common/home');
    217 
    218 			$data['checkout'] = $this->url->link('checkout/checkout', '', true);
    219 
    220 			$this->load->model('setting/extension');
    221 
    222 			$data['modules'] = array();
    223 			
    224 			$files = glob(DIR_APPLICATION . '/controller/extension/total/*.php');
    225 
    226 			if ($files) {
    227 				foreach ($files as $file) {
    228 					$result = $this->load->controller('extension/total/' . basename($file, '.php'));
    229 					
    230 					if ($result) {
    231 						$data['modules'][] = $result;
    232 					}
    233 				}
    234 			}
    235 
    236 			$data['column_left'] = $this->load->controller('common/column_left');
    237 			$data['column_right'] = $this->load->controller('common/column_right');
    238 			$data['content_top'] = $this->load->controller('common/content_top');
    239 			$data['content_bottom'] = $this->load->controller('common/content_bottom');
    240 			$data['footer'] = $this->load->controller('common/footer');
    241 			$data['header'] = $this->load->controller('common/header');
    242 
    243 			$this->response->setOutput($this->load->view('checkout/cart', $data));
    244 		} else {
    245 			$data['text_error'] = $this->language->get('text_empty');
    246 			
    247 			$data['continue'] = $this->url->link('common/home');
    248 
    249 			unset($this->session->data['success']);
    250 
    251 			$data['column_left'] = $this->load->controller('common/column_left');
    252 			$data['column_right'] = $this->load->controller('common/column_right');
    253 			$data['content_top'] = $this->load->controller('common/content_top');
    254 			$data['content_bottom'] = $this->load->controller('common/content_bottom');
    255 			$data['footer'] = $this->load->controller('common/footer');
    256 			$data['header'] = $this->load->controller('common/header');
    257 
    258 			$this->response->setOutput($this->load->view('error/not_found', $data));
    259 		}
    260 	}
    261 
    262 	public function add() {
    263 		$this->load->language('checkout/cart');
    264 
    265 		$json = array();
    266 
    267 		if (isset($this->request->post['product_id'])) {
    268 			$product_id = (int)$this->request->post['product_id'];
    269 		} else {
    270 			$product_id = 0;
    271 		}
    272 
    273 		$this->load->model('catalog/product');
    274 
    275 		$product_info = $this->model_catalog_product->getProduct($product_id);
    276 
    277 		if ($product_info) {
    278 			if (isset($this->request->post['quantity'])) {
    279 				$quantity = (int)$this->request->post['quantity'];
    280 			} else {
    281 				$quantity = 1;
    282 			}
    283 
    284 			if (isset($this->request->post['option'])) {
    285 				$option = array_filter($this->request->post['option']);
    286 			} else {
    287 				$option = array();
    288 			}
    289 
    290 			$product_options = $this->model_catalog_product->getProductOptions($this->request->post['product_id']);
    291 
    292 			foreach ($product_options as $product_option) {
    293 				if ($product_option['required'] && empty($option[$product_option['product_option_id']])) {
    294 					$json['error']['option'][$product_option['product_option_id']] = sprintf($this->language->get('error_required'), $product_option['name']);
    295 				}
    296 			}
    297 
    298 			if (isset($this->request->post['recurring_id'])) {
    299 				$recurring_id = $this->request->post['recurring_id'];
    300 			} else {
    301 				$recurring_id = 0;
    302 			}
    303 
    304 			$recurrings = $this->model_catalog_product->getProfiles($product_info['product_id']);
    305 
    306 			if ($recurrings) {
    307 				$recurring_ids = array();
    308 
    309 				foreach ($recurrings as $recurring) {
    310 					$recurring_ids[] = $recurring['recurring_id'];
    311 				}
    312 
    313 				if (!in_array($recurring_id, $recurring_ids)) {
    314 					$json['error']['recurring'] = $this->language->get('error_recurring_required');
    315 				}
    316 			}
    317 
    318 			if (!$json) {
    319 				$this->cart->add($this->request->post['product_id'], $quantity, $option, $recurring_id);
    320 
    321 				$json['success'] = sprintf($this->language->get('text_success'), $this->url->link('product/product', 'product_id=' . $this->request->post['product_id']), $product_info['name'], $this->url->link('checkout/cart'));
    322 
    323 				// Unset all shipping and payment methods
    324 				unset($this->session->data['shipping_method']);
    325 				unset($this->session->data['shipping_methods']);
    326 				unset($this->session->data['payment_method']);
    327 				unset($this->session->data['payment_methods']);
    328 
    329 				// Totals
    330 				$this->load->model('setting/extension');
    331 
    332 				$totals = array();
    333 				$taxes = $this->cart->getTaxes();
    334 				$total = 0;
    335 		
    336 				// Because __call can not keep var references so we put them into an array. 			
    337 				$total_data = array(
    338 					'totals' => &$totals,
    339 					'taxes'  => &$taxes,
    340 					'total'  => &$total
    341 				);
    342 
    343 				// Display prices
    344 				if ($this->customer->isLogged() || !$this->config->get('config_customer_price')) {
    345 					$sort_order = array();
    346 
    347 					$results = $this->model_setting_extension->getExtensions('total');
    348 
    349 					foreach ($results as $key => $value) {
    350 						$sort_order[$key] = $this->config->get('total_' . $value['code'] . '_sort_order');
    351 					}
    352 
    353 					array_multisort($sort_order, SORT_ASC, $results);
    354 
    355 					foreach ($results as $result) {
    356 						if ($this->config->get('total_' . $result['code'] . '_status')) {
    357 							$this->load->model('extension/total/' . $result['code']);
    358 
    359 							// We have to put the totals in an array so that they pass by reference.
    360 							$this->{'model_extension_total_' . $result['code']}->getTotal($total_data);
    361 						}
    362 					}
    363 
    364 					$sort_order = array();
    365 
    366 					foreach ($totals as $key => $value) {
    367 						$sort_order[$key] = $value['sort_order'];
    368 					}
    369 
    370 					array_multisort($sort_order, SORT_ASC, $totals);
    371 				}
    372 
    373 				$json['total'] = sprintf($this->language->get('text_items'), $this->cart->countProducts() + (isset($this->session->data['vouchers']) ? count($this->session->data['vouchers']) : 0), $this->currency->format($total, $this->session->data['currency']));
    374 			} else {
    375 				$json['redirect'] = str_replace('&amp;', '&', $this->url->link('product/product', 'product_id=' . $this->request->post['product_id']));
    376 			}
    377 		}
    378 
    379 		$this->response->addHeader('Content-Type: application/json');
    380 		$this->response->setOutput(json_encode($json));
    381 	}
    382 
    383 	public function edit() {
    384 		$this->load->language('checkout/cart');
    385 
    386 		$json = array();
    387 
    388 		// Update
    389 		if (!empty($this->request->post['quantity'])) {
    390 			foreach ($this->request->post['quantity'] as $key => $value) {
    391 				$this->cart->update($key, $value);
    392 			}
    393 
    394 			$this->session->data['success'] = $this->language->get('text_remove');
    395 
    396 			unset($this->session->data['shipping_method']);
    397 			unset($this->session->data['shipping_methods']);
    398 			unset($this->session->data['payment_method']);
    399 			unset($this->session->data['payment_methods']);
    400 			unset($this->session->data['reward']);
    401 
    402 			$this->response->redirect($this->url->link('checkout/cart'));
    403 		}
    404 
    405 		$this->response->addHeader('Content-Type: application/json');
    406 		$this->response->setOutput(json_encode($json));
    407 	}
    408 
    409 	public function remove() {
    410 		$this->load->language('checkout/cart');
    411 
    412 		$json = array();
    413 
    414 		// Remove
    415 		if (isset($this->request->post['key'])) {
    416 			$this->cart->remove($this->request->post['key']);
    417 
    418 			unset($this->session->data['vouchers'][$this->request->post['key']]);
    419 
    420 			$json['success'] = $this->language->get('text_remove');
    421 
    422 			unset($this->session->data['shipping_method']);
    423 			unset($this->session->data['shipping_methods']);
    424 			unset($this->session->data['payment_method']);
    425 			unset($this->session->data['payment_methods']);
    426 			unset($this->session->data['reward']);
    427 
    428 			// Totals
    429 			$this->load->model('setting/extension');
    430 
    431 			$totals = array();
    432 			$taxes = $this->cart->getTaxes();
    433 			$total = 0;
    434 
    435 			// Because __call can not keep var references so we put them into an array. 			
    436 			$total_data = array(
    437 				'totals' => &$totals,
    438 				'taxes'  => &$taxes,
    439 				'total'  => &$total
    440 			);
    441 
    442 			// Display prices
    443 			if ($this->customer->isLogged() || !$this->config->get('config_customer_price')) {
    444 				$sort_order = array();
    445 
    446 				$results = $this->model_setting_extension->getExtensions('total');
    447 
    448 				foreach ($results as $key => $value) {
    449 					$sort_order[$key] = $this->config->get('total_' . $value['code'] . '_sort_order');
    450 				}
    451 
    452 				array_multisort($sort_order, SORT_ASC, $results);
    453 
    454 				foreach ($results as $result) {
    455 					if ($this->config->get('total_' . $result['code'] . '_status')) {
    456 						$this->load->model('extension/total/' . $result['code']);
    457 
    458 						// We have to put the totals in an array so that they pass by reference.
    459 						$this->{'model_extension_total_' . $result['code']}->getTotal($total_data);
    460 					}
    461 				}
    462 
    463 				$sort_order = array();
    464 
    465 				foreach ($totals as $key => $value) {
    466 					$sort_order[$key] = $value['sort_order'];
    467 				}
    468 
    469 				array_multisort($sort_order, SORT_ASC, $totals);
    470 			}
    471 
    472 			$json['total'] = sprintf($this->language->get('text_items'), $this->cart->countProducts() + (isset($this->session->data['vouchers']) ? count($this->session->data['vouchers']) : 0), $this->currency->format($total, $this->session->data['currency']));
    473 		}
    474 
    475 		$this->response->addHeader('Content-Type: application/json');
    476 		$this->response->setOutput(json_encode($json));
    477 	}
    478 }