3
3
namespace Codexshaper \WooCommerce \Traits ;
4
4
5
5
use Codexshaper \WooCommerce \Facades \WooCommerce ;
6
+ use Codexshaper \WooCommerce \WooCommerceApi ;
6
7
use Illuminate \Support \LazyCollection ;
7
8
8
9
trait QueryBuilderTrait
@@ -37,6 +38,16 @@ trait QueryBuilderTrait
37
38
*/
38
39
protected $ isOriginal = false ;
39
40
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
+
40
51
/**
41
52
* Retrieve all Items.
42
53
*
@@ -47,14 +58,14 @@ trait QueryBuilderTrait
47
58
protected function all ($ options = [])
48
59
{
49
60
if ($ this ->isLazyCollection ) {
50
- return LazyCollection::make (WooCommerce:: all ($ this ->endpoint , $ options ));
61
+ return LazyCollection::make ( $ this -> wooCommerCeInstance ()-> all ($ this ->endpoint , $ options ));
51
62
}
52
63
53
64
if ($ this ->isCollection ) {
54
- return collect (WooCommerce:: all ($ this ->endpoint , $ options ));
65
+ return collect ( $ this -> wooCommerCeInstance ()-> all ($ this ->endpoint , $ options ));
55
66
}
56
67
57
- return WooCommerce:: all ($ this ->endpoint , $ options );
68
+ return $ this -> wooCommerCeInstance ()-> all ($ this ->endpoint , $ options );
58
69
}
59
70
60
71
/**
@@ -68,14 +79,14 @@ protected function all($options = [])
68
79
protected function find ($ id , $ options = [])
69
80
{
70
81
if ($ this ->isLazyCollection ) {
71
- return LazyCollection::make (WooCommerce:: find ("{$ this ->endpoint }/ {$ id }" , $ options ));
82
+ return LazyCollection::make ( $ this -> wooCommerCeInstance ()-> find ("{$ this ->endpoint }/ {$ id }" , $ options ));
72
83
}
73
84
74
85
if ($ this ->isCollection ) {
75
- return collect (WooCommerce:: find ("{$ this ->endpoint }/ {$ id }" , $ options ));
86
+ return collect ( $ this -> wooCommerCeInstance ()-> find ("{$ this ->endpoint }/ {$ id }" , $ options ));
76
87
}
77
88
78
- return WooCommerce:: find ("{$ this ->endpoint }/ {$ id }" , $ options );
89
+ return $ this -> wooCommerCeInstance ()-> find ("{$ this ->endpoint }/ {$ id }" , $ options );
79
90
}
80
91
81
92
/**
@@ -88,14 +99,14 @@ protected function find($id, $options = [])
88
99
protected function create ($ data )
89
100
{
90
101
if ($ this ->isLazyCollection ) {
91
- return LazyCollection::make (WooCommerce:: create ($ this ->endpoint , $ data ));
102
+ return LazyCollection::make ( $ this -> wooCommerCeInstance ()-> create ($ this ->endpoint , $ data ));
92
103
}
93
104
94
105
if ($ this ->isCollection ) {
95
- return collect (WooCommerce:: create ($ this ->endpoint , $ data ));
106
+ return collect ( $ this -> wooCommerCeInstance ()-> create ($ this ->endpoint , $ data ));
96
107
}
97
108
98
- return WooCommerce:: create ($ this ->endpoint , $ data );
109
+ return $ this -> wooCommerCeInstance ()-> create ($ this ->endpoint , $ data );
99
110
}
100
111
101
112
/**
@@ -109,14 +120,14 @@ protected function create($data)
109
120
protected function update ($ id , $ data )
110
121
{
111
122
if ($ this ->isLazyCollection ) {
112
- return LazyCollection::make (WooCommerce:: update ("{$ this ->endpoint }/ {$ id }" , $ data ));
123
+ return LazyCollection::make ( $ this -> wooCommerCeInstance ()-> update ("{$ this ->endpoint }/ {$ id }" , $ data ));
113
124
}
114
125
115
126
if ($ this ->isCollection ) {
116
- return collect (WooCommerce:: update ("{$ this ->endpoint }/ {$ id }" , $ data ));
127
+ return collect ( $ this -> wooCommerCeInstance ()-> update ("{$ this ->endpoint }/ {$ id }" , $ data ));
117
128
}
118
129
119
- return WooCommerce:: update ("{$ this ->endpoint }/ {$ id }" , $ data );
130
+ return $ this -> wooCommerCeInstance ()-> update ("{$ this ->endpoint }/ {$ id }" , $ data );
120
131
}
121
132
122
133
/**
@@ -130,14 +141,14 @@ protected function update($id, $data)
130
141
protected function delete ($ id , $ options = [])
131
142
{
132
143
if ($ this ->isLazyCollection ) {
133
- return LazyCollection::make (WooCommerce:: delete ("{$ this ->endpoint }/ {$ id }" , $ options ));
144
+ return LazyCollection::make ( $ this -> wooCommerCeInstance ()-> delete ("{$ this ->endpoint }/ {$ id }" , $ options ));
134
145
}
135
146
136
147
if ($ this ->isCollection ) {
137
- return collect (WooCommerce:: delete ("{$ this ->endpoint }/ {$ id }" , $ options ));
148
+ return collect ( $ this -> wooCommerCeInstance ()-> delete ("{$ this ->endpoint }/ {$ id }" , $ options ));
138
149
}
139
150
140
- return WooCommerce:: delete ("{$ this ->endpoint }/ {$ id }" , $ options );
151
+ return $ this -> wooCommerCeInstance ()-> delete ("{$ this ->endpoint }/ {$ id }" , $ options );
141
152
}
142
153
143
154
/**
@@ -150,32 +161,32 @@ protected function delete($id, $options = [])
150
161
protected function batch ($ data )
151
162
{
152
163
if ($ this ->isLazyCollection ) {
153
- return LazyCollection::make (WooCommerce:: create ("{$ this ->endpoint }/batch " , $ data ));
164
+ return LazyCollection::make ( $ this -> wooCommerCeInstance ()-> create ("{$ this ->endpoint }/batch " , $ data ));
154
165
}
155
166
156
167
if ($ this ->isCollection ) {
157
- return collect (WooCommerce:: create ("{$ this ->endpoint }/batch " , $ data ));
168
+ return collect ( $ this -> wooCommerCeInstance ()-> create ("{$ this ->endpoint }/batch " , $ data ));
158
169
}
159
170
160
- return WooCommerce:: create ("{$ this ->endpoint }/batch " , $ data );
171
+ return $ this -> wooCommerCeInstance ()-> create ("{$ this ->endpoint }/batch " , $ data );
161
172
}
162
173
163
174
/**
164
175
* Retrieve data.
165
176
*
166
- * @return array
177
+ * @return \Illuminate\Support\Collection| array
167
178
*/
168
179
protected function get ()
169
180
{
170
181
if ($ this ->isLazyCollection ) {
171
- return LazyCollection::make (WooCommerce:: all ($ this ->endpoint , $ this ->options ));
182
+ return LazyCollection::make ( $ this -> wooCommerCeInstance ()-> all ($ this ->endpoint , $ this ->options ));
172
183
}
173
184
174
185
if ($ this ->isCollection ) {
175
- return collect (WooCommerce:: all ($ this ->endpoint , $ this ->options ));
186
+ return collect ( $ this -> wooCommerCeInstance ()-> all ($ this ->endpoint , $ this ->options ));
176
187
}
177
188
178
- return WooCommerce:: all ($ this ->endpoint , $ this ->options );
189
+ return $ this -> wooCommerCeInstance ()-> all ($ this ->endpoint , $ this ->options );
179
190
}
180
191
181
192
/**
@@ -238,6 +249,13 @@ protected function withLazyCollection()
238
249
return $ this ;
239
250
}
240
251
252
+ public function withConfig ($ configSet )
253
+ {
254
+ $ this ->config = $ configSet ;
255
+
256
+ return $ this ;
257
+ }
258
+
241
259
/**
242
260
* Set options for woocommerce request.
243
261
*
@@ -328,11 +346,11 @@ protected function paginate($per_page = 10, $current_page = 1, $options = [])
328
346
}
329
347
330
348
$ 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 ();
336
354
337
355
$ pagination = [
338
356
'total_results ' => $ totalResults ,
@@ -371,8 +389,8 @@ protected function paginate($per_page = 10, $current_page = 1, $options = [])
371
389
protected function count ()
372
390
{
373
391
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 ();
376
394
377
395
return $ totalResults ;
378
396
} catch (\Exception $ ex ) {
@@ -387,7 +405,7 @@ protected function count()
387
405
*/
388
406
public function save ()
389
407
{
390
- $ this ->results = WooCommerce:: create ($ this ->endpoint , $ this ->properties );
408
+ $ this ->results = $ this -> wooCommerCeInstance ()-> create ($ this ->endpoint , $ this ->properties );
391
409
392
410
if ($ this ->isLazyCollection ) {
393
411
return LazyCollection::make ($ this ->results );
0 commit comments