$shop = ShopifyApp::shop();
if( $shopSettings->discount_id == '' || $shopSettings->discount_id == null || $shopSettings->discount_id == undefined ) {
$price_rule = $shop->api()->rest(
'POST',
'/admin/price_rules.json',
[
"price_rule" => [
"title" => "SUMMERSALE10OFF",
"target_type" => "line_item",
"target_selection" => "all",
"allocation_method" => "across",
"value_type" => "percentage",
"value" => "-"+request('discount_percentage'),
"customer_selection" => "all",
"starts_at" => "2017-01-19T17:59:10Z"
]
]
);
$shopSettings->price_rule_id = $price_rule->body->price_rule->id;
$discount_code = $shop->api()->rest(
'POST',
'/admin/price_rules/'.$price_rule->body->price_rule->id.'/discount_codes.json',
[
"discount_code" => [
"code" => $price_rule->body->price_rule->title
]
]
);
$shopSettings->discount_id = $discount_code->body->discount_code->id;
} else if( request('discount_percentage') && request('discount_type') == "percentage" ) {
$price_rule = $shop->api()->rest(
'POST',
'/admin/price_rules/'.$shopSettings->price_rule_id.'.json',
[
"price_rule" => [
"value" => "-"+request('discount_percentage')
]
]
);
}