Skip to content

Commit daf793d

Browse files
committed
Fixed endpioint
1 parent 2e468fb commit daf793d

File tree

6 files changed

+82
-9
lines changed

6 files changed

+82
-9
lines changed

src/Models/Customer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ class Customer
99
{
1010
use QueryBuilderTrait;
1111

12+
protected $endpoint = 'customers';
13+
1214
public function all($options = [])
1315
{
1416
return WooCommerce::all('customers', $options);

src/Models/Order.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ class Order
99
{
1010
use QueryBuilderTrait;
1111

12+
protected $endpoint = 'orders';
13+
1214
public function all($options = [])
1315
{
1416
return WooCommerce::all('orders', $options);

src/Models/Product.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,40 @@ class Product
99
{
1010
use QueryBuilderTrait;
1111

12+
protected $endpoint = 'products';
13+
14+
/**
15+
* Retrieve all products
16+
*
17+
* @param array $options
18+
*
19+
* @return array
20+
*/
1221
public function all($options = [])
1322
{
1423
return WooCommerce::all('products', $options);
1524
}
1625

26+
/**
27+
* Retrieve single product
28+
*
29+
* @param integer $id
30+
* @param array $options
31+
*
32+
* @return object
33+
*/
1734
public function find($id, $options = [])
1835
{
1936
return collect(WooCommerce::find("products/{$id}", $options));
2037
}
2138

39+
/**
40+
* Create new product
41+
*
42+
* @param array $data
43+
*
44+
* @return object
45+
*/
2246
public function create($data)
2347
{
2448
return WooCommerce::create('products', $data);

src/Traits/QueryBuilderTrait.php

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,39 @@
66

77
trait QueryBuilderTrait
88
{
9-
10-
protected $options;
11-
12-
/* Custom Query */
13-
9+
/**
10+
* @var $options
11+
*/
12+
protected $options = [];
13+
14+
/**
15+
* Retrieve data
16+
*
17+
* @return array
18+
*/
1419
public function get()
1520
{
16-
return WooCommerce::all('products', $this->options);
21+
return WooCommerce::all($this->endpoint, $this->options);
1722
}
1823

24+
/**
25+
* Retrieve data
26+
*
27+
* @return object
28+
*/
1929
public function first()
2030
{
2131
return collect($this->get()[0]);
2232
}
2333

24-
public function options(array $parameters)
34+
/**
35+
* Set options for woocommerce request
36+
*
37+
* @param array $parameters
38+
*
39+
* @return object $this
40+
*/
41+
public function options($parameters)
2542
{
2643
if (!is_array($parameters)) {
2744
throw new \Exception("Options must be an array", 1);
@@ -39,16 +56,21 @@ public function options(array $parameters)
3956
return $this;
4057
}
4158

59+
/**
60+
* Join options for woocommerce request
61+
*
62+
* @param array $parameters
63+
*
64+
* @return object $this
65+
*/
4266
public function where(...$parameters)
4367
{
4468
if (count($parameters) == 2) {
4569
$this->options[$parameters[0]] = $parameters[1];
4670
}
4771

4872
if (count($parameters) == 1) {
49-
5073
foreach ($parameters as $parameter) {
51-
5274
foreach ($parameter as $key => $value) {
5375
$this->options[$key] = $value;
5476
}
@@ -57,6 +79,13 @@ public function where(...$parameters)
5779
return $this;
5880
}
5981

82+
/**
83+
*
84+
* @param string $name
85+
* @param string $direction
86+
*
87+
* @return object $this
88+
*/
6089
public function orderBy($name, $direction = 'desc')
6190
{
6291
$this->options['orderby'] = $name;

src/Traits/WoocommerceTrait.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public function all($endpoint = '', $options = [])
1717
{
1818
return $this->client->get($endpoint, $options);
1919
}
20+
2021
/**
2122
* GET method.
2223
* Retrieve Single data
@@ -30,6 +31,7 @@ public function find($endpoint = '', $options = [])
3031
{
3132
return $this->client->get($endpoint, $options);
3233
}
34+
3335
/**
3436
* POST method.
3537
* Insert data
@@ -43,6 +45,7 @@ public function create($endpoint, $data)
4345
{
4446
return $this->client->post($endpoint, $data);
4547
}
48+
4649
/**
4750
* PUT method.
4851
* Update data
@@ -56,6 +59,7 @@ public function update($endpoint, $data)
5659
{
5760
return $this->client->put($endpoint, $data);
5861
}
62+
5963
/**
6064
* DELETE method.
6165
* Remove data
@@ -69,6 +73,7 @@ public function delete($endpoint, $options = [])
6973
{
7074
return $this->client->delete($endpoint, $options);
7175
}
76+
7277
/**
7378
* Return the last request header
7479
*
@@ -78,6 +83,7 @@ public function getRequest()
7883
{
7984
return $this->client->http->getRequest();
8085
}
86+
8187
/**
8288
* Return the http response headers from last request
8389
*
@@ -87,6 +93,7 @@ public function getResponse()
8793
{
8894
return $this->client->http->getResponse();
8995
}
96+
9097
/**
9198
* Return the current page number
9299
*
@@ -96,6 +103,7 @@ public function current()
96103
{
97104
return !empty($this->getRequest()->getParameters()['page']) ? $this->getRequest()->getParameters()['page'] : 1;
98105
}
106+
99107
/**
100108
* Count the total results and return it
101109
*
@@ -105,6 +113,7 @@ public function countResults()
105113
{
106114
return (int) $this->getResponse()->getHeaders()['X-WP-Total'];
107115
}
116+
108117
/**
109118
* Count the total pages and return
110119
*
@@ -114,6 +123,7 @@ public function countPages()
114123
{
115124
return (int) $this->getResponse()->getHeaders()['X-WP-TotalPages'];
116125
}
126+
117127
/**
118128
* Return the previous page number
119129
*
@@ -124,6 +134,7 @@ public function previous()
124134
$currentPage = $this->current();
125135
return (--$currentPage > 1) ? $currentPage : null;
126136
}
137+
127138
/**
128139
* Return the next page number
129140
*

src/WoocommerceApi.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ class WooCommerceApi
1111

1212
protected $client;
1313

14+
/**
15+
* Build Woocommerce connection
16+
*
17+
* @return void
18+
*/
1419
public function __construct()
1520
{
1621
$this->client = new Client(

0 commit comments

Comments
 (0)