首页 > 解决方案 > Shopify 草稿订单创建生成错误“必须对应于从值计算的错误”

问题描述

我正在 Shopify 上创建带有折扣的草稿订单,它的返回错误如“必须对应于从值计算的值”。

我计算折扣如下:

$amount 是订单的总金额 (78.99),$rate (30) 是折扣百分比的值。

$discount = $amount * ( $rate / 100);
$discount = $discount * pow(10, 2);
$discount = floatval($discount);
$discount = $discount / pow(10, 2);
$new_discount_amt = round($discount, 2);

在这里,我的总数是 78.99,我想申请 30% 的折扣。所以最终的折扣金额是 23.7

$applied_discount = array(
                "title" => "RCT Reorder Discount",
                "description" => "Description",
                "value" => "30",
                "value_type" => "percentage",
                "amount" => $new_discount_amt
            );

Shopify 退货

{"errors":{"applied_discount.amount":["must correspond to that calculated from the value"]}}

这个计算有什么问题?在 Shopify 中计算折扣的正确方法是什么?

标签: shopify

解决方案


看起来大致还可以。请记住,如果您使用基于十进制的货币,金额以美分为单位。

以下在 node.js 应用程序中的生产工作:

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
    }
};

推荐阅读