33namespace Codexshaper \WooCommerce \Traits ;
44
55use Codexshaper \WooCommerce \Facades \WooCommerce ;
6+ use Codexshaper \WooCommerce \WooCommerceApi ;
67use Illuminate \Support \LazyCollection ;
78
89trait QueryBuilderTrait
@@ -37,6 +38,16 @@ trait QueryBuilderTrait
3738 */
3839 protected $ isOriginal = false ;
3940
41+ protected $ config = [];
42+
43+ protected function wooCommerCeInstance ()
44+ {
45+ return sizeof ($ this ->config ) == 0
46+ ? new WooCommerceApi ()
47+ : (new WooCommerceApi ())->setConfig ($ this ->config );
48+ }
49+
50+
4051 /**
4152 * Retrieve all Items.
4253 *
@@ -47,14 +58,14 @@ trait QueryBuilderTrait
4758 protected function all ($ options = [])
4859 {
4960 if ($ this ->isLazyCollection ) {
50- return LazyCollection::make (WooCommerce:: all ($ this ->endpoint , $ options ));
61+ return LazyCollection::make ( $ this -> wooCommerCeInstance ()-> all ($ this ->endpoint , $ options ));
5162 }
5263
5364 if ($ this ->isCollection ) {
54- return collect (WooCommerce:: all ($ this ->endpoint , $ options ));
65+ return collect ( $ this -> wooCommerCeInstance ()-> all ($ this ->endpoint , $ options ));
5566 }
5667
57- return WooCommerce:: all ($ this ->endpoint , $ options );
68+ return $ this -> wooCommerCeInstance ()-> all ($ this ->endpoint , $ options );
5869 }
5970
6071 /**
@@ -68,14 +79,14 @@ protected function all($options = [])
6879 protected function find ($ id , $ options = [])
6980 {
7081 if ($ this ->isLazyCollection ) {
71- return LazyCollection::make (WooCommerce:: find ("{$ this ->endpoint }/ {$ id }" , $ options ));
82+ return LazyCollection::make ( $ this -> wooCommerCeInstance ()-> find ("{$ this ->endpoint }/ {$ id }" , $ options ));
7283 }
7384
7485 if ($ this ->isCollection ) {
75- return collect (WooCommerce:: find ("{$ this ->endpoint }/ {$ id }" , $ options ));
86+ return collect ( $ this -> wooCommerCeInstance ()-> find ("{$ this ->endpoint }/ {$ id }" , $ options ));
7687 }
7788
78- return WooCommerce:: find ("{$ this ->endpoint }/ {$ id }" , $ options );
89+ return $ this -> wooCommerCeInstance ()-> find ("{$ this ->endpoint }/ {$ id }" , $ options );
7990 }
8091
8192 /**
@@ -88,14 +99,14 @@ protected function find($id, $options = [])
8899 protected function create ($ data )
89100 {
90101 if ($ this ->isLazyCollection ) {
91- return LazyCollection::make (WooCommerce:: create ($ this ->endpoint , $ data ));
102+ return LazyCollection::make ( $ this -> wooCommerCeInstance ()-> create ($ this ->endpoint , $ data ));
92103 }
93104
94105 if ($ this ->isCollection ) {
95- return collect (WooCommerce:: create ($ this ->endpoint , $ data ));
106+ return collect ( $ this -> wooCommerCeInstance ()-> create ($ this ->endpoint , $ data ));
96107 }
97108
98- return WooCommerce:: create ($ this ->endpoint , $ data );
109+ return $ this -> wooCommerCeInstance ()-> create ($ this ->endpoint , $ data );
99110 }
100111
101112 /**
@@ -109,14 +120,14 @@ protected function create($data)
109120 protected function update ($ id , $ data )
110121 {
111122 if ($ this ->isLazyCollection ) {
112- return LazyCollection::make (WooCommerce:: update ("{$ this ->endpoint }/ {$ id }" , $ data ));
123+ return LazyCollection::make ( $ this -> wooCommerCeInstance ()-> update ("{$ this ->endpoint }/ {$ id }" , $ data ));
113124 }
114125
115126 if ($ this ->isCollection ) {
116- return collect (WooCommerce:: update ("{$ this ->endpoint }/ {$ id }" , $ data ));
127+ return collect ( $ this -> wooCommerCeInstance ()-> update ("{$ this ->endpoint }/ {$ id }" , $ data ));
117128 }
118129
119- return WooCommerce:: update ("{$ this ->endpoint }/ {$ id }" , $ data );
130+ return $ this -> wooCommerCeInstance ()-> update ("{$ this ->endpoint }/ {$ id }" , $ data );
120131 }
121132
122133 /**
@@ -130,14 +141,14 @@ protected function update($id, $data)
130141 protected function delete ($ id , $ options = [])
131142 {
132143 if ($ this ->isLazyCollection ) {
133- return LazyCollection::make (WooCommerce:: delete ("{$ this ->endpoint }/ {$ id }" , $ options ));
144+ return LazyCollection::make ( $ this -> wooCommerCeInstance ()-> delete ("{$ this ->endpoint }/ {$ id }" , $ options ));
134145 }
135146
136147 if ($ this ->isCollection ) {
137- return collect (WooCommerce:: delete ("{$ this ->endpoint }/ {$ id }" , $ options ));
148+ return collect ( $ this -> wooCommerCeInstance ()-> delete ("{$ this ->endpoint }/ {$ id }" , $ options ));
138149 }
139150
140- return WooCommerce:: delete ("{$ this ->endpoint }/ {$ id }" , $ options );
151+ return $ this -> wooCommerCeInstance ()-> delete ("{$ this ->endpoint }/ {$ id }" , $ options );
141152 }
142153
143154 /**
@@ -150,32 +161,32 @@ protected function delete($id, $options = [])
150161 protected function batch ($ data )
151162 {
152163 if ($ this ->isLazyCollection ) {
153- return LazyCollection::make (WooCommerce:: create ("{$ this ->endpoint }/batch " , $ data ));
164+ return LazyCollection::make ( $ this -> wooCommerCeInstance ()-> create ("{$ this ->endpoint }/batch " , $ data ));
154165 }
155166
156167 if ($ this ->isCollection ) {
157- return collect (WooCommerce:: create ("{$ this ->endpoint }/batch " , $ data ));
168+ return collect ( $ this -> wooCommerCeInstance ()-> create ("{$ this ->endpoint }/batch " , $ data ));
158169 }
159170
160- return WooCommerce:: create ("{$ this ->endpoint }/batch " , $ data );
171+ return $ this -> wooCommerCeInstance ()-> create ("{$ this ->endpoint }/batch " , $ data );
161172 }
162173
163174 /**
164175 * Retrieve data.
165176 *
166- * @return array
177+ * @return \Illuminate\Support\Collection| array
167178 */
168179 protected function get ()
169180 {
170181 if ($ this ->isLazyCollection ) {
171- return LazyCollection::make (WooCommerce:: all ($ this ->endpoint , $ this ->options ));
182+ return LazyCollection::make ( $ this -> wooCommerCeInstance ()-> all ($ this ->endpoint , $ this ->options ));
172183 }
173184
174185 if ($ this ->isCollection ) {
175- return collect (WooCommerce:: all ($ this ->endpoint , $ this ->options ));
186+ return collect ( $ this -> wooCommerCeInstance ()-> all ($ this ->endpoint , $ this ->options ));
176187 }
177188
178- return WooCommerce:: all ($ this ->endpoint , $ this ->options );
189+ return $ this -> wooCommerCeInstance ()-> all ($ this ->endpoint , $ this ->options );
179190 }
180191
181192 /**
@@ -238,6 +249,13 @@ protected function withLazyCollection()
238249 return $ this ;
239250 }
240251
252+ public function withConfig ($ configSet )
253+ {
254+ $ this ->config = $ configSet ;
255+
256+ return $ this ;
257+ }
258+
241259 /**
242260 * Set options for woocommerce request.
243261 *
@@ -328,11 +346,11 @@ protected function paginate($per_page = 10, $current_page = 1, $options = [])
328346 }
329347
330348 $ data = $ this ->get ();
331- $ totalResults = WooCommerce:: countResults ();
332- $ totalPages = WooCommerce:: countPages ();
333- $ currentPage = WooCommerce:: current ();
334- $ previousPage = WooCommerce:: previous ();
335- $ nextPage = WooCommerce:: next ();
349+ $ totalResults = $ this -> wooCommerCeInstance ()-> countResults ();
350+ $ totalPages = $ this -> wooCommerCeInstance ()-> countPages ();
351+ $ currentPage = $ this -> wooCommerCeInstance ()-> current ();
352+ $ previousPage = $ this -> wooCommerCeInstance ()-> previous ();
353+ $ nextPage = $ this -> wooCommerCeInstance ()-> next ();
336354
337355 $ pagination = [
338356 'total_results ' => $ totalResults ,
@@ -371,8 +389,8 @@ protected function paginate($per_page = 10, $current_page = 1, $options = [])
371389 protected function count ()
372390 {
373391 try {
374- $ results = WooCommerce:: all ($ this ->endpoint , $ this ->options );
375- $ totalResults = WooCommerce:: countResults ();
392+ $ results = $ this -> wooCommerCeInstance ()-> all ($ this ->endpoint , $ this ->options );
393+ $ totalResults = $ this -> wooCommerCeInstance ()-> countResults ();
376394
377395 return $ totalResults ;
378396 } catch (\Exception $ ex ) {
@@ -387,7 +405,7 @@ protected function count()
387405 */
388406 public function save ()
389407 {
390- $ this ->results = WooCommerce:: create ($ this ->endpoint , $ this ->properties );
408+ $ this ->results = $ this -> wooCommerCeInstance ()-> create ($ this ->endpoint , $ this ->properties );
391409
392410 if ($ this ->isLazyCollection ) {
393411 return LazyCollection::make ($ this ->results );
0 commit comments