@@ -11,34 +11,39 @@ class ProductsController extends Controller
1111{
1212 private $ api2cart ;
1313
14+ /**
15+ * ProductsController constructor.
16+ * @param Api2Cart $api2Cart
17+ */
1418 public function __construct (Api2Cart $ api2Cart )
1519 {
1620 $ this ->api2cart = $ api2Cart ;
1721 }
1822
23+ /**
24+ * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
25+ */
1926 public function index ()
2027 {
2128 return view ('products.index ' );
2229 }
2330
31+ /**
32+ * @param string|null $store_id Store ID
33+ * @param Request $request Request
34+ * @return \Illuminate\Http\JsonResponse
35+ */
2436 public function productList ($ store_id =null ,Request $ request )
2537 {
2638 \Debugbar::disable ();
2739
28-
29-
30- /**
31- * get account carts & extract exact store info
32- */
33- $ carts = collect ($ this ->api2cart ->getCartList ());
3440 $ storeInfo = $ this ->api2cart ->getCart ( $ store_id );
3541
3642 $ sort_by = ($ request ->get ('sort_by ' )) ? 'create_at ' : null ;
3743 $ sort_direct = ($ request ->get ('sort_direct ' )) ? true : false ;
3844 $ created_from = ($ request ->get ('created_from ' )) ? $ request ->get ('created_from ' ) : null ;
3945 $ limit = ($ request ->get ('limit ' )) ? $ request ->get ('limit ' ) : null ;
4046
41-
4247 $ totalProducts = $ this ->api2cart ->getProductCount ( $ store_id );
4348
4449 $ products = collect ([]);
@@ -57,14 +62,30 @@ public function productList($store_id=null,Request $request)
5762 // collect product variants
5863 if ( isset ($ item ['type ' ]) && $ item ['type ' ] === 'configurable ' ){
5964 $ pv = $ this ->api2cart ->getProductVariants ($ store_id , $ item ['id ' ] );
60- $ newItem ['children ' ] = $ pv ['children ' ] ?? [];
65+ $ newItem ['children ' ] = [];
66+
67+ if (!empty ($ pv ['children ' ])) {
68+ $ childrens = collect ($ pv ['children ' ]);
69+ $ minPrice = $ childrens ->where ('default_price ' , $ childrens ->min ('default_price ' ))->first ();
70+
71+ if ($ minPrice ) {
72+ $ newItem ['children ' ]['min ' ] = $ minPrice ;
73+ }
74+
75+ $ maxPrice = $ childrens ->where ('default_price ' , $ childrens ->max ('default_price ' ))->first ();
76+
77+ if ($ maxPrice && isset ($ newItem ['children ' ]['min ' ]) && $ newItem ['children ' ]['min ' ]['default_price ' ] < $ maxPrice ['default_price ' ]) {
78+ $ newItem ['children ' ]['max ' ] = $ maxPrice ;
79+ }
80+ }
81+
82+ $ newItem ['children ' ] = array_values ($ newItem ['children ' ]);
6183 }
6284
6385 $ products ->push ( $ newItem );
6486 }
6587 }
6688
67-
6889 if ( isset ($ result ['pagination ' ]['next ' ]) && strlen ($ result ['pagination ' ]['next ' ]) ){
6990 // get next iteration to load rest customers
7091 while ( isset ($ result ['pagination ' ]['next ' ]) && strlen ($ result ['pagination ' ]['next ' ]) ){
@@ -77,19 +98,33 @@ public function productList($store_id=null,Request $request)
7798 $ newItem ['currency ' ] = ( isset ($ storeInfo ['stores_info ' ][0 ]['currency ' ]) ) ? $ storeInfo ['stores_info ' ][0 ]['currency ' ]['iso3 ' ] : '' ;
7899
79100 // collect product variants
80- if ( isset ($ item ['type ' ]) && $ item ['type ' ] === 'configurable ' ) {
101+ if (isset ($ item ['type ' ]) && $ item ['type ' ] === 'configurable ' ) {
81102 $ pv = $ this ->api2cart ->getProductVariants ($ store_id , $ item ['id ' ] );
82- $ newItem ['children ' ] = $ pv ['children ' ];
103+ $ newItem ['children ' ] = [];
104+
105+ if (!empty ($ pv ['children ' ])) {
106+ $ childrens = collect ($ pv ['children ' ]);
107+ $ minPrice = $ childrens ->where ('default_price ' , $ childrens ->min ('default_price ' ))->first ();
108+
109+ if ($ minPrice ) {
110+ $ newItem ['children ' ]['min ' ] = $ minPrice ;
111+ }
112+
113+ $ maxPrice = $ childrens ->where ('default_price ' , $ childrens ->max ('default_price ' ))->first ();
114+
115+ if ($ maxPrice && isset ($ newItem ['children ' ]['min ' ]) && $ newItem ['children ' ]['min ' ]['default_price ' ] < $ maxPrice ['default_price ' ]) {
116+ $ newItem ['children ' ]['max ' ] = $ maxPrice ;
117+ }
118+ }
119+
120+ $ newItem ['children ' ] = array_values ($ newItem ['children ' ]);
83121 }
84122
85123 $ products ->push ( $ newItem );
86124 }
87125 }
88126 }
89-
90127 }
91-
92-
93128 }
94129
95130 if ( $ sort_by ){
@@ -117,15 +152,16 @@ public function productList($store_id=null,Request $request)
117152 ];
118153
119154 return response ()->json ($ data );
120-
121-
122-
123155 }
124156
125-
126157 /**
127158 * Show the form for editing the specified resource.
128159 *
160+ * @param string|null $store_id Store ID
161+ * @param string|null $product_id Product ID
162+ * @param ProductRequest $request Request
163+ * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
164+ * @throws \Throwable
129165 */
130166 public function edit ($ store_id =null , $ product_id =null , Request $ request )
131167 {
@@ -138,16 +174,20 @@ public function edit($store_id=null, $product_id=null, Request $request)
138174 $ product ['children ' ] = $ pv ['children ' ];
139175 }
140176
141- // Log::debug( 'edit product ');
142- // Log::debug( print_r($product,1));
143-
144177 if ( $ request ->ajax () ){
145178 return response ()->json (['data ' => view ('products.form ' ,compact ('product ' ,'store_id ' , 'product_id ' ))->render (), 'item ' => $ product ,'log ' => $ this ->api2cart ->getLog () ]);
146179 }
147180
148181 return redirect (route ('products.index ' ));
149182 }
150183
184+ /**
185+ * @param string|null $store_id Store ID
186+ * @param string|null $product_id Product ID
187+ * @param ProductRequest $request Request
188+ *
189+ * @return \Illuminate\Http\JsonResponse
190+ */
151191 public function update ($ store_id =null , $ product_id =null , ProductRequest $ request )
152192 {
153193 \Debugbar::disable ();
@@ -225,21 +265,15 @@ public function update($store_id=null, $product_id=null, ProductRequest $request
225265 }
226266
227267 return response ()->json (['item ' => $ result , 'log ' => $ this ->api2cart ->getLog ()]);
228-
229268 }
230-
231-
232-
233269 }
234270
235-
236-
237271 /**
238272 * Remove the specified resource from storage.
239273 *
240- * @param int $id
241- *
242- * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
274+ * @param string|null $store_id Store ID
275+ * @param string|null $product_id Product ID
276+ * @return \Illuminate\Http\JsonResponse
243277 */
244278 public function destroy ($ store_id =null , $ product_id =null )
245279 {
@@ -248,18 +282,6 @@ public function destroy($store_id=null, $product_id=null)
248282 } else {
249283 return response ()->json ([ 'log ' => $ this ->api2cart ->getLog () ], 404 );
250284 }
251-
252- }
253-
254- public function destroyImage ($ store_id =null , $ product_id =null , Request $ request )
255- {
256-
257- // Log::debug("{$store_id} {$product_id}");
258- // Log::debug( $request->all() );
259-
260- // use image url as key, cause different store have different info
261- // $this->api2cart->deleteProductImage($store_id, $product_id, $request->get('key') );
262-
263285 }
264286
265287}
0 commit comments