Reputation: 591
I am creating a draft order on Shopify with discount, Its return error like 'must correspond to that calculated from the value'.
I am calculating discount as following:
$amount is the total amount (78.99) of order and $rate (30) is a value of discount percentage.
$discount = $amount * ( $rate / 100);
$discount = $discount * pow(10, 2);
$discount = floatval($discount);
$discount = $discount / pow(10, 2);
$new_discount_amt = round($discount, 2);
Here, my total is 78.99 and I want to apply a 30% discount on that. so the final discount amount is 23.7
$applied_discount = array(
"title" => "RCT Reorder Discount",
"description" => "Description",
"value" => "30",
"value_type" => "percentage",
"amount" => $new_discount_amt
);
Shopify return
{"errors":{"applied_discount.amount":["must correspond to that calculated from the value"]}}
What's wrong with this calculation? What is the right method to calculate discount in Shopify?
Upvotes: 3
Views: 457
Reputation: 15377
That looks roughly ok. Remember though that the amount is in cents if you are using a decimal based currency.
the following works in production in a node.js app:
var discount = 0.33;
var qty = parseInt(row.qty,10);
var rate = v.price * discount; //discount amount in cents
var line = {
variant_id: v.id,
quantity:qty,
description: row.description,
applied_discount:{
title:'Wholesale Discount',
value_type:'percentage',
value:(100*discount),
amount: Math.floor(100* qty * rate)/100
}
};
Upvotes: 1