[ Index ]

PHP Cross Reference of osCMax 2.0.4

title

Body

[close]

/admin/includes/classes/ -> shopping_cart.php (source)

   1  <?php
   2  /*

   3  $Id: shopping_cart.php 3 2006-05-27 04:59:07Z user $

   4  

   5    osCMax Power E-Commerce

   6    http://oscdox.com

   7  

   8    Copyright 2006 osCMax

   9  

  10    Released under the GNU General Public License

  11  */
  12  
  13    class shoppingCart {
  14      var $contents, $total, $weight;
  15  
  16      function shoppingCart() {
  17        $this->reset();
  18      }
  19  
  20      function restore_contents() {
  21        global $customer_id;
  22  
  23        if (!$customer_id) return 0;
  24  
  25  // insert current cart contents in database

  26        if ($this->contents) {
  27          reset($this->contents);
  28          while (list($products_id, ) = each($this->contents)) {
  29            $qty = $this->contents[$products_id]['qty'];
  30            $product_query = tep_db_query("select products_id from " . TABLE_CUSTOMERS_BASKET . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($products_id) . "'");
  31            if (!tep_db_num_rows($product_query)) {
  32              tep_db_query("insert into " . TABLE_CUSTOMERS_BASKET . " (customers_id, products_id, customers_basket_quantity, customers_basket_date_added) values ('" . (int)$customer_id . "', '" . tep_db_input($products_id) . "', '" . tep_db_input($qty) . "', '" . date('Ymd') . "')");
  33              if ($this->contents[$products_id]['attributes']) {
  34                reset($this->contents[$products_id]['attributes']);
  35                while (list($option, $value) = each($this->contents[$products_id]['attributes'])) {
  36                  tep_db_query("insert into " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " (customers_id, products_id, products_options_id, products_options_value_id) values ('" . (int)$customer_id . "', '" . tep_db_input($products_id) . "', '" . (int)$option . "', '" . (int)$value . "')");
  37                }
  38              }
  39            } else {
  40              tep_db_query("update " . TABLE_CUSTOMERS_BASKET . " set customers_basket_quantity = '" . tep_db_input($qty) . "' where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($products_id) . "'");
  41            }
  42          }
  43        }
  44  
  45  // reset per-session cart contents, but not the database contents

  46        $this->reset(FALSE);
  47  
  48        $products_query = tep_db_query("select products_id, customers_basket_quantity from " . TABLE_CUSTOMERS_BASKET . " where customers_id = '" . (int)$customer_id . "'");
  49        while ($products = tep_db_fetch_array($products_query)) {
  50          $this->contents[$products['products_id']] = array('qty' => $products['customers_basket_quantity']);
  51  // attributes

  52          $attributes_query = tep_db_query("select products_options_id, products_options_value_id from " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($products['products_id']) . "'");
  53          while ($attributes = tep_db_fetch_array($attributes_query)) {
  54            $this->contents[$products['products_id']]['attributes'][$attributes['products_options_id']] = $attributes['products_options_value_id'];
  55          }
  56        }
  57  
  58        $this->cleanup();
  59      }
  60  
  61      function reset($reset_database = FALSE) {
  62        global $customer_id;
  63  
  64        $this->contents = array();
  65        $this->total = 0;
  66  
  67        if ($customer_id && $reset_database) {
  68          tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET . " where customers_id = '" . (int)$customer_id . "'");
  69          tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " where customers_id = '" . (int)$customer_id . "'");
  70        }
  71      }
  72  
  73      function add_cart($products_id, $qty = '', $attributes = '') {
  74        global $new_products_id_in_cart, $customer_id;
  75  
  76        $products_id = tep_get_uprid($products_id, $attributes);
  77  
  78        if ($this->in_cart($products_id)) {
  79          $this->update_quantity($products_id, $qty, $attributes);
  80        } else {
  81          if ($qty == '') $qty = '1'; // if no quantity is supplied, then add '1' to the customers basket

  82  
  83          $this->contents[] = array($products_id);
  84          $this->contents[$products_id] = array('qty' => $qty);
  85  // insert into database

  86          if ($customer_id) tep_db_query("insert into " . TABLE_CUSTOMERS_BASKET . " (customers_id, products_id, customers_basket_quantity, customers_basket_date_added) values ('" . (int)$customer_id . "', '" . tep_db_input($products_id) . "', '" . tep_db_input($qty) . "', '" . date('Ymd') . "')");
  87  
  88          if (is_array($attributes)) {
  89            reset($attributes);
  90            while (list($option, $value) = each($attributes)) {
  91              $this->contents[$products_id]['attributes'][$option] = $value;
  92  // insert into database

  93              if ($customer_id) tep_db_query("insert into " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " (customers_id, products_id, products_options_id, products_options_value_id) values ('" . (int)$customer_id . "', '" . tep_db_input($products_id) . "', '" . (int)$option . "', '" . (int)$value . "')");
  94            }
  95          }
  96          $new_products_id_in_cart = $products_id;
  97          tep_session_register('new_products_id_in_cart');
  98        }
  99        $this->cleanup();
 100      }
 101  
 102      function update_quantity($products_id, $quantity = '', $attributes = '') {
 103        global $customer_id;
 104  
 105        if ($quantity == '') return true; // nothing needs to be updated if theres no quantity, so we return true..

 106  
 107        $this->contents[$products_id] = array('qty' => $quantity);
 108  // update database

 109        if ($customer_id) tep_db_query("update " . TABLE_CUSTOMERS_BASKET . " set customers_basket_quantity = '" . tep_db_input($quantity) . "' where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($products_id) . "'");
 110  
 111        if (is_array($attributes)) {
 112          reset($attributes);
 113          while (list($option, $value) = each($attributes)) {
 114            $this->contents[$products_id]['attributes'][$option] = $value;
 115  // update database

 116            if ($customer_id) tep_db_query("update " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " set products_options_value_id = '" . (int)$value . "' where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($products_id) . "' and products_options_id = '" . (int)$option . "'");
 117          }
 118        }
 119      }
 120  
 121      function cleanup() {
 122        global $customer_id;
 123  
 124        reset($this->contents);
 125        while (list($key,) = each($this->contents)) {
 126          if ($this->contents[$key]['qty'] < 1) {
 127            unset($this->contents[$key]);
 128  // remove from database

 129            if ($customer_id) {
 130              tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($key) . "'");
 131              tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($key) . "'");
 132            }
 133          }
 134        }
 135      }
 136  
 137      function count_contents() {  // get total number of items in cart 
 138          $total_items = 0;
 139          if (is_array($this->contents)) {
 140              reset($this->contents);
 141              while (list($products_id, ) = each($this->contents)) {
 142                  $total_items += $this->get_quantity($products_id);
 143              }
 144          }
 145          return $total_items;
 146      }
 147  
 148      function get_quantity($products_id) {
 149        if ($this->contents[$products_id]) {
 150          return $this->contents[$products_id]['qty'];
 151        } else {
 152          return 0;
 153        }
 154      }
 155  
 156      function in_cart($products_id) {
 157        if ($this->contents[$products_id]) {
 158          return true;
 159        } else {
 160          return false;
 161        }
 162      }
 163  
 164      function remove($products_id) {
 165        global $customer_id;
 166  
 167        unset($this->contents[$products_id]);
 168  // remove from database

 169        if ($customer_id) {
 170          tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($products_id) . "'");
 171          tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($products_id) . "'");
 172        }
 173      }
 174  
 175      function remove_all() {
 176        $this->reset();
 177      }
 178  
 179      function get_product_id_list() {
 180        $product_id_list = '';
 181        if (is_array($this->contents))
 182        {
 183          reset($this->contents);
 184          while (list($products_id, ) = each($this->contents)) {
 185            $product_id_list .= ', ' . $products_id;
 186          }
 187        }
 188        return substr($product_id_list, 2);
 189      }
 190  
 191      function calculate() {
 192        $this->total = 0;
 193        $this->weight = 0;
 194        if (!is_array($this->contents)) return 0;
 195  
 196        reset($this->contents);
 197        while (list($products_id, ) = each($this->contents)) {
 198          $qty = $this->contents[$products_id]['qty'];
 199  
 200  // products price

 201          $product_query = tep_db_query("select products_id, products_price, products_tax_class_id, products_weight from " . TABLE_PRODUCTS . " where products_id='" . (int)tep_get_prid($products_id) . "'");
 202          if ($product = tep_db_fetch_array($product_query)) {
 203            $prid = $product['products_id'];
 204            $products_tax = tep_get_tax_rate($product['products_tax_class_id']);
 205            $products_price = $product['products_price'];
 206            $products_weight = $product['products_weight'];
 207  
 208            $specials_query = tep_db_query("select specials_new_products_price from " . TABLE_SPECIALS . " where products_id = '" . (int)$prid . "' and status = '1'");
 209            if (tep_db_num_rows ($specials_query)) {
 210              $specials = tep_db_fetch_array($specials_query);
 211              $products_price = $specials['specials_new_products_price'];
 212            }
 213  
 214            $this->total += tep_add_tax($products_price, $products_tax) * $qty;
 215            $this->weight += ($qty * $products_weight);
 216          }
 217  
 218  // attributes price

 219          if (isset($this->contents[$products_id]['attributes'])) {
 220            reset($this->contents[$products_id]['attributes']);
 221            while (list($option, $value) = each($this->contents[$products_id]['attributes'])) {
 222              $attribute_price_query = tep_db_query("select options_values_price, price_prefix from " . TABLE_PRODUCTS_ATTRIBUTES . " where products_id = '" . (int)$prid . "' and options_id = '" . (int)$option . "' and options_values_id = '" . (int)$value . "'");
 223              $attribute_price = tep_db_fetch_array($attribute_price_query);
 224              if ($attribute_price['price_prefix'] == '+') {
 225                $this->total += $qty * tep_add_tax($attribute_price['options_values_price'], $products_tax);
 226              } else {
 227                $this->total -= $qty * tep_add_tax($attribute_price['options_values_price'], $products_tax);
 228              }
 229            }
 230          }
 231        }
 232      }
 233  
 234      function attributes_price($products_id) {
 235        $attributes_price = 0;
 236  
 237        if (isset($this->contents[$products_id]['attributes'])) {
 238          reset($this->contents[$products_id]['attributes']);
 239          while (list($option, $value) = each($this->contents[$products_id]['attributes'])) {
 240            $attribute_price_query = tep_db_query("select options_values_price, price_prefix from " . TABLE_PRODUCTS_ATTRIBUTES . " where products_id = '" . (int)$products_id . "' and options_id = '" . (int)$option . "' and options_values_id = '" . (int)$value . "'");
 241            $attribute_price = tep_db_fetch_array($attribute_price_query);
 242            if ($attribute_price['price_prefix'] == '+') {
 243              $attributes_price += $attribute_price['options_values_price'];
 244            } else {
 245              $attributes_price -= $attribute_price['options_values_price'];
 246            }
 247          }
 248        }
 249  
 250        return $attributes_price;
 251      }
 252  
 253      function get_products() {
 254        global $languages_id;
 255  
 256        if (!is_array($this->contents)) return 0;
 257        $products_array = array();
 258        reset($this->contents);
 259        while (list($products_id, ) = each($this->contents)) {
 260          $products_query = tep_db_query("select p.products_id, pd.products_name, p.products_model, p.products_price, p.products_weight, p.products_tax_class_id from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd where p.products_id='" . (int)tep_get_prid($products_id) . "' and pd.products_id = p.products_id and pd.language_id = '" . (int)$languages_id . "'");
 261          if ($products = tep_db_fetch_array($products_query)) {
 262            $prid = $products['products_id'];
 263            $products_price = $products['products_price'];
 264  
 265            $specials_query = tep_db_query("select specials_new_products_price from " . TABLE_SPECIALS . " where products_id = '" . (int)$prid . "' and status = '1'");
 266            if (tep_db_num_rows($specials_query)) {
 267              $specials = tep_db_fetch_array($specials_query);
 268              $products_price = $specials['specials_new_products_price'];
 269            }
 270  
 271            $products_array[] = array('id' => $products_id,
 272                                      'name' => $products['products_name'],
 273                                      'model' => $products['products_model'],
 274                                      'price' => $products_price,
 275                                      'quantity' => $this->contents[$products_id]['qty'],
 276                                      'weight' => $products['products_weight'],
 277                                      'final_price' => ($products_price + $this->attributes_price($products_id)),
 278                                      'tax_class_id' => $products['products_tax_class_id'],
 279                                      'attributes' => (isset($this->contents[$products_id]['attributes']) ? $this->contents[$products_id]['attributes'] : ''));
 280          }
 281        }
 282        return $products_array;
 283      }
 284  
 285      function show_total() {
 286        $this->calculate();
 287  
 288        return $this->total;
 289      }
 290  
 291      function show_weight() {
 292        $this->calculate();
 293  
 294        return $this->weight;
 295      }
 296  
 297      function unserialize($broken) {
 298        for(reset($broken);$kv=each($broken);) {
 299          $key=$kv['key'];
 300          if (gettype($this->$key)!="user function")
 301          $this->$key=$kv['value'];
 302        }
 303      }
 304  
 305    }
 306  ?>


Generated: Fri Jan 1 13:43:16 2010 Cross-referenced by PHPXref 0.7