  |
1 | 1 | | <?php |
| |
2 | 2 | | /* |
  |
3 | | - | $Id: shopping_cart.php 1674 2007-08-20 22:56:00Z hpdl $ |
| |
| 3 | + | $Id: shopping_cart.php 1677 2007-08-29 00:33:19Z hpdl $ |
|
4 | 4 | | |
| |
5 | 5 | | osCommerce, Open Source E-Commerce Solutions |
| |
6 | 6 | | http://www.oscommerce.com |
| |
|
|
 |
… |
|
32 | 32 | | 'tax_groups' => array(), |
| |
33 | 33 | | 'shipping_boxes_weight' => 0, |
| |
34 | 34 | | 'shipping_boxes' => 1, |
  |
35 | | - | 'shipping_address' => array('zone_id' => STORE_ZONE, 'country_id' => STORE_COUNTRY), |
| |
| 35 | + | 'shipping_address' => array('zone_id' => STORE_ZONE, |
| |
| 36 | + | 'country_id' => STORE_COUNTRY), |
|
36 | 37 | | 'shipping_method' => array(), |
  |
37 | | - | 'billing_address' => array('zone_id' => STORE_ZONE, 'country_id' => STORE_COUNTRY), |
| |
| 38 | + | 'billing_address' => array('zone_id' => STORE_ZONE, |
| |
| 39 | + | 'country_id' => STORE_COUNTRY), |
|
38 | 40 | | 'billing_method' => array(), |
| |
39 | 41 | | 'shipping_quotes' => array(), |
| |
40 | 42 | | 'order_totals' => array()); |
| |
|
|
 |
… |
|
59 | 61 | | $this->_order_totals =& $_SESSION['osC_ShoppingCart_data']['order_totals']; |
| |
60 | 62 | | } |
| |
61 | 63 | | |
  |
62 | | - | public function update() { |
| |
| 64 | + | public function refresh() { |
|
63 | 65 | | if ( !isset($_SESSION['cartID']) ) { |
| |
64 | 66 | | $this->_calculate(); |
| |
65 | 67 | | } |
| |
|
|
 |
… |
|
76 | 78 | | return false; |
| |
77 | 79 | | } |
| |
78 | 80 | | |
  |
79 | | - | // insert current cart contents in database |
| |
80 | | - | if ( $this->hasContents() ) { |
| |
81 | | - | foreach ( $this->_contents as $product_id => $data ) { |
| |
82 | | - | $Qproduct = $osC_Database->query('select products_id, customers_basket_quantity from :table_customers_basket where customers_id = :customers_id and products_id = :products_id'); |
| |
83 | | - | $Qproduct->bindTable(':table_customers_basket', TABLE_CUSTOMERS_BASKET); |
| |
| 81 | + | foreach ( $this->_contents as $item_id => $data ) { |
| |
| 82 | + | $db_action = 'check'; |
| |
| 83 | + | |
| |
| 84 | + | if ( isset($data['variants']) ) { |
| |
| 85 | + | foreach ( $data['variants'] as $variant ) { |
| |
| 86 | + | if ( $variant['has_custom_value'] === true ) { |
| |
| 87 | + | $db_action = 'insert'; |
| |
| 88 | + | |
| |
| 89 | + | break; |
| |
| 90 | + | } |
| |
| 91 | + | } |
| |
| 92 | + | } |
| |
| 93 | + | |
| |
| 94 | + | if ( $db_action == 'check' ) { |
| |
| 95 | + | $Qproduct = $osC_Database->query('select item_id, quantity from :table_shopping_carts where customers_id = :customers_id and products_id = :products_id'); |
| |
| 96 | + | $Qproduct->bindTable(':table_shopping_carts', TABLE_SHOPPING_CARTS); |
|
84 | 97 | | $Qproduct->bindInt(':customers_id', $osC_Customer->getID()); |
  |
85 | | - | $Qproduct->bindInt(':products_id', $product_id); |
| |
| 98 | + | $Qproduct->bindInt(':products_id', $data['id']); |
|
86 | 99 | | $Qproduct->execute(); |
| |
87 | 100 | | |
| |
88 | 101 | | if ( $Qproduct->numberOfRows() > 0 ) { |
  |
89 | | - | $Qupdate = $osC_Database->query('update :table_customers_basket set customers_basket_quantity = :customers_basket_quantity where customers_id = :customers_id and products_id = :products_id'); |
| |
90 | | - | $Qupdate->bindTable(':table_customers_basket', TABLE_CUSTOMERS_BASKET); |
| |
91 | | - | $Qupdate->bindInt(':customers_basket_quantity', $data['quantity'] + $Qproduct->valueInt('customers_basket_quantity')); |
| |
| 102 | + | $Qupdate = $osC_Database->query('update :table_shopping_carts set quantity = :quantity where customers_id = :customers_id and item_id = :item_id'); |
| |
| 103 | + | $Qupdate->bindTable(':table_shopping_carts', TABLE_SHOPPING_CARTS); |
| |
| 104 | + | $Qupdate->bindInt(':quantity', $data['quantity'] + $Qproduct->valueInt('quantity')); |
|
92 | 105 | | $Qupdate->bindInt(':customers_id', $osC_Customer->getID()); |
  |
93 | | - | $Qupdate->bindInt(':products_id', $product_id); |
| |
| 106 | + | $Qupdate->bindInt(':item_id', $Qproduct->valueInt('item_id')); |
|
94 | 107 | | $Qupdate->execute(); |
| |
95 | 108 | | } else { |
  |
96 | | - | $Qnew = $osC_Database->query('insert into :table_customers_basket (customers_id, products_id, customers_basket_quantity, customers_basket_date_added) values (:customers_id, :products_id, :customers_basket_quantity, now())'); |
| |
97 | | - | $Qnew->bindTable(':table_customers_basket', TABLE_CUSTOMERS_BASKET); |
| |
98 | | - | $Qnew->bindInt(':customers_id', $osC_Customer->getID()); |
| |
99 | | - | $Qnew->bindValue(':products_id', $product_id); |
| |
100 | | - | $Qnew->bindInt(':customers_basket_quantity', $data['quantity']); |
| |
101 | | - | $Qnew->execute(); |
| |
| 109 | + | $db_action = 'insert'; |
|
102 | 110 | | } |
| |
103 | 111 | | } |
  |
| 112 | + | |
| |
| 113 | + | if ( $db_action == 'insert') { |
| |
| 114 | + | $Qid = $osC_Database->query('select max(item_id) as item_id from :table_shopping_carts where customers_id = :customers_id'); |
| |
| 115 | + | $Qid->bindTable(':table_shopping_carts', TABLE_SHOPPING_CARTS); |
| |
| 116 | + | $Qid->bindInt(':customers_id', $osC_Customer->getID()); |
| |
| 117 | + | $Qid->execute(); |
| |
| 118 | + | |
| |
| 119 | + | $db_item_id = $Qid->valueInt('item_id') + 1; |
| |
| 120 | + | |
| |
| 121 | + | $Qnew = $osC_Database->query('insert into :table_shopping_carts (customers_id, item_id, products_id, quantity, date_added) values (:customers_id, :item_id, :products_id, :quantity, :date_added)'); |
| |
| 122 | + | $Qnew->bindTable(':table_shopping_carts', TABLE_SHOPPING_CARTS); |
| |
| 123 | + | $Qnew->bindInt(':customers_id', $osC_Customer->getID()); |
| |
| 124 | + | $Qnew->bindInt(':item_id', $db_item_id); |
| |
| 125 | + | $Qnew->bindInt(':products_id', $data['id']); |
| |
| 126 | + | $Qnew->bindInt(':quantity', $data['quantity']); |
| |
| 127 | + | $Qnew->bindRaw(':date_added', 'now()'); |
| |
| 128 | + | $Qnew->execute(); |
| |
| 129 | + | |
| |
| 130 | + | if ( isset($data['variants']) ) { |
| |
| 131 | + | foreach ( $data['variants'] as $variant ) { |
| |
| 132 | + | if ( $variant['has_custom_value'] === true ) { |
| |
| 133 | + | $Qnew = $osC_Database->query('insert into :table_shopping_carts_custom_variants_values (shopping_carts_item_id, customers_id, products_id, products_variants_values_id, products_variants_values_text) values (:shopping_carts_item_id, :customers_id, :products_id, :products_variants_values_id, :products_variants_values_text)'); |
| |
| 134 | + | $Qnew->bindTable(':table_shopping_carts_custom_variants_values', TABLE_SHOPPING_CARTS_CUSTOM_VARIANTS_VALUES); |
| |
| 135 | + | $Qnew->bindInt(':shopping_carts_item_id', $db_item_id); |
| |
| 136 | + | $Qnew->bindInt(':customers_id', $osC_Customer->getID()); |
| |
| 137 | + | $Qnew->bindInt(':products_id', $data['id']); |
| |
| 138 | + | $Qnew->bindInt(':products_variants_values_id', $variant['value_id']); |
| |
| 139 | + | $Qnew->bindValue(':products_variants_values_text', $variant['value_title']); |
| |
| 140 | + | $Qnew->execute(); |
| |
| 141 | + | } |
| |
| 142 | + | } |
| |
| 143 | + | } |
| |
| 144 | + | } |
|
104 | 145 | | } |
| |
105 | 146 | | |
| |
106 | 147 | | // reset per-session cart contents, but not the database contents |
| |
107 | 148 | | $this->reset(); |
| |
108 | 149 | | |
| |
109 | 150 | | $_delete_array = array(); |
| |
110 | 151 | | |
  |
111 | | - | $Qproducts = $osC_Database->query('select cb.products_id, cb.customers_basket_quantity, cb.customers_basket_date_added, p.parent_id, p.products_price, p.products_tax_class_id, p.products_weight, p.products_weight_class, p.products_status from :table_customers_basket cb, :table_products p where cb.customers_id = :customers_id and cb.products_id = p.products_id order by cb.customers_basket_date_added desc'); |
| |
112 | | - | $Qproducts->bindTable(':table_customers_basket', TABLE_CUSTOMERS_BASKET); |
| |
| 152 | + | $Qproducts = $osC_Database->query('select sc.item_id, sc.products_id, sc.quantity, sc.date_added, p.parent_id, p.products_price, p.products_tax_class_id, p.products_weight, p.products_weight_class, p.products_status from :table_shopping_carts sc, :table_products p where sc.customers_id = :customers_id and sc.products_id = p.products_id order by sc.date_added desc'); |
| |
| 153 | + | $Qproducts->bindTable(':table_shopping_carts', TABLE_SHOPPING_CARTS); |
|
113 | 154 | | $Qproducts->bindTable(':table_products', TABLE_PRODUCTS); |
| |
114 | 155 | | $Qproducts->bindInt(':customers_id', $osC_Customer->getID()); |
| |
115 | 156 | | $Qproducts->execute(); |
| |
|
|
 |
… |
|
136 | 177 | | } |
| |
137 | 178 | | } |
| |
138 | 179 | | |
  |
139 | | - | $this->_contents[$Qproducts->valueInt('products_id')] = array('id' => $Qproducts->valueInt('products_id'), |
| |
140 | | - | 'parent_id' => $Qproducts->valueInt('parent_id'), |
| |
141 | | - | 'name' => $Qdesc->value('products_name'), |
| |
142 | | - | 'keyword' => $Qdesc->value('products_keyword'), |
| |
143 | | - | 'image' => ($Qimage->numberOfRows() === 1) ? $Qimage->value('image') : '', |
| |
144 | | - | 'price' => $price, |
| |
145 | | - | 'quantity' => $Qproducts->valueInt('customers_basket_quantity'), |
| |
146 | | - | 'weight' => $Qproducts->value('products_weight'), |
| |
147 | | - | 'tax_class_id' => $Qproducts->valueInt('products_tax_class_id'), |
| |
148 | | - | 'date_added' => osC_DateTime::getShort($Qproducts->value('customers_basket_date_added')), |
| |
149 | | - | 'weight_class_id' => $Qproducts->valueInt('products_weight_class')); |
| |
| 180 | + | $this->_contents[$Qproducts->valueInt('item_id')] = array('item_id' => $Qproducts->valueInt('item_id'), |
| |
| 181 | + | 'id' => $Qproducts->valueInt('products_id'), |
| |
| 182 | + | 'parent_id' => $Qproducts->valueInt('parent_id'), |
| |
| 183 | + | 'name' => $Qdesc->value('products_name'), |
| |
| 184 | + | 'keyword' => $Qdesc->value('products_keyword'), |
| |
| 185 | + | 'image' => ($Qimage->numberOfRows() === 1) ? $Qimage->value('image') : '', |
| |
| 186 | + | 'price' => $price, |
| |
| 187 | + | 'quantity' => $Qproducts->valueInt('quantity'), |
| |
| 188 | + | 'weight' => $Qproducts->value('products_weight'), |
| |
| 189 | + | 'tax_class_id' => $Qproducts->valueInt('products_tax_class_id'), |
| |
| 190 | + | 'date_added' => osC_DateTime::getShort($Qproducts->value('date_added')), |
| |
| 191 | + | 'weight_class_id' => $Qproducts->valueInt('products_weight_class')); |
|
150 | 192 | | |
| |
151 | 193 | | if ( $Qproducts->valueInt('parent_id') > 0 ) { |
| |
152 | 194 | | $Qcheck = $osC_Database->query('select products_status from :table_products where products_id = :products_id'); |
| |
|
|
 |
… |
|
155 | 197 | | $Qcheck->execute(); |
| |
156 | 198 | | |
| |
157 | 199 | | if ( $Qcheck->valueInt('products_status') === 1 ) { |
  |
158 | | - | $Qvariant = $osC_Database->query('select pvg.id as group_id, pvg.title as group_title, pvv.id as value_id, pvv.title as value_title from :table_products_variants pv, :table_products_variants_values pvv, :table_products_variants_groups pvg where pv.products_id = :products_id and pv.variants_values_id = pvv.id and pvv.languages_id = :languages_id and pvv.products_variants_groups_id = pvg.id and pvg.languages_id = :languages_id'); |
| |
| 200 | + | $Qvariant = $osC_Database->query('select pvg.id as group_id, pvg.title as group_title, pvg.module, pvv.id as value_id, pvv.title as value_title from :table_products_variants pv, :table_products_variants_values pvv, :table_products_variants_groups pvg where pv.products_id = :products_id and pv.variants_values_id = pvv.id and pvv.languages_id = :languages_id and pvv.products_variants_groups_id = pvg.id and pvg.languages_id = :languages_id'); |
|
159 | 201 | | $Qvariant->bindTable(':table_products_variants', TABLE_PRODUCTS_VARIANTS); |
| |
160 | 202 | | $Qvariant->bindTable(':table_products_variants_values', TABLE_PRODUCTS_VARIANTS_VALUES); |
| |
161 | 203 | | $Qvariant->bindTable(':table_products_variants_groups', TABLE_PRODUCTS_VARIANTS_GROUPS); |
| |
|
|
 |
… |
|
164 | 206 | | $Qvariant->bindInt(':languages_id', $osC_Language->getID()); |
| |
165 | 207 | | $Qvariant->execute(); |
| |
166 | 208 | | |
  |
167 | | - | if ( $Qproducts->numberOfRows() > 0 ) { |
| |
| 209 | + | if ( $Qvariant->numberOfRows() > 0 ) { |
|
168 | 210 | | while ( $Qvariant->next() ) { |
  |
169 | | - | $this->_contents[$Qproducts->valueInt('products_id')]['variants'][$Qvariant->valueInt('group_id')] = array('group_id' => $Qvariant->valueInt('group_id'), |
| |
170 | | - | 'value_id' => $Qvariant->valueInt('value_id'), |
| |
171 | | - | 'group_title' => $Qvariant->value('group_title'), |
| |
172 | | - | 'value_title' => $Qvariant->value('value_title')); |
| |
| 211 | + | $group_title = osC_Variants::getGroupTitle($Qvariant->value('module'), $Qvariant->toArray()); |
| |
| 212 | + | $value_title = $Qvariant->value('value_title'); |
| |
| 213 | + | $has_custom_value = false; |
| |
| 214 | + | |
| |
| 215 | + | $Qcvv = $osC_Database->query('select products_variants_values_text from :table_shopping_carts_custom_variants_values where customers_id = :customers_id and shopping_carts_item_id = :shopping_carts_item_id and products_id = :products_id and products_variants_values_id = :products_variants_values_id'); |
| |
| 216 | + | $Qcvv->bindTable(':table_shopping_carts_custom_variants_values', TABLE_SHOPPING_CARTS_CUSTOM_VARIANTS_VALUES); |
| |
| 217 | + | $Qcvv->bindInt(':customers_id', $osC_Customer->getID()); |
| |
| 218 | + | $Qcvv->bindInt(':shopping_carts_item_id', $Qproducts->valueInt('item_id')); |
| |
| 219 | + | $Qcvv->bindInt(':products_id', $Qproducts->valueInt('products_id')); |
| |
| 220 | + | $Qcvv->bindInt(':products_variants_values_id', $Qvariant->valueInt('value_id')); |
| |
| 221 | + | $Qcvv->execute(); |
| |
| 222 | + | |
| |
| 223 | + | if ( $Qcvv->numberOfRows() === 1 ) { |
| |
| 224 | + | $value_title = $Qcvv->value('products_variants_values_text'); |
| |
| 225 | + | $has_custom_value = true; |
| |
| 226 | + | } |
| |
| 227 | + | |
| |
| 228 | + | $this->_contents[$Qproducts->valueInt('item_id')]['variants'][] = array('group_id' => $Qvariant->valueInt('group_id'), |
| |
| 229 | + | 'value_id' => $Qvariant->valueInt('value_id'), |
| |
| 230 | + | 'group_title' => $group_title, |
| |
| 231 | + | 'value_title' => $value_title, |
| |
| 232 | + | 'has_custom_value' => $has_custom_value); |
|
173 | 233 | | } |
| |
174 | 234 | | } else { |
  |
175 | | - | $_delete_array[] = $Qproducts->valueInt('products_id'); |
| |
| 235 | + | $_delete_array[] = $Qproducts->valueInt('item_id'); |
|
176 | 236 | | } |
| |
177 | 237 | | } else { |
  |
178 | | - | $_delete_array[] = $Qproducts->valueInt('products_id'); |
| |
| 238 | + | $_delete_array[] = $Qproducts->valueInt('item_id'); |
|
179 | 239 | | } |
| |
180 | 240 | | } |
| |
181 | 241 | | } else { |
  |
182 | | - | $_delete_array[] = $Qproducts->valueInt('products_id'); |
| |
| 242 | + | $_delete_array[] = $Qproducts->valueInt('item_id'); |
|
183 | 243 | | } |
| |
184 | 244 | | } |
| |
185 | 245 | | |
| |
186 | 246 | | if ( !empty($_delete_array) ) { |
  |
187 | | - | foreach ( $_delete_array as $product_id ) { |
| |
188 | | - | unset($this->_contents[$product_id]); |
| |
| 247 | + | foreach ( $_delete_array as $id ) { |
| |
| 248 | + | unset($this->_contents[$id]); |
|
189 | 249 | | } |
| |
190 | 250 | | |
  |
191 | | - | $Qdelete = $osC_Database->query('delete from :table_customers_basket where customers_id = :customers_id and products_id in (":products_id")'); |
| |
192 | | - | $Qdelete->bindTable(':table_customers_basket', TABLE_CUSTOMERS_BASKET); |
| |
| 251 | + | $Qdelete = $osC_Database->query('delete from :table_shopping_carts where customers_id = :customers_id and item_id in (":item_id")'); |
| |
| 252 | + | $Qdelete->bindTable(':table_shopping_carts', TABLE_SHOPPING_CARTS); |
|
193 | 253 | | $Qdelete->bindInt(':customers_id', $osC_Customer->getID()); |
  |
194 | | - | $Qdelete->bindRaw(':products_id', implode('", "', $_delete_array)); |
| |
| 254 | + | $Qdelete->bindRaw(':item_id', implode('", "', $_delete_array)); |
|
195 | 255 | | $Qdelete->execute(); |
  |
| 256 | + | |
| |
| 257 | + | $Qdelete = $osC_Database->query('delete from :table_shopping_carts_custom_variants_values where customers_id = :customers_id and shopping_carts_item_id in (":shopping_carts_item_id")'); |
| |
| 258 | + | $Qdelete->bindTable(':table_shopping_carts_custom_variants_values', TABLE_SHOPPING_CARTS_CUSTOM_VARIANTS_VALUES); |
| |
| 259 | + | $Qdelete->bindInt(':customers_id', $osC_Customer->getID()); |
| |
| 260 | + | $Qdelete->bindRaw(':shopping_carts_item_id', implode('", "', $_delete_array)); |
| |
| 261 | + | $Qdelete->execute(); |
|
196 | 262 | | } |
| |
197 | 263 | | |
| |
198 | 264 | | $this->_cleanUp(); |
| |
|
|
 |
… |
|
203 | 269 | | global $osC_Database, $osC_Customer; |
| |
204 | 270 | | |
| |
205 | 271 | | if ( ($reset_database === true) && $osC_Customer->isLoggedOn() ) { |
  |
206 | | - | $Qdelete = $osC_Database->query('delete from :table_customers_basket where customers_id = :customers_id'); |
| |
207 | | - | $Qdelete->bindTable(':table_customers_basket', TABLE_CUSTOMERS_BASKET); |
| |
| 272 | + | $Qdelete = $osC_Database->query('delete from :table_shopping_carts where customers_id = :customers_id'); |
| |
| 273 | + | $Qdelete->bindTable(':table_shopping_carts', TABLE_SHOPPING_CARTS); |
|
208 | 274 | | $Qdelete->bindInt(':customers_id', $osC_Customer->getID()); |
| |
209 | 275 | | $Qdelete->execute(); |
  |
| 276 | + | |
| |
| 277 | + | $Qdelete = $osC_Database->query('delete from :table_shopping_carts_custom_variants_values where customers_id = :customers_id'); |
| |
| 278 | + | $Qdelete->bindTable(':table_shopping_carts_custom_variants_values', TABLE_SHOPPING_CARTS_CUSTOM_VARIANTS_VALUES); |
| |
| 279 | + | $Qdelete->bindInt(':customers_id', $osC_Customer->getID()); |
| |
| 280 | + | $Qdelete->execute(); |
|
210 | 281 | | } |
| |
211 | 282 | | |
| |
212 | 283 | | $this->_contents = array(); |
| |
|
|
 |
… |
|
240 | 311 | | |
| |
241 | 312 | | if ( $Qproduct->valueInt('products_status') === 1 ) { |
| |
242 | 313 | | if ( $this->exists($product_id) ) { |
  |
| 314 | + | $item_id = $this->getBasketID($product_id); |
| |
| 315 | + | |
|
243 | 316 | | if ( !is_numeric($quantity) ) { |
  |
244 | | - | $quantity = $this->getQuantity($product_id) + 1; |
| |
| 317 | + | $quantity = $this->getQuantity($item_id) + 1; |
|
245 | 318 | | } |
| |
246 | 319 | | |
  |
247 | | - | $this->_contents[$product_id]['quantity'] = $quantity; |
| |
| 320 | + | $this->_contents[$item_id]['quantity'] = $quantity; |
|
248 | 321 | | |
| |
249 | 322 | | if ( $osC_Customer->isLoggedOn() ) { |
  |
250 | | - | $Qupdate = $osC_Database->query('update :table_customers_basket set customers_basket_quantity = :customers_basket_quantity where customers_id = :customers_id and products_id = :products_id'); |
| |
251 | | - | $Qupdate->bindTable(':table_customers_basket', TABLE_CUSTOMERS_BASKET); |
| |
252 | | - | $Qupdate->bindInt(':customers_basket_quantity', $quantity); |
| |
| 323 | + | $Qupdate = $osC_Database->query('update :table_shopping_carts set quantity = :quantity where customers_id = :customers_id and item_id = :item_id'); |
| |
| 324 | + | $Qupdate->bindTable(':table_shopping_carts', TABLE_SHOPPING_CARTS); |
| |
| 325 | + | $Qupdate->bindInt(':quantity', $quantity); |
|
253 | 326 | | $Qupdate->bindInt(':customers_id', $osC_Customer->getID()); |
  |
254 | | - | $Qupdate->bindInt(':products_id', $product_id); |
| |
| 327 | + | $Qupdate->bindInt(':item_id', $item_id); |
|
255 | 328 | | $Qupdate->execute(); |
| |
256 | 329 | | } |
| |
257 | 330 | | } else { |
| |
|
|
 |
… |
|
275 | 348 | | } |
| |
276 | 349 | | } |
| |
277 | 350 | | |
  |
278 | | - | $this->_contents[$product_id] = array('id' => $product_id, |
| |
279 | | - | 'parent_id' => $Qproduct->valueInt('parent_id'), |
| |
280 | | - | 'name' => $Qdescription->value('products_name'), |
| |
281 | | - | 'keyword' => $Qdescription->value('products_keyword'), |
| |
282 | | - | 'image' => $Qproduct->value('image'), |
| |
283 | | - | 'price' => $price, |
| |
284 | | - | 'quantity' => $quantity, |
| |
285 | | - | 'weight' => $Qproduct->value('products_weight'), |
| |
286 | | - | 'tax_class_id' => $Qproduct->valueInt('products_tax_class_id'), |
| |
287 | | - | 'date_added' => osC_DateTime::getShort(osC_DateTime::getNow()), |
| |
288 | | - | 'weight_class_id' => $Qproduct->valueInt('products_weight_class')); |
| |
| 351 | + | if ( $osC_Customer->isLoggedOn() ) { |
| |
| 352 | + | $Qid = $osC_Database->query('select max(item_id) as item_id from :table_shopping_carts where customers_id = :customers_id'); |
| |
| 353 | + | $Qid->bindTable(':table_shopping_carts', TABLE_SHOPPING_CARTS); |
| |
| 354 | + | $Qid->bindInt(':customers_id', $osC_Customer->getID()); |
| |
| 355 | + | $Qid->execute(); |
|
289 | 356 | | |
  |
| 357 | + | $item_id = $Qid->valueInt('item_id') + 1; |
| |
| 358 | + | } else { |
| |
| 359 | + | $item_id = max(array_keys($this->_contents)) + 1; |
| |
| 360 | + | } |
| |
| 361 | + | |
| |
| 362 | + | $this->_contents[$item_id] = array('item_id' => $item_id, |
| |
| 363 | + | 'id' => $product_id, |
| |
| 364 | + | 'parent_id' => $Qproduct->valueInt('parent_id'), |
| |
| 365 | + | 'name' => $Qdescription->value('products_name'), |
| |
| 366 | + | 'keyword' => $Qdescription->value('products_keyword'), |
| |
| 367 | + | 'image' => $Qproduct->value('image'), |
| |
| 368 | + | 'price' => $price, |
| |
| 369 | + | 'quantity' => $quantity, |
| |
| 370 | + | 'weight' => $Qproduct->value('products_weight'), |
| |
| 371 | + | 'tax_class_id' => $Qproduct->valueInt('products_tax_class_id'), |
| |
| 372 | + | 'date_added' => osC_DateTime::getShort(osC_DateTime::getNow()), |
| |
| 373 | + | 'weight_class_id' => $Qproduct->valueInt('products_weight_class')); |
| |
| 374 | + | |
|
290 | 375 | | if ( $osC_Customer->isLoggedOn() ) { |
  |
291 | | - | $Qnew = $osC_Database->query('insert into :table_customers_basket (customers_id, products_id, customers_basket_quantity, customers_basket_date_added) values (:customers_id, :products_id, :customers_basket_quantity, now())'); |
| |
292 | | - | $Qnew->bindTable(':table_customers_basket', TABLE_CUSTOMERS_BASKET); |
| |
| 376 | + | $Qnew = $osC_Database->query('insert into :table_shopping_carts (customers_id, item_id, products_id, quantity, date_added) values (:customers_id, :item_id, :products_id, :quantity, :date_added)'); |
| |
| 377 | + | $Qnew->bindTable(':table_shopping_carts', TABLE_SHOPPING_CARTS); |
|
293 | 378 | | $Qnew->bindInt(':customers_id', $osC_Customer->getID()); |
  |
| 379 | + | $Qnew->bindInt(':item_id', $item_id); |
|
294 | 380 | | $Qnew->bindInt(':products_id', $product_id); |
  |
295 | | - | $Qnew->bindInt(':customers_basket_quantity', $quantity); |
| |
| 381 | + | $Qnew->bindInt(':quantity', $quantity); |
| |
| 382 | + | $Qnew->bindRaw(':date_added', 'now()'); |
|
296 | 383 | | $Qnew->execute(); |
| |
297 | 384 | | } |
| |
298 | 385 | | |
| |
299 | 386 | | if ( $Qproduct->valueInt('parent_id') > 0 ) { |
  |
300 | | - | $Qvariant = $osC_Database->query('select pvg.id as group_id, pvg.title as group_title, pvv.id as value_id, pvv.title as value_title from :table_products_variants pv, :table_products_variants_values pvv, :table_products_variants_groups pvg where pv.products_id = :products_id and pv.variants_values_id = pvv.id and pvv.languages_id = :languages_id and pvv.products_variants_groups_id = pvg.id and pvg.languages_id = :languages_id'); |
| |
| 387 | + | $Qvariant = $osC_Database->query('select pvg.id as group_id, pvg.title as group_title, pvg.module, pvv.id as value_id, pvv.title as value_title from :table_products_variants pv, :table_products_variants_values pvv, :table_products_variants_groups pvg where pv.products_id = :products_id and pv.variants_values_id = pvv.id and pvv.languages_id = :languages_id and pvv.products_variants_groups_id = pvg.id and pvg.languages_id = :languages_id'); |
|
301 | 388 | | $Qvariant->bindTable(':table_products_variants', TABLE_PRODUCTS_VARIANTS); |
| |
302 | 389 | | $Qvariant->bindTable(':table_products_variants_values', TABLE_PRODUCTS_VARIANTS_VALUES); |
| |
303 | 390 | | $Qvariant->bindTable(':table_products_variants_groups', TABLE_PRODUCTS_VARIANTS_GROUPS); |
| |
|
|
 |
… |
|
307 | 394 | | $Qvariant->execute(); |
| |
308 | 395 | | |
| |
309 | 396 | | while ( $Qvariant->next() ) { |
  |
310 | | - | $this->_contents[$product_id]['variants'][$Qvariant->valueInt('group_id')] = array('group_id' => $Qvariant->valueInt('group_id'), |
| |
311 | | - | 'value_id' => $Qvariant->valueInt('value_id'), |
| |
312 | | - | 'group_title' => $Qvariant->value('group_title'), |
| |
313 | | - | 'value_title' => $Qvariant->value('value_title')); |
| |
| 397 | + | $group_title = osC_Variants::getGroupTitle($Qvariant->value('module'), $Qvariant->toArray()); |
| |
| 398 | + | $value_title = osC_Variants::getValueTitle($Qvariant->value('module'), $Qvariant->toArray()); |
| |
| 399 | + | $has_custom_value = osC_Variants::hasCustomValue($Qvariant->value('module')); |
| |
| 400 | + | |
| |
| 401 | + | $this->_contents[$item_id]['variants'][] = array('group_id' => $Qvariant->valueInt('group_id'), |
| |
| 402 | + | 'value_id' => $Qvariant->valueInt('value_id'), |
| |
| 403 | + | 'group_title' => $group_title, |
| |
| 404 | + | 'value_title' => $value_title, |
| |
| 405 | + | 'has_custom_value' => $has_custom_value); |
| |
| 406 | + | |
| |
| 407 | + | if ( $osC_Customer->isLoggedOn() && ($has_custom_value === true) ) { |
| |
| 408 | + | $Qnew = $osC_Database->query('insert into :table_shopping_carts_custom_variants_values (shopping_carts_item_id, customers_id, products_id, products_variants_values_id, products_variants_values_text) values (:shopping_carts_item_id, :customers_id, :products_id, :products_variants_values_id, :products_variants_values_text)'); |
| |
| 409 | + | $Qnew->bindTable(':table_shopping_carts_custom_variants_values', TABLE_SHOPPING_CARTS_CUSTOM_VARIANTS_VALUES); |
| |
| 410 | + | $Qnew->bindInt(':shopping_carts_item_id', $item_id); |
| |
| 411 | + | $Qnew->bindInt(':customers_id', $osC_Customer->getID()); |
| |
| 412 | + | $Qnew->bindInt(':products_id', $product_id); |
| |
| 413 | + | $Qnew->bindInt(':products_variants_values_id', $Qvariant->valueInt('value_id')); |
| |
| 414 | + | $Qnew->bindValue(':products_variants_values_text', $value_title); |
| |
| 415 | + | $Qnew->execute(); |
| |
| 416 | + | } |
|
314 | 417 | | } |
| |
315 | 418 | | } |
| |
316 | 419 | | } |
| |
|
|
 |
… |
|
324 | 427 | | public function numberOfItems() { |
| |
325 | 428 | | $total = 0; |
| |
326 | 429 | | |
  |
327 | | - | if ( $this->hasContents() ) { |
| |
328 | | - | foreach ( $this->_contents as $product ) { |
| |
329 | | - | $total += $this->getQuantity($product['id']); |
| |
330 | | - | } |
| |
| 430 | + | foreach ( $this->_contents as $product ) { |
| |
| 431 | + | $total += $product['quantity']; |
|
331 | 432 | | } |
| |
332 | 433 | | |
| |
333 | 434 | | return $total; |
| |
334 | 435 | | } |
| |
335 | 436 | | |
  |
336 | | - | public function getQuantity($product_id) { |
| |
337 | | - | return ( isset($this->_contents[$product_id]) ) ? $this->_contents[$product_id]['quantity'] : 0; |
| |
| 437 | + | public function getBasketID($product_id) { |
| |
| 438 | + | foreach ( $this->_contents as $item_id => $product ) { |
| |
| 439 | + | if ( $product['id'] === $product_id ) { |
| |
| 440 | + | return $item_id; |
| |
| 441 | + | } |
| |
| 442 | + | } |
|
338 | 443 | | } |
| |
339 | 444 | | |
  |
| 445 | + | public function getQuantity($item_id) { |
| |
| 446 | + | return ( isset($this->_contents[$item_id]) ) ? $this->_contents[$item_id]['quantity'] : 0; |
| |
| 447 | + | } |
| |
| 448 | + | |
|
340 | 449 | | public function exists($product_id) { |
  |
341 | | - | return isset($this->_contents[$product_id]); |
| |
| 450 | + | foreach ( $this->_contents as $product ) { |
| |
| 451 | + | if ( $product['id'] === $product_id ) { |
| |
| 452 | + | if ( isset($product['variants']) ) { |
| |
| 453 | + | foreach ( $product['variants'] as $variant ) { |
| |
| 454 | + | if ( $variant['has_custom_value'] === true ) { |
| |
| 455 | + | return false; |
| |
| 456 | + | } |
| |
| 457 | + | } |
| |
| 458 | + | } |
| |
| 459 | + | |
| |
| 460 | + | return true; |
| |
| 461 | + | } |
| |
| 462 | + | } |
| |
| 463 | + | |
| |
| 464 | + | return false; |
|
342 | 465 | | } |
| |
343 | 466 | | |
  |
344 | | - | public function remove($product_id) { |
| |
| 467 | + | public function update($item_id, $quantity) { |
|
345 | 468 | | global $osC_Database, $osC_Customer; |
| |
346 | 469 | | |
  |
347 | | - | unset($this->_contents[$product_id]); |
| |
| 470 | + | if ( !is_numeric($quantity) ) { |
| |
| 471 | + | $quantity = $this->getQuantity($item_id) + 1; |
| |
| 472 | + | } |
|
348 | 473 | | |
  |
| 474 | + | $this->_contents[$item_id]['quantity'] = $quantity; |
| |
| 475 | + | |
|
349 | 476 | | if ( $osC_Customer->isLoggedOn() ) { |
  |
350 | | - | $Qdelete = $osC_Database->query('delete from :table_customers_basket where customers_id = :customers_id and products_id = :products_id'); |
| |
351 | | - | $Qdelete->bindTable(':table_customers_basket', TABLE_CUSTOMERS_BASKET); |
| |
| 477 | + | $Qupdate = $osC_Database->query('update :table_shopping_carts set quantity = :quantity where customers_id = :customers_id and item_id = :item_id'); |
| |
| 478 | + | $Qupdate->bindTable(':table_shopping_carts', TABLE_SHOPPING_CARTS); |
| |
| 479 | + | $Qupdate->bindInt(':quantity', $quantity); |
| |
| 480 | + | $Qupdate->bindInt(':customers_id', $osC_Customer->getID()); |
| |
| 481 | + | $Qupdate->bindInt(':item_id', $item_id); |
| |
| 482 | + | $Qupdate->execute(); |
| |
| 483 | + | } |
| |
| 484 | + | |
| |
| 485 | + | $this->_cleanUp(); |
| |
| 486 | + | $this->_calculate(); |
| |
| 487 | + | } |
| |
| 488 | + | |
| |
| 489 | + | public function remove($item_id) { |
| |
| 490 | + | global $osC_Database, $osC_Customer; |
| |
| 491 | + | |
| |
| 492 | + | unset($this->_contents[$item_id]); |
| |
| 493 | + | |
| |
| 494 | + | if ( $osC_Customer->isLoggedOn() ) { |
| |
| 495 | + | $Qdelete = $osC_Database->query('delete from :table_shopping_carts where customers_id = :customers_id and item_id = :item_id'); |
| |
| 496 | + | $Qdelete->bindTable(':table_shopping_carts', TABLE_SHOPPING_CARTS); |
|
352 | 497 | | $Qdelete->bindInt(':customers_id', $osC_Customer->getID()); |
  |
353 | | - | $Qdelete->bindInt(':products_id', $product_id); |
| |
| 498 | + | $Qdelete->bindInt(':item_id', $item_id); |
|
354 | 499 | | $Qdelete->execute(); |
  |
| 500 | + | |
| |
| 501 | + | $Qdelete = $osC_Database->query('delete from :table_shopping_carts_custom_variants_values where customers_id = :customers_id and shopping_carts_item_id = :shopping_carts_item_id'); |
| |
| 502 | + | $Qdelete->bindTable(':table_shopping_carts_custom_variants_values', TABLE_SHOPPING_CARTS_CUSTOM_VARIANTS_VALUES); |
| |
| 503 | + | $Qdelete->bindInt(':customers_id', $osC_Customer->getID()); |
| |
| 504 | + | $Qdelete->bindInt(':shopping_carts_item_id', $item_id); |
| |
| 505 | + | $Qdelete->execute(); |
|
355 | 506 | | } |
| |
356 | 507 | | |
| |
357 | 508 | | $this->_calculate(); |
| |
|
|
 |
… |
|
450 | 601 | | return $this->_content_type; |
| |
451 | 602 | | } |
| |
452 | 603 | | |
  |
453 | | - | public function isVariant($id) { |
| |
454 | | - | return isset($this->_contents[$id]['variants']) && !empty($this->_contents[$id]['variants']); |
| |
| 604 | + | public function isVariant($item_id) { |
| |
| 605 | + | return isset($this->_contents[$item_id]['variants']) && !empty($this->_contents[$item_id]['variants']); |
|
455 | 606 | | } |
| |
456 | 607 | | |
  |
457 | | - | public function getVariant($id) { |
| |
458 | | - | if ( isset($this->_contents[$id]['variants']) && !empty($this->_contents[$id]['variants']) ) { |
| |
459 | | - | return $this->_contents[$id]['variants']; |
| |
| 608 | + | public function getVariant($item_id) { |
| |
| 609 | + | if ( isset($this->_contents[$item_id]['variants']) && !empty($this->_contents[$item_id]['variants']) ) { |
| |
| 610 | + | return $this->_contents[$item_id]['variants']; |
|
460 | 611 | | } |
| |
461 | 612 | | } |
| |
462 | 613 | | |
  |
463 | | - | public function isInStock($product_id) { |
| |
| 614 | + | public function isInStock($item_id) { |
|
464 | 615 | | global $osC_Database; |
| |
465 | 616 | | |
| |
466 | 617 | | $Qstock = $osC_Database->query('select products_quantity from :table_products where products_id = :products_id'); |
| |
467 | 618 | | $Qstock->bindTable(':table_products', TABLE_PRODUCTS); |
  |
468 | | - | $Qstock->bindInt(':products_id', $product_id); |
| |
| 619 | + | $Qstock->bindInt(':products_id', $this->_contents[$item_id]['id']); |
|
469 | 620 | | $Qstock->execute(); |
| |
470 | 621 | | |
  |
471 | | - | if ( ($Qstock->valueInt('products_quantity') - $this->_contents[$product_id]['quantity']) > 0 ) { |
| |
| 622 | + | if ( ($Qstock->valueInt('products_quantity') - $this->_contents[$item_id]['quantity']) > 0 ) { |
|
472 | 623 | | return true; |
| |
473 | 624 | | } elseif ( $this->_products_in_stock === true ) { |
| |
474 | 625 | | $this->_products_in_stock = false; |
| |
|
|
 |
… |
|
708 | 859 | | private function _cleanUp() { |
| |
709 | 860 | | global $osC_Database, $osC_Customer; |
| |
710 | 861 | | |
  |
711 | | - | foreach ( $this->_contents as $product_id => $data ) { |
| |
| 862 | + | foreach ( $this->_contents as $item_id => $data ) { |
|
712 | 863 | | if ( $data['quantity'] < 1 ) { |
  |
713 | | - | unset($this->_contents[$product_id]); |
| |
| 864 | + | unset($this->_contents[$item_id]); |
|
714 | 865 | | |
| |
715 | 866 | | if ( $osC_Customer->isLoggedOn() ) { |
  |
716 | | - | $Qdelete = $osC_Database->query('delete from :table_customers_basket where customers_id = :customers_id and products_id = :products_id'); |
| |
717 | | - | $Qdelete->bindTable(':table_customers_basket', TABLE_CUSTOMERS_BASKET); |
| |
| 867 | + | $Qdelete = $osC_Database->query('delete from :table_shopping_carts where customers_id = :customers_id and item_id = :item_id'); |
| |
| 868 | + | $Qdelete->bindTable(':table_shopping_carts', TABLE_SHOPPING_CARTS); |
|
718 | 869 | | $Qdelete->bindInt(':customers_id', $osC_Customer->getID()); |
  |
719 | | - | $Qdelete->bindValue(':products_id', $product_id); |
| |
| 870 | + | $Qdelete->bindInt(':item_id', $item_id); |
|
720 | 871 | | $Qdelete->execute(); |
  |
| 872 | + | |
| |
| 873 | + | $Qdelete = $osC_Database->query('delete from :table_shopping_carts_custom_variants_values where customers_id = :customers_id and shopping_carts_item_id = :shopping_carts_item_id'); |
| |
| 874 | + | $Qdelete->bindTable(':table_shopping_carts_custom_variants_values', TABLE_SHOPPING_CARTS_CUSTOM_VARIANTS_VALUES); |
| |
| 875 | + | $Qdelete->bindInt(':customers_id', $osC_Customer->getID()); |
| |
| 876 | + | $Qdelete->bindInt(':shopping_carts_item_id', $item_id); |
| |
| 877 | + | $Qdelete->execute(); |
  |
721 | 878 | | } |
| |
722 | 879 | | } |
| |
723 | 880 | | } |