shop.balmet.com

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

cart.php (8797B)


      1 <?php
      2 class ControllerApiCart extends Controller {
      3 	public function add() {
      4 		$this->load->language('api/cart');
      5 
      6 		$json = array();
      7 			
      8 		if (!isset($this->session->data['api_id'])) {
      9 			$json['error']['warning'] = $this->language->get('error_permission');
     10 		} else {
     11 			if (isset($this->request->post['product'])) {
     12 				$this->cart->clear();
     13 
     14 				foreach ($this->request->post['product'] as $product) {
     15 					if (isset($product['option'])) {
     16 						$option = $product['option'];
     17 					} else {
     18 						$option = array();
     19 					}
     20 
     21 					$this->cart->add($product['product_id'], $product['quantity'], $option);
     22 				}
     23 
     24 				$json['success'] = $this->language->get('text_success');
     25 
     26 				unset($this->session->data['shipping_method']);
     27 				unset($this->session->data['shipping_methods']);
     28 				unset($this->session->data['payment_method']);
     29 				unset($this->session->data['payment_methods']);
     30 			} elseif (isset($this->request->post['product_id'])) {
     31 				$this->load->model('catalog/product');
     32 
     33 				$product_info = $this->model_catalog_product->getProduct($this->request->post['product_id']);
     34 
     35 				if ($product_info) {
     36 					if (isset($this->request->post['quantity'])) {
     37 						$quantity = $this->request->post['quantity'];
     38 					} else {
     39 						$quantity = 1;
     40 					}
     41 
     42 					if (isset($this->request->post['option'])) {
     43 						$option = array_filter($this->request->post['option']);
     44 					} else {
     45 						$option = array();
     46 					}
     47 
     48 					$product_options = $this->model_catalog_product->getProductOptions($this->request->post['product_id']);
     49 
     50 					foreach ($product_options as $product_option) {
     51 						if ($product_option['required'] && empty($option[$product_option['product_option_id']])) {
     52 							$json['error']['option'][$product_option['product_option_id']] = sprintf($this->language->get('error_required'), $product_option['name']);
     53 						}
     54 					}
     55 
     56 					if (!isset($json['error']['option'])) {
     57 						$this->cart->add($this->request->post['product_id'], $quantity, $option);
     58 
     59 						$json['success'] = $this->language->get('text_success');
     60 
     61 						unset($this->session->data['shipping_method']);
     62 						unset($this->session->data['shipping_methods']);
     63 						unset($this->session->data['payment_method']);
     64 						unset($this->session->data['payment_methods']);
     65 					}
     66 				} else {
     67 					$json['error']['store'] = $this->language->get('error_store');
     68 				}
     69 			}
     70 		}
     71 
     72 		$this->response->addHeader('Content-Type: application/json');
     73 		$this->response->setOutput(json_encode($json));
     74 	}
     75 
     76 	public function edit() {
     77 		$this->load->language('api/cart');
     78 
     79 		$json = array();
     80 
     81 		if (!isset($this->session->data['api_id'])) {
     82 			$json['error'] = $this->language->get('error_permission');
     83 		} else {
     84 			$this->cart->update($this->request->post['key'], $this->request->post['quantity']);
     85 
     86 			$json['success'] = $this->language->get('text_success');
     87 
     88 			unset($this->session->data['shipping_method']);
     89 			unset($this->session->data['shipping_methods']);
     90 			unset($this->session->data['payment_method']);
     91 			unset($this->session->data['payment_methods']);
     92 			unset($this->session->data['reward']);
     93 		}
     94 
     95 		$this->response->addHeader('Content-Type: application/json');
     96 		$this->response->setOutput(json_encode($json));
     97 	}
     98 
     99 	public function remove() {
    100 		$this->load->language('api/cart');
    101 
    102 		$json = array();
    103 
    104 		if (!isset($this->session->data['api_id'])) {
    105 			$json['error'] = $this->language->get('error_permission');
    106 		} else {
    107 			// Remove
    108 			if (isset($this->request->post['key'])) {
    109 				$this->cart->remove($this->request->post['key']);
    110 
    111 				unset($this->session->data['vouchers'][$this->request->post['key']]);
    112 
    113 				$json['success'] = $this->language->get('text_success');
    114 
    115 				unset($this->session->data['shipping_method']);
    116 				unset($this->session->data['shipping_methods']);
    117 				unset($this->session->data['payment_method']);
    118 				unset($this->session->data['payment_methods']);
    119 				unset($this->session->data['reward']);
    120 			}
    121 		}
    122 
    123 		$this->response->addHeader('Content-Type: application/json');
    124 		$this->response->setOutput(json_encode($json));
    125 	}
    126 
    127 	public function products() {
    128 		$this->load->language('api/cart');
    129 
    130 		$json = array();
    131 
    132 		if (!isset($this->session->data['api_id'])) {
    133 			$json['error']['warning'] = $this->language->get('error_permission');
    134 		} else {
    135 			// Stock
    136 			if (!$this->cart->hasStock() && (!$this->config->get('config_stock_checkout') || $this->config->get('config_stock_warning'))) {
    137 				$json['error']['stock'] = $this->language->get('error_stock');
    138 			}
    139 
    140 			// Products
    141 			$json['products'] = array();
    142 
    143 			$products = $this->cart->getProducts();
    144 
    145 			foreach ($products as $product) {
    146 				$product_total = 0;
    147 
    148 				foreach ($products as $product_2) {
    149 					if ($product_2['product_id'] == $product['product_id']) {
    150 						$product_total += $product_2['quantity'];
    151 					}
    152 				}
    153 
    154 				if ($product['minimum'] > $product_total) {
    155 					$json['error']['minimum'][] = sprintf($this->language->get('error_minimum'), $product['name'], $product['minimum']);
    156 				}
    157 
    158 				$option_data = array();
    159 
    160 				foreach ($product['option'] as $option) {
    161 					$option_data[] = array(
    162 						'product_option_id'       => $option['product_option_id'],
    163 						'product_option_value_id' => $option['product_option_value_id'],
    164 						'name'                    => $option['name'],
    165 						'value'                   => $option['value'],
    166 						'type'                    => $option['type']
    167 					);
    168 				}
    169 
    170 				$json['products'][] = array(
    171 					'cart_id'    => $product['cart_id'],
    172 					'product_id' => $product['product_id'],
    173 					'name'       => $product['name'],
    174 					'model'      => $product['model'],
    175 					'option'     => $option_data,
    176 					'quantity'   => $product['quantity'],
    177 					'stock'      => $product['stock'] ? true : !(!$this->config->get('config_stock_checkout') || $this->config->get('config_stock_warning')),
    178 					'shipping'   => $product['shipping'],
    179 					'price'      => $this->currency->format($this->tax->calculate($product['price'], $product['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']),
    180 					'total'      => $this->currency->format($this->tax->calculate($product['price'], $product['tax_class_id'], $this->config->get('config_tax')) * $product['quantity'], $this->session->data['currency']),
    181 					'reward'     => $product['reward']
    182 				);
    183 			}
    184 
    185 			// Voucher
    186 			$json['vouchers'] = array();
    187 
    188 			if (!empty($this->session->data['vouchers'])) {
    189 				foreach ($this->session->data['vouchers'] as $key => $voucher) {
    190 					$json['vouchers'][] = array(
    191 						'code'             => $voucher['code'],
    192 						'description'      => $voucher['description'],
    193 						'from_name'        => $voucher['from_name'],
    194 						'from_email'       => $voucher['from_email'],
    195 						'to_name'          => $voucher['to_name'],
    196 						'to_email'         => $voucher['to_email'],
    197 						'voucher_theme_id' => $voucher['voucher_theme_id'],
    198 						'message'          => $voucher['message'],
    199 						'price'            => $this->currency->format($voucher['amount'], $this->session->data['currency']),			
    200 						'amount'           => $voucher['amount']
    201 					);
    202 				}
    203 			}
    204 
    205 			// Totals
    206 			$this->load->model('setting/extension');
    207 
    208 			$totals = array();
    209 			$taxes = $this->cart->getTaxes();
    210 			$total = 0;
    211 
    212 			// Because __call can not keep var references so we put them into an array. 
    213 			$total_data = array(
    214 				'totals' => &$totals,
    215 				'taxes'  => &$taxes,
    216 				'total'  => &$total
    217 			);
    218 			
    219 			$sort_order = array();
    220 
    221 			$results = $this->model_setting_extension->getExtensions('total');
    222 
    223 			foreach ($results as $key => $value) {
    224 				$sort_order[$key] = $this->config->get('total_' . $value['code'] . '_sort_order');
    225 			}
    226 
    227 			array_multisort($sort_order, SORT_ASC, $results);
    228 
    229 			foreach ($results as $result) {
    230 				if ($this->config->get('total_' . $result['code'] . '_status')) {
    231 					$this->load->model('extension/total/' . $result['code']);
    232 					
    233 					// We have to put the totals in an array so that they pass by reference.
    234 					$this->{'model_extension_total_' . $result['code']}->getTotal($total_data);
    235 				}
    236 			}
    237 
    238 			$sort_order = array();
    239 
    240 			foreach ($totals as $key => $value) {
    241 				$sort_order[$key] = $value['sort_order'];
    242 			}
    243 
    244 			array_multisort($sort_order, SORT_ASC, $totals);
    245 
    246 			$json['totals'] = array();
    247 
    248 			foreach ($totals as $total) {
    249 				$json['totals'][] = array(
    250 					'title' => $total['title'],
    251 					'text'  => $this->currency->format($total['value'], $this->session->data['currency'])
    252 				);
    253 			}
    254 		}
    255 		
    256 		$this->response->addHeader('Content-Type: application/json');
    257 		$this->response->setOutput(json_encode($json));
    258 	}
    259 }