shoppingCart::show_total()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
  * osCommerce Online Merchant
4
  *
5
  * @copyright (c) 2016 osCommerce; https://www.oscommerce.com
6
  * @license MIT; https://www.oscommerce.com/license/mit.txt
7
  */
8
9
  use OSC\OM\Registry;
10
11
  class shoppingCart {
12
    var $db, $contents, $total, $weight;
13
14
    function shoppingCart() {
15
      $this->db = Registry::get('Db');
16
17
      $this->contents = array();
18
      $this->total = 0;
19
    }
20
21
    function add_cart($products_id, $qty = '', $attributes = '') {
22
      $products_id = tep_get_uprid($products_id, $attributes);
23
24
      if ($this->in_cart($products_id)) {
25
        $this->update_quantity($products_id, $qty, $attributes);
26
      } else {
27
        if ($qty == '') $qty = '1'; // if no quantity is supplied, then add '1' to the customers basket
28
29
        $this->contents[$products_id] = array('qty' => $qty);
30
31 View Code Duplication
        if (is_array($attributes)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
32
          foreach( $attributes as $option => $value ) {
33
            $this->contents[$products_id]['attributes'][$option] = $value;
34
          }
35
        }
36
      }
37
      $this->cleanup();
38
    }
39
40
    function update_quantity($products_id, $quantity = '', $attributes = '') {
41
      if ($quantity == '') return true; // nothing needs to be updated if theres no quantity, so we return true..
42
43
      $this->contents[$products_id] = array('qty' => $quantity);
44
45 View Code Duplication
      if (is_array($attributes)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
46
        foreach( $attributes as $option => $value ) {
47
          $this->contents[$products_id]['attributes'][$option] = $value;
48
        }
49
      }
50
    }
51
52
    function cleanup() {
53
      foreach( array_keys($this->contents) as $key ) {
54
        if ($this->contents[$key]['qty'] < 1) {
55
          unset($this->contents[$key]);
56
        }
57
      }
58
    }
59
60
    function in_cart($products_id) {
61
      if (isset($this->contents[$products_id])) {
62
        return true;
63
      } else {
64
        return false;
65
      }
66
    }
67
68
    function calculate() {
69
      $this->total = 0;
70
      $this->weight = 0;
71
      if (!is_array($this->contents)) return 0;
72
73
      foreach ( array_keys($this->contents) as $products_id ) {
74
        $qty = $this->contents[$products_id]['qty'];
75
76
// products price
77
        $Qproduct = $this->db->get('products', [
78
          'products_id',
79
          'products_price',
80
          'products_tax_class_id',
81
          'products_weight'
82
        ], [
83
          'products_id' => (int)tep_get_prid($products_id)
84
        ]);
85
86
        if ($Qproduct->fetch() !== false) {
87
          $prid = $Qproduct->valueInt('products_id');
88
          $products_tax = tep_get_tax_rate($Qproduct->valueInt('products_tax_class_id'));
89
          $products_price = $Qproduct->value('products_price');
90
          $products_weight = $Qproduct->value('products_weight');
91
92
          $Qspecials = $this->db->get('specials', 'specials_new_products_price', ['products_id' => $prid, 'status' => '1']);
93
94
          if ($Qspecials->fetch() !== false) {
95
            $products_price = $Qspecials->value('specials_new_products_price');
96
          }
97
98
          $this->total += tep_add_tax($products_price, $products_tax) * $qty;
99
          $this->weight += ($qty * $products_weight);
100
101
// attributes price
102
          if (isset($this->contents[$products_id]['attributes'])) {
103
            foreach ( $this->contents[$products_id]['attributes'] as $option => $value ) {
104
              $Qattribute = $this->db->get('products_attributes', [
105
                'options_values_price',
106
                'price_prefix'
107
              ], [
108
                'products_id' => $prid,
109
                'options_id' => (int)$option,
110
                'options_values_id' => (int)$value
111
              ]);
112
113
              if ($Qattribute->value('price_prefix') == '+') {
114
                $this->total += $qty * tep_add_tax($Qattribute->value('options_values_price'), $products_tax);
115
              } else {
116
                $this->total -= $qty * tep_add_tax($Qattribute->value('options_values_price'), $products_tax);
117
              }
118
            }
119
          }
120
        }
121
      }
122
    }
123
124
    function show_total() {
125
      $this->calculate();
126
127
      return $this->total;
128
    }
129
  }
130
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
131