So I realized that in using the WP REST API to create orders, the discount_total
parameter shows the discount amount (which is probably calculated by subtracting the line_items['total']
value from the total cost of the items the customer purchased).
I am not entirely sure if this is true though as it’s just a temporary solution I came up with to understand how WOOCOMMERCE does the discount calculation.
So in my code snippet below, I mocked an order request via the WP API with an externally calculated discount amount after which I passed the new $totalPrice
value to the API so that it’ll reflect the discount on the Admin Dashboard. The code WORKED and the discount was reflected on the dashboard
//code snippet
$order_data = [
'payment_method' => 'bacs',
'payment_method_title' => 'Checkout',
'customer_id' => intval($customer_id),
'transaction_id' => (string)$transaction_id,
'set_paid' => true,
'status' => 'completed',
'billing' => $billing_data,
'shipping' => $shipping_data,
'meta_data' => array(
array(
'key' => 'created by',
'value' => 'api request
)
),
'line_items' => array(
array(
'product_id' => intval($product_id),
'quantity' => intval($quantityToBuy),
'total' => (string)$totalPrice,
)
),
'shipping_lines' => array(
array(
'method_id' => 'free_shipping',
'method_title' => 'Free Shipping',
'total' => '0.00'
)
),
];
However when I repeated the same process for orders that included COUPONS, only the coupon discount was reflected.
$order_data = [
'payment_method' => 'bacs',
'payment_method_title' => 'Checkout',
'customer_id' => intval($customer_id),
'transaction_id' => (string)$transaction_id,
'set_paid' => true,
'status' => 'completed',
'billing' => $billing_data,
'shipping' => $shipping_data,
'meta_data' => array(
array(
'key' => 'created by',
'value' => 'api request'
)
),
'line_items' => array(
array(
'product_id' => intval($product_id),
'quantity' => intval($quantityToBuy),
'total' => (string)$totalPrice,
)
),
'shipping_lines' => array(
array(
'method_id' => 'free_shipping',
'method_title' => 'Free Shipping',
'total' => '0.00'
)
),
'coupon_lines' => array(
array(
'code' => (string)$coupon_code
)
)
];
So in cases where a customer uses a coupon in addition to some other discount, the total amount to be paid by the user would be different (lesser) than the amount reflected on the dashboard.
How do I resolve this issue, I am open to any suggestions.
Thank you