@@ -25,8 +25,9 @@ public function __construct()
2525 $ this ->session = \Config \Services::session ();
2626
2727 // Libraries
28- $ this ->setting = new \App \Libraries \Setting ();
2928 $ this ->currency = new \App \Libraries \Currency ();
29+ $ this ->language = new \App \Libraries \Language ();
30+ $ this ->setting = new \App \Libraries \Setting ();
3031 $ this ->weight = new \App \Libraries \Weight ();
3132
3233 // Remove all expired cart items with no customer_id
@@ -87,7 +88,7 @@ public function __construct()
8788 foreach ($ cart_query ->getResult () as $ result ) {
8889 if ($ result ->seller_id !== $ seller_id ) {
8990 // Add cart to customer
90- $ this ->add ($ customer_id , $ result ->seller_id , $ result ->product_id , $ result ->quantity );
91+ $ this ->add ($ customer_id , $ result ->seller_id , $ result ->product_id , $ result ->quantity , json_decode ( $ result -> option , true ) );
9192 }
9293
9394 // Delete cart
@@ -119,11 +120,16 @@ public function remove($customer_id, $seller_id, $key)
119120 */
120121 public function add ($ customer_id , $ seller_id , $ product_id , $ quantity , $ options = [])
121122 {
123+ if (is_array ($ options )) {
124+ asort ($ options );
125+ }
126+
122127 $ cart_builder = $ this ->db ->table ('cart ' );
123128
124129 $ cart_builder ->where ('customer_id ' , $ customer_id );
125130 $ cart_builder ->where ('seller_id ' , $ seller_id );
126131 $ cart_builder ->where ('product_id ' , $ product_id );
132+ $ cart_builder ->where ('option ' , json_encode ($ options ));
127133 $ cart_builder ->where ('key ' , $ this ->getKey ());
128134
129135 $ cart_query = $ cart_builder ->get ();
@@ -133,10 +139,12 @@ public function add($customer_id, $seller_id, $product_id, $quantity, $options =
133139 $ cart_update_builder = $ this ->db ->table ('cart ' );
134140
135141 $ cart_update_builder ->set ('quantity ' , 'quantity + ' . $ quantity , false );
142+ $ cart_update_builder ->set ('date_modified ' , new Time ('now ' ));
136143
137144 $ cart_update_builder ->where ('customer_id ' , $ customer_id );
138145 $ cart_update_builder ->where ('seller_id ' , $ seller_id );
139146 $ cart_update_builder ->where ('product_id ' , $ product_id );
147+ $ cart_update_builder ->where ('option ' , json_encode ($ options ));
140148 $ cart_update_builder ->where ('key ' , $ this ->getKey ());
141149
142150 $ cart_update_builder ->update ();
@@ -150,7 +158,9 @@ public function add($customer_id, $seller_id, $product_id, $quantity, $options =
150158 'key ' => $ this ->getKey (),
151159 'product_id ' => $ product_id ,
152160 'quantity ' => $ quantity ,
161+ 'option ' => json_encode ($ options ),
153162 'date_added ' => new Time ('now ' ),
163+ 'date_modified ' => new Time ('now ' ),
154164 ];
155165
156166 $ cart_insert_builder ->insert ($ cart_insert_data );
@@ -231,6 +241,8 @@ public function getProducts($seller_id)
231241 $ cart_builder ->where ('seller_id ' , $ seller_id );
232242 $ cart_builder ->where ('key ' , $ this ->getKey ());
233243
244+ $ cart_builder ->orderBy ('date_modified ' , 'DESC ' );
245+
234246 $ cart_query = $ cart_builder ->get ();
235247
236248 foreach ($ cart_query ->getResult () as $ result ) {
@@ -248,6 +260,118 @@ public function getProducts($seller_id)
248260 $ product_query = $ product_builder ->get ();
249261
250262 if ($ product = $ product_query ->getRow ()) {
263+ // Get options
264+ $ option_data = [];
265+
266+ if (is_array (json_decode ($ result ->option , true ))) {
267+ $ options = json_decode ($ result ->option , true );
268+ } else {
269+ $ options = [];
270+ }
271+
272+ foreach ($ options as $ key => $ value ) {
273+ // Get option
274+ $ option_builder = $ this ->db ->table ('option o ' );
275+
276+ $ option_builder ->where ('o.option_id ' , $ key );
277+
278+ $ option_query = $ option_builder ->get ();
279+
280+ if ($ option_row = $ option_query ->getRow ()) {
281+ // Get option description
282+ $ option_description_data = [];
283+
284+ $ option_description_builder = $ this ->db ->table ('option_description ' );
285+
286+ $ option_description_builder ->where ('option_id ' , $ key );
287+
288+ $ option_description_query = $ option_description_builder ->get ();
289+
290+ foreach ($ option_description_query ->getResult () as $ option_description ) {
291+ $ option_description_data [$ option_description ->language_id ] = [
292+ 'name ' => $ option_description ->name ,
293+ ];
294+ }
295+
296+ // Get option value
297+ $ option_value_data = [];
298+
299+ $ option_value_builder = $ this ->db ->table ('option_value ov ' );
300+
301+ $ option_value_builder ->where ('ov.option_id ' , $ key );
302+ $ option_value_builder ->where ('ov.option_value_id ' , $ value );
303+
304+ $ option_value_query = $ option_value_builder ->get ();
305+
306+ if ($ option_value_row = $ option_value_query ->getRow ()) {
307+ // Get option value description
308+ $ option_value_description_data = [];
309+
310+ $ option_value_description_builder = $ this ->db ->table ('option_value_description ' );
311+
312+ $ option_value_description_builder ->where ('option_id ' , $ key );
313+ $ option_value_description_builder ->where ('option_value_id ' , $ value );
314+
315+ $ option_value_description_query = $ option_value_description_builder ->get ();
316+
317+ foreach ($ option_value_description_query ->getResult () as $ option_value_description ) {
318+ $ option_value_description_data [$ option_value_description ->language_id ] = [
319+ 'name ' => $ option_value_description ->name ,
320+ ];
321+ }
322+
323+ $ option_value_data = [
324+ 'option_id ' => $ option_value_row ->option_id ,
325+ 'option_value_id ' => $ option_value_row ->option_value_id ,
326+ 'seller_id ' => $ option_value_row ->seller_id ,
327+ 'customer_id ' => $ option_value_row ->customer_id ,
328+ 'description ' => $ option_value_description_data ,
329+ 'sort_order ' => $ option_value_row ->sort_order ,
330+ 'status ' => $ option_value_row ->status ,
331+ ];
332+ }
333+
334+ $ option_data [] = [
335+ 'option_id ' => $ option_row ->option_id ,
336+ 'seller_id ' => $ option_row ->seller_id ,
337+ 'customer_id ' => $ option_row ->customer_id ,
338+ 'description ' => $ option_description_data ,
339+ 'sort_order ' => $ option_row ->sort_order ,
340+ 'status ' => $ option_row ->status ,
341+ 'option_value ' => $ option_value_data ,
342+ ];
343+ }
344+ }
345+
346+ $ option_data_sort_order = [];
347+
348+ foreach ($ option_data as $ key => $ value ) {
349+ $ option_data_sort_order [$ key ] = $ value ['sort_order ' ];
350+ }
351+
352+ array_multisort ($ option_data_sort_order , SORT_ASC , $ option_data );
353+
354+ // Product variant
355+ if (!empty ($ product ->product_option )) {
356+ // Get product variant info
357+ $ product_variant_builder = $ this ->db ->table ('product_variant ' );
358+
359+ $ product_variant_builder ->where ('options ' , $ result ->option );
360+
361+ $ product_variant_query = $ product_variant_builder ->get ();
362+
363+ if ($ product_variant_row = $ product_variant_query ->getRow ()) {
364+ $ price = $ product_variant_row ->price ;
365+ $ weight = $ product_variant_row ->weight ;
366+ } else {
367+ $ price = $ product ->price ;
368+ $ weight = $ product ->weight ;
369+ }
370+ } else {
371+ $ price = $ product ->price ;
372+ $ weight = $ product ->weight ;
373+ }
374+
251375 $ products [] = [
252376 'cart_id ' => $ result ->cart_id ,
253377 'customer_id ' => $ result ->customer_id ,
@@ -256,13 +380,16 @@ public function getProducts($seller_id)
256380 'name ' => $ product ->name ,
257381 'description ' => $ product ->description ,
258382 'slug ' => $ product ->slug ,
259- 'price ' => $ product ->price ,
260- 'weight ' => $ product ->weight ,
383+ 'product_option ' => $ product ->product_option ,
384+ 'price ' => $ price ,
385+ 'weight ' => $ weight ,
261386 'weight_class_id ' => $ product ->weight_class_id ,
262387 'main_image ' => $ product ->main_image ,
263388 'quantity ' => $ result ->quantity ,
264- 'total ' => $ product -> price * $ result ->quantity ,
389+ 'total ' => $ price * $ result ->quantity ,
265390 'date_added ' => $ result ->date_added ,
391+ 'option ' => $ option_data ,
392+ 'option_ids ' => $ result ->option ,
266393 ];
267394 }
268395 }
0 commit comments