Skip to content

Commit

Permalink
Add price rule service (#24)
Browse files Browse the repository at this point in the history
* Add price rule service functions

* Update PriceRuleFields

* Fix typo in DiscountCodeService
  • Loading branch information
robwittman authored Oct 2, 2018
1 parent e471e9a commit 7f2c176
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 28 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ clover.xml
.auth

test.php

.idea
66 changes: 40 additions & 26 deletions lib/Enum/Fields/PriceRuleFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,62 @@

class PriceRuleFields extends AbstractObjectEnum
{
const ALLOCATION_METHOD = 'allocation_method';
const CREATED_AT = 'created_at';
const CUSTOMER_SELECTION = 'customer_selection';
const ENDS_AT = 'ends_at';
const ENTITLED_COLLECTION_IDS = 'entitled_collection_ids';
const ENTITLED_COUNTRY_IDS = 'entitled_country_ids';
const ENTITLED_PRODUCT_IDS = 'entitled_product_ids';
const ENTITLED_VARIANT_IDS = 'entitled_variant_ids';
const ID = 'id';
const TITLE = 'title';
const TARGET_TYPE = 'target_type';
const TARGET_SELECTIN = 'target_selection';
const ALLOCATION_METHOD = 'allocation_method';
const VALUE_TYPE = 'value_type';
const VALUE = 'value';
const ONCE_PER_CUSTOMER = 'once_per_customer';
const USAGE_LIMIT = 'usage_limit';
const CUSTOMER_SELECTION = 'customer_selection';
const PREREQUISITE_CUSTOMER_IDS = 'prerequisite_customer_ids';
const PREREQUISITE_QUANTITY_RANGE = 'prerequisite_quantity_range';
const PREREQUISITE_SAVED_SEARCH_IDS = 'prerequisite_saved_search_ids';
const PREREQUISITE_SUBTOTAL_RANGE = 'prerequisite_subtotal_range';
const PREREQUISITE_SHIPPING_PRICE_RANGE = 'prerequisite_shipping_price_range';
const ENTITLED_PRODUCT_IDS = 'entitled_product_ids';
const ENTITLED_COLLECTION_IDS = 'entitled_collection_ids';
const ENTITLED_COUNTRY_IDS = 'entitled_country_ids';
const PREREQUISITE_SUBTOTAL_RANGE = 'prerequisite_subtotal_range';
const STARTS_AT = 'starts_at';
const ENDS_AT = 'ends_at';
const TARGET_SELECTION = 'target_selection';
const TARGET_TYPE = 'target_type';
const TITLE = 'title';
const USAGE_LIMIT = 'usage_limit';
const PREREQUISITE_PRODUCT_IDS = 'prerequisite_product_ids';
const PREREQUISITE_VARIANT_IDS = 'prerequisite_variant_ids';
const PREREQUISITE_COLLECTION_IDS = 'prerequisite_collection_ids';
const VALUE_TYPE = 'value_type';
const VALUE = 'value';
const PREREQUISITE_TO_ENTITLEMENT_QUANTITY_RATIO = 'prerequisite_to_entitlement_quantity_ratio';

public function getFieldTypes()
{
return array(
'allocation_method' => 'string',
'created_at' => 'DateTime',
'customer_selection' => 'string',
'ends_at' => 'DateTime',
'entitled_collection_ids' => 'array',
'entitled_country_ids' => 'array',
'entitled_product_ids' => 'array',
'entitled_variant_ids' => 'array',
'id' => 'integer',
'title' => 'string',
'target_type' => 'string',
'target_selection' => 'string',
'allocation_method' => 'string',
'value_type' => 'string',
'value' => "string",
'once_per_customer' => 'boolean',
'usage_limit' => 'integer',
'customer_selection' => 'string',
'prerequisite_customer_ids' => 'array',
'prerequisite_quantity_range' => 'array',
'prerequisite_saved_search_ids' => 'array',
'prerequisite_shipping_price_range' => 'object',
'prerequisite_subtotal_range' => 'object',
'entitled_product_ids' => 'array',
'entitled_variant_ids' => 'array',
'entitled_collection_ids' => 'array',
'entitled_country_ids' => 'array',
'starts_at' => 'DateTime',
'ends_at' => 'DateTime'
'target_selection' => 'string',
'target_type' => 'string',
'title' => 'string',
'usage_limit' => 'integer',
'prerequisite_product_ids' => 'array',
'prerequisite_variant_ids' => 'array',
'prerequisite_collection_ids' => 'array',
'value' => "string",
'value_type' => 'string',
'prerequisite_to_entitlement_quantity_ratio' => 'object'
);
}
}
2 changes: 1 addition & 1 deletion lib/Service/DiscountCodeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class DiscountCodeService extends AbstractService
public function all($priceRuleId, array $params = array())
{
$endpoint = '/admin/price_rules/'.$priceRuleId.'/discount_codes.json';
$request = $this->request($endpoint, 'GET', $params);
$response = $this->request($endpoint, 'GET', $params);
return $this->createCollection(DiscountCode::class, $response['discount_codes']);
}

Expand Down
48 changes: 47 additions & 1 deletion lib/Service/PriceRuleService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,51 @@

class PriceRuleService extends AbstractService
{

public function all(array $params = array())
{
$endpoint = '/admin/price_rules.json';
$response = $this->request($endpoint, 'GET', $params);
return $this->createCollection(PriceRule::class, $response['price_rules']);
}

public function get($priceRuleId, array $fields = array())
{
$params = array();
if (!empty($fields)) {
$params['fields'] = $fields;
}
$endpoint = '/admin/price_rules/'.$priceRuleId.'.json';
$response = $this->request($endpoint, 'GET', $params);
return $this->createObject(PriceRule::class, $response['price_rule']);
}

public function create(PriceRule &$priceRule)
{
$data = $priceRule->exportData();
$endpoint = '/admin/price_rules.json';
$response = $this->request(
$endpoint,
'POST',
array('price_rule' => $data)
);
$priceRule->setData($response['price_rule']);
}

public function update(PriceRule &$priceRule)
{
$data = $priceRule->exportData();
$endpoint = '/admin/price_rules/'.$priceRule->id.'.json';
$response = $this->request(
$endpoint, 'PUT', array(
'price_rule' => $data
)
);
$priceRule->setData($response['price_rule']);
}

public function delete(PriceRule $priceRule)
{
$endpoint = '/admin/price_rules/'.$priceRule->id.'.json';
$this->request($endpoint, 'DELETE');
}
}

0 comments on commit 7f2c176

Please sign in to comment.