| Conditions | 42 |
| Paths | 4802 |
| Total Lines | 259 |
| Code Lines | 152 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 70 | public function getProducts() |
||
| 71 | { |
||
| 72 | $product_data = array(); |
||
| 73 | |||
| 74 | $cart_query = $this->db->query(" |
||
| 75 | SELECT * |
||
| 76 | FROM cart |
||
| 77 | WHERE customer_id = '" . (int) $this->customer->getId() . "' |
||
| 78 | AND session_id = '" . $this->db->escape($this->session->getSessionId()) . "' |
||
| 79 | "); |
||
| 80 | |||
| 81 | foreach ($cart_query->rows as $cart) { |
||
| 82 | $stock = true; |
||
| 83 | |||
| 84 | $product_query = $this->db->query(" |
||
| 85 | SELECT * |
||
| 86 | FROM product p |
||
| 87 | LEFT JOIN product_description pd ON (p.product_id = pd.product_id) |
||
| 88 | WHERE p.product_id = '" . (int) $cart['product_id'] . "' |
||
| 89 | AND pd.language_id = '" . (int) $this->config->get('config_language_id') . "' |
||
| 90 | AND p.status = '1' |
||
| 91 | "); |
||
| 92 | |||
| 93 | if ($product_query->num_rows && ($cart['quantity'] > 0)) { |
||
| 94 | $option_price = 0; |
||
| 95 | $option_points = 0; |
||
| 96 | |||
| 97 | $option_data = array(); |
||
| 98 | |||
| 99 | foreach (json_decode($cart['option']) as $product_option_id => $value) { |
||
| 100 | $option_query = $this->db->query(" |
||
| 101 | SELECT po.product_option_id, po.option_id, od.name, o.type |
||
| 102 | FROM product_option po |
||
| 103 | LEFT JOIN `option` o ON (po.option_id = o.option_id) |
||
| 104 | LEFT JOIN option_description od ON (o.option_id = od.option_id) |
||
| 105 | WHERE po.product_option_id = '" . (int) $product_option_id . "' |
||
| 106 | AND po.product_id = '" . (int) $cart['product_id'] . "' |
||
| 107 | AND od.language_id = '" . (int) $this->config->get('config_language_id') . "' |
||
| 108 | "); |
||
| 109 | |||
| 110 | if ($option_query->num_rows) { |
||
| 111 | if ($option_query->row['type'] == 'select' || $option_query->row['type'] == 'radio') { |
||
| 112 | $option_value_query = $this->db->query(" |
||
| 113 | SELECT pov.option_value_id, ovd.name, pov.quantity, pov.subtract, pov.price, pov.price_prefix, pov.points, pov.points_prefix |
||
| 114 | FROM product_option_value pov |
||
| 115 | LEFT JOIN option_value ov ON (pov.option_value_id = ov.option_value_id) |
||
| 116 | LEFT JOIN option_value_description ovd ON (ov.option_value_id = ovd.option_value_id) |
||
| 117 | WHERE pov.product_option_value_id = '" . (int) $value . "' |
||
| 118 | AND pov.product_option_id = '" . (int) $product_option_id . "' |
||
| 119 | AND ovd.language_id = '" . (int) $this->config->get('config_language_id') . "' |
||
| 120 | "); |
||
| 121 | |||
| 122 | if ($option_value_query->num_rows) { |
||
| 123 | if ($option_value_query->row['price_prefix'] == '+') { |
||
| 124 | $option_price += $option_value_query->row['price']; |
||
| 125 | } elseif ($option_value_query->row['price_prefix'] == '-') { |
||
| 126 | $option_price -= $option_value_query->row['price']; |
||
| 127 | } |
||
| 128 | |||
| 129 | if ($option_value_query->row['points_prefix'] == '+') { |
||
| 130 | $option_points += $option_value_query->row['points']; |
||
| 131 | } elseif ($option_value_query->row['points_prefix'] == '-') { |
||
| 132 | $option_points -= $option_value_query->row['points']; |
||
| 133 | } |
||
| 134 | |||
| 135 | if ($option_value_query->row['subtract'] && (!$option_value_query->row['quantity'] || ($option_value_query->row['quantity'] < $cart['quantity']))) { |
||
| 136 | $stock = false; |
||
| 137 | } |
||
| 138 | |||
| 139 | $option_data[] = array( |
||
| 140 | 'product_option_id' => $product_option_id, |
||
| 141 | 'product_option_value_id' => $value, |
||
| 142 | 'option_id' => $option_query->row['option_id'], |
||
| 143 | 'option_value_id' => $option_value_query->row['option_value_id'], |
||
| 144 | 'name' => $option_query->row['name'], |
||
| 145 | 'value' => $option_value_query->row['name'], |
||
| 146 | 'type' => $option_query->row['type'], |
||
| 147 | 'quantity' => $option_value_query->row['quantity'], |
||
| 148 | 'subtract' => $option_value_query->row['subtract'], |
||
| 149 | 'price' => $option_value_query->row['price'], |
||
| 150 | 'price_prefix' => $option_value_query->row['price_prefix'], |
||
| 151 | 'points' => $option_value_query->row['points'], |
||
| 152 | 'points_prefix' => $option_value_query->row['points_prefix'] |
||
| 153 | ); |
||
| 154 | } |
||
| 155 | } elseif ($option_query->row['type'] == 'checkbox' && is_array($value)) { |
||
| 156 | foreach ($value as $product_option_value_id) { |
||
| 157 | $option_value_query = $this->db->query(" |
||
| 158 | SELECT pov.option_value_id, pov.quantity, pov.subtract, pov.price, pov.price_prefix, pov.points, pov.points_prefix, ovd.name |
||
| 159 | FROM product_option_value pov |
||
| 160 | LEFT JOIN option_value_description ovd ON (pov.option_value_id = ovd.option_value_id) |
||
| 161 | WHERE pov.product_option_value_id = '" . (int) $product_option_value_id . "' |
||
| 162 | AND pov.product_option_id = '" . (int) $product_option_id . "' |
||
| 163 | AND ovd.language_id = '" . (int) $this->config->get('config_language_id') . "' |
||
| 164 | "); |
||
| 165 | |||
| 166 | if ($option_value_query->num_rows) { |
||
| 167 | if ($option_value_query->row['price_prefix'] == '+') { |
||
| 168 | $option_price += $option_value_query->row['price']; |
||
| 169 | } elseif ($option_value_query->row['price_prefix'] == '-') { |
||
| 170 | $option_price -= $option_value_query->row['price']; |
||
| 171 | } |
||
| 172 | |||
| 173 | if ($option_value_query->row['points_prefix'] == '+') { |
||
| 174 | $option_points += $option_value_query->row['points']; |
||
| 175 | } elseif ($option_value_query->row['points_prefix'] == '-') { |
||
| 176 | $option_points -= $option_value_query->row['points']; |
||
| 177 | } |
||
| 178 | |||
| 179 | if ($option_value_query->row['subtract'] && (!$option_value_query->row['quantity'] || ($option_value_query->row['quantity'] < $cart['quantity']))) { |
||
| 180 | $stock = false; |
||
| 181 | } |
||
| 182 | |||
| 183 | $option_data[] = array( |
||
| 184 | 'product_option_id' => $product_option_id, |
||
| 185 | 'product_option_value_id' => $product_option_value_id, |
||
| 186 | 'option_id' => $option_query->row['option_id'], |
||
| 187 | 'option_value_id' => $option_value_query->row['option_value_id'], |
||
| 188 | 'name' => $option_query->row['name'], |
||
| 189 | 'value' => $option_value_query->row['name'], |
||
| 190 | 'type' => $option_query->row['type'], |
||
| 191 | 'quantity' => $option_value_query->row['quantity'], |
||
| 192 | 'subtract' => $option_value_query->row['subtract'], |
||
| 193 | 'price' => $option_value_query->row['price'], |
||
| 194 | 'price_prefix' => $option_value_query->row['price_prefix'], |
||
| 195 | 'points' => $option_value_query->row['points'], |
||
| 196 | 'points_prefix' => $option_value_query->row['points_prefix'] |
||
| 197 | ); |
||
| 198 | } |
||
| 199 | } |
||
| 200 | } elseif ($option_query->row['type'] == 'text' || $option_query->row['type'] == 'textarea' || $option_query->row['type'] == 'file' || $option_query->row['type'] == 'date' || $option_query->row['type'] == 'datetime' || $option_query->row['type'] == 'time') { |
||
| 201 | $option_data[] = array( |
||
| 202 | 'product_option_id' => $product_option_id, |
||
| 203 | 'product_option_value_id' => '', |
||
| 204 | 'option_id' => $option_query->row['option_id'], |
||
| 205 | 'option_value_id' => '', |
||
| 206 | 'name' => $option_query->row['name'], |
||
| 207 | 'value' => $value, |
||
| 208 | 'type' => $option_query->row['type'], |
||
| 209 | 'quantity' => '', |
||
| 210 | 'subtract' => '', |
||
| 211 | 'price' => '', |
||
| 212 | 'price_prefix' => '', |
||
| 213 | 'points' => '', |
||
| 214 | 'points_prefix' => '' |
||
| 215 | ); |
||
| 216 | } |
||
| 217 | } |
||
| 218 | } |
||
| 219 | |||
| 220 | $price = $product_query->row['price']; |
||
| 221 | |||
| 222 | // Product Discounts |
||
| 223 | $discount_quantity = 0; |
||
| 224 | |||
| 225 | foreach ($cart_query->rows as $cart_2) { |
||
| 226 | if ($cart_2['product_id'] == $cart['product_id']) { |
||
| 227 | $discount_quantity += $cart_2['quantity']; |
||
| 228 | } |
||
| 229 | } |
||
| 230 | |||
| 231 | $product_discount_query = $this->db->query(" |
||
| 232 | SELECT price |
||
| 233 | FROM product_discount |
||
| 234 | WHERE product_id = '" . (int) $cart['product_id'] . "' |
||
| 235 | AND customer_group_id = '" . (int) $this->config->get('config_customer_group_id') . "' |
||
| 236 | AND quantity <= '" . (int) $discount_quantity . "' |
||
| 237 | AND ((date_start = '2000-01-01' OR date_start < NOW()) |
||
| 238 | AND (date_end = '2000-01-01' OR date_end > NOW())) |
||
| 239 | ORDER BY quantity DESC, priority ASC, price ASC |
||
| 240 | LIMIT 1 |
||
| 241 | "); |
||
| 242 | |||
| 243 | if ($product_discount_query->num_rows) { |
||
| 244 | $price = $product_discount_query->row['price']; |
||
| 245 | } |
||
| 246 | |||
| 247 | // Product Specials |
||
| 248 | $product_special_query = $this->db->query(" |
||
| 249 | SELECT price |
||
| 250 | FROM product_special |
||
| 251 | WHERE product_id = '" . (int) $cart['product_id'] . "' |
||
| 252 | AND customer_group_id = '" . (int) $this->config->get('config_customer_group_id') . "' |
||
| 253 | AND ((date_start = '2000-01-01' OR date_start < NOW()) |
||
| 254 | AND (date_end = '2000-01-01' OR date_end > NOW())) |
||
| 255 | ORDER BY priority ASC, price ASC |
||
| 256 | LIMIT 1 |
||
| 257 | "); |
||
| 258 | |||
| 259 | if ($product_special_query->num_rows) { |
||
| 260 | $price = $product_special_query->row['price']; |
||
| 261 | } |
||
| 262 | |||
| 263 | // Reward Points |
||
| 264 | $product_reward_query = $this->db->query(" |
||
| 265 | SELECT points |
||
| 266 | FROM product_reward |
||
| 267 | WHERE product_id = '" . (int) $cart['product_id'] . "' |
||
| 268 | AND customer_group_id = '" . (int) $this->config->get('config_customer_group_id') . "' |
||
| 269 | "); |
||
| 270 | |||
| 271 | if ($product_reward_query->num_rows) { |
||
| 272 | $reward = $product_reward_query->row['points']; |
||
| 273 | } else { |
||
| 274 | $reward = 0; |
||
| 275 | } |
||
| 276 | |||
| 277 | // Downloads |
||
| 278 | $download_data = array(); |
||
| 279 | |||
| 280 | $download_query = $this->db->query(" |
||
| 281 | SELECT * |
||
| 282 | FROM product_to_download p2d |
||
| 283 | LEFT JOIN download d ON (p2d.download_id = d.download_id) |
||
| 284 | LEFT JOIN download_description dd ON (d.download_id = dd.download_id) |
||
| 285 | WHERE p2d.product_id = '" . (int) $cart['product_id'] . "' |
||
| 286 | AND dd.language_id = '" . (int) $this->config->get('config_language_id') . "' |
||
| 287 | "); |
||
| 288 | |||
| 289 | foreach ($download_query->rows as $download) { |
||
| 290 | $download_data[] = array( |
||
| 291 | 'download_id' => $download['download_id'], |
||
| 292 | 'name' => $download['name'], |
||
| 293 | 'filename' => $download['filename'], |
||
| 294 | 'mask' => $download['mask'] |
||
| 295 | ); |
||
| 296 | } |
||
| 297 | |||
| 298 | // Stock |
||
| 299 | if (!$product_query->row['quantity'] || ($product_query->row['quantity'] < $cart['quantity'])) { |
||
| 300 | $stock = false; |
||
| 301 | } |
||
| 302 | |||
| 303 | $product_data[] = array( |
||
| 304 | 'cart_id' => $cart['cart_id'], |
||
| 305 | 'product_id' => $product_query->row['product_id'], |
||
| 306 | 'name' => $product_query->row['name'], |
||
| 307 | 'model' => $product_query->row['model'], |
||
| 308 | 'shipping' => $product_query->row['shipping'], |
||
| 309 | 'image' => $product_query->row['image'], |
||
| 310 | 'option' => $option_data, |
||
| 311 | 'download' => $download_data, |
||
| 312 | 'quantity' => $cart['quantity'], |
||
| 313 | 'minimum' => $product_query->row['minimum'], |
||
| 314 | 'subtract' => $product_query->row['subtract'], |
||
| 315 | 'stock' => $stock, |
||
| 316 | 'price' => ($price + $option_price), |
||
| 317 | 'total' => ($price + $option_price) * $cart['quantity'], |
||
| 318 | 'reward' => $reward * $cart['quantity'], |
||
| 319 | 'points' => ($product_query->row['points'] ? ($product_query->row['points'] + $option_points) * $cart['quantity'] : 0), |
||
| 320 | 'width' => $product_query->row['width'], |
||
| 321 | 'height' => $product_query->row['height'] |
||
| 322 | ); |
||
| 323 | } else { |
||
| 324 | $this->remove($cart['cart_id']); |
||
| 325 | } |
||
| 326 | } |
||
| 327 | |||
| 328 | return $product_data; |
||
| 329 | } |
||
| 467 |