Skip to content

Commit 7f2c176

Browse files
authored
Add price rule service (robwittman#24)
* Add price rule service functions * Update PriceRuleFields * Fix typo in DiscountCodeService
1 parent e471e9a commit 7f2c176

File tree

4 files changed

+90
-28
lines changed

4 files changed

+90
-28
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ clover.xml
1616
.auth
1717

1818
test.php
19+
20+
.idea

lib/Enum/Fields/PriceRuleFields.php

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,48 +4,62 @@
44

55
class PriceRuleFields extends AbstractObjectEnum
66
{
7+
const ALLOCATION_METHOD = 'allocation_method';
78
const CREATED_AT = 'created_at';
9+
const CUSTOMER_SELECTION = 'customer_selection';
10+
const ENDS_AT = 'ends_at';
11+
const ENTITLED_COLLECTION_IDS = 'entitled_collection_ids';
12+
const ENTITLED_COUNTRY_IDS = 'entitled_country_ids';
13+
const ENTITLED_PRODUCT_IDS = 'entitled_product_ids';
14+
const ENTITLED_VARIANT_IDS = 'entitled_variant_ids';
815
const ID = 'id';
9-
const TITLE = 'title';
10-
const TARGET_TYPE = 'target_type';
11-
const TARGET_SELECTIN = 'target_selection';
12-
const ALLOCATION_METHOD = 'allocation_method';
13-
const VALUE_TYPE = 'value_type';
14-
const VALUE = 'value';
1516
const ONCE_PER_CUSTOMER = 'once_per_customer';
16-
const USAGE_LIMIT = 'usage_limit';
17-
const CUSTOMER_SELECTION = 'customer_selection';
17+
const PREREQUISITE_CUSTOMER_IDS = 'prerequisite_customer_ids';
18+
const PREREQUISITE_QUANTITY_RANGE = 'prerequisite_quantity_range';
1819
const PREREQUISITE_SAVED_SEARCH_IDS = 'prerequisite_saved_search_ids';
19-
const PREREQUISITE_SUBTOTAL_RANGE = 'prerequisite_subtotal_range';
2020
const PREREQUISITE_SHIPPING_PRICE_RANGE = 'prerequisite_shipping_price_range';
21-
const ENTITLED_PRODUCT_IDS = 'entitled_product_ids';
22-
const ENTITLED_COLLECTION_IDS = 'entitled_collection_ids';
23-
const ENTITLED_COUNTRY_IDS = 'entitled_country_ids';
21+
const PREREQUISITE_SUBTOTAL_RANGE = 'prerequisite_subtotal_range';
2422
const STARTS_AT = 'starts_at';
25-
const ENDS_AT = 'ends_at';
23+
const TARGET_SELECTION = 'target_selection';
24+
const TARGET_TYPE = 'target_type';
25+
const TITLE = 'title';
26+
const USAGE_LIMIT = 'usage_limit';
27+
const PREREQUISITE_PRODUCT_IDS = 'prerequisite_product_ids';
28+
const PREREQUISITE_VARIANT_IDS = 'prerequisite_variant_ids';
29+
const PREREQUISITE_COLLECTION_IDS = 'prerequisite_collection_ids';
30+
const VALUE_TYPE = 'value_type';
31+
const VALUE = 'value';
32+
const PREREQUISITE_TO_ENTITLEMENT_QUANTITY_RATIO = 'prerequisite_to_entitlement_quantity_ratio';
2633

2734
public function getFieldTypes()
2835
{
2936
return array(
37+
'allocation_method' => 'string',
3038
'created_at' => 'DateTime',
39+
'customer_selection' => 'string',
40+
'ends_at' => 'DateTime',
41+
'entitled_collection_ids' => 'array',
42+
'entitled_country_ids' => 'array',
43+
'entitled_product_ids' => 'array',
44+
'entitled_variant_ids' => 'array',
3145
'id' => 'integer',
32-
'title' => 'string',
33-
'target_type' => 'string',
34-
'target_selection' => 'string',
35-
'allocation_method' => 'string',
36-
'value_type' => 'string',
37-
'value' => "string",
3846
'once_per_customer' => 'boolean',
39-
'usage_limit' => 'integer',
40-
'customer_selection' => 'string',
47+
'prerequisite_customer_ids' => 'array',
48+
'prerequisite_quantity_range' => 'array',
4149
'prerequisite_saved_search_ids' => 'array',
50+
'prerequisite_shipping_price_range' => 'object',
4251
'prerequisite_subtotal_range' => 'object',
43-
'entitled_product_ids' => 'array',
44-
'entitled_variant_ids' => 'array',
45-
'entitled_collection_ids' => 'array',
46-
'entitled_country_ids' => 'array',
4752
'starts_at' => 'DateTime',
48-
'ends_at' => 'DateTime'
53+
'target_selection' => 'string',
54+
'target_type' => 'string',
55+
'title' => 'string',
56+
'usage_limit' => 'integer',
57+
'prerequisite_product_ids' => 'array',
58+
'prerequisite_variant_ids' => 'array',
59+
'prerequisite_collection_ids' => 'array',
60+
'value' => "string",
61+
'value_type' => 'string',
62+
'prerequisite_to_entitlement_quantity_ratio' => 'object'
4963
);
5064
}
5165
}

lib/Service/DiscountCodeService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class DiscountCodeService extends AbstractService
1717
public function all($priceRuleId, array $params = array())
1818
{
1919
$endpoint = '/admin/price_rules/'.$priceRuleId.'/discount_codes.json';
20-
$request = $this->request($endpoint, 'GET', $params);
20+
$response = $this->request($endpoint, 'GET', $params);
2121
return $this->createCollection(DiscountCode::class, $response['discount_codes']);
2222
}
2323

lib/Service/PriceRuleService.php

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,51 @@
66

77
class PriceRuleService extends AbstractService
88
{
9-
9+
public function all(array $params = array())
10+
{
11+
$endpoint = '/admin/price_rules.json';
12+
$response = $this->request($endpoint, 'GET', $params);
13+
return $this->createCollection(PriceRule::class, $response['price_rules']);
14+
}
15+
16+
public function get($priceRuleId, array $fields = array())
17+
{
18+
$params = array();
19+
if (!empty($fields)) {
20+
$params['fields'] = $fields;
21+
}
22+
$endpoint = '/admin/price_rules/'.$priceRuleId.'.json';
23+
$response = $this->request($endpoint, 'GET', $params);
24+
return $this->createObject(PriceRule::class, $response['price_rule']);
25+
}
26+
27+
public function create(PriceRule &$priceRule)
28+
{
29+
$data = $priceRule->exportData();
30+
$endpoint = '/admin/price_rules.json';
31+
$response = $this->request(
32+
$endpoint,
33+
'POST',
34+
array('price_rule' => $data)
35+
);
36+
$priceRule->setData($response['price_rule']);
37+
}
38+
39+
public function update(PriceRule &$priceRule)
40+
{
41+
$data = $priceRule->exportData();
42+
$endpoint = '/admin/price_rules/'.$priceRule->id.'.json';
43+
$response = $this->request(
44+
$endpoint, 'PUT', array(
45+
'price_rule' => $data
46+
)
47+
);
48+
$priceRule->setData($response['price_rule']);
49+
}
50+
51+
public function delete(PriceRule $priceRule)
52+
{
53+
$endpoint = '/admin/price_rules/'.$priceRule->id.'.json';
54+
$this->request($endpoint, 'DELETE');
55+
}
1056
}

0 commit comments

Comments
 (0)