-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Edit order totals calculation #26
base: main
Are you sure you want to change the base?
Conversation
@Saggre Thanks for the PR. Do you have a guide for recreating the issue you have seen? Perhaps just a screendump from an order would be enough to recreate it locally and verify the solution. |
@bilfeldt Quickest way to recreate the issue is to manually add a negative fee to an order in wp admin panel. This order results in the following request json fields with plugin Version: 8.0.25: "subtotal_price_excluding_tax": 5.58,
"subtotal_price_including_tax": -2.3599999999999996,
"shipping_price_excluding_tax": 6.52,
"shipping_price_including_tax": 8.26,
"total_price_excluding_tax": 4.87,
"total_price_including_tax": 5.9,
"total_tax_amount": 1.03, And this is how the json should look after the fix: "subtotal_price_excluding_tax": 6.61,
"subtotal_price_including_tax": 5.9,
"shipping_price_excluding_tax": 8.26,
"shipping_price_including_tax": 10,
"total_price_excluding_tax": 4.87,
"total_price_including_tax": 5.9,
"total_tax_amount": 1.03, Note that there's actually a mistake in my solution with subtotal_price_including_tax. |
@Saggre Thanks for the PR. After having looked at the template file responsible for the admin page you listed $order->get_subtotal(); // including VAT - without any discounts
$order->get_total_discount(); // including VAT
$order->get_discount_tax();
$order->get_shipping_total(); // including VAT
$order->get_total_fees(); Meaning that the following calculation should work: // Order totals
$order_total_incl_tax = $this->order->get_total();
$order_total_tax = $this->order->get_total_tax();
$order_total_excl_tax = $order_total_incl_tax - $order_total_tax;
// Shipping
$order_shipping_incl_tax = (float) $this->order->get_shipping_total(); // Note returns string for some reason
$order_shipping_tax = (float) $this->order->get_shipping_tax(); // Note returns string for some reason
$order_shipping_excl_tax = $order_shipping_incl_tax - $order_shipping_tax;
// Order subtotals (without shipping but with discount)
$order_subtotal_incl_tax = $this->order->get_subtotal() - $this->get_total_discount();
$order_subtotal_tax = $order_total_tax - $order_shipping_tax - $order->get_discount_tax();
$order_subtotal_excl_tax = $order_subtotal_incl_tax - $order_subtotal_tax; Note that all available methods can be found here: https://woocommerce.github.io/code-reference/classes/WC-Abstract-Order.html |
@Saggre I have now updated the calculation on your PR. Can you confirm if this is working on the test order that you have? |
@bilfeldt Now with the same test order, and a test environment with only WooCommerce and Smart Send, the values look like this: "subtotal_price_excluding_tax": 7.32,
"subtotal_price_including_tax": 6.61,
"shipping_price_excluding_tax": 6.52,
"shipping_price_including_tax": 8.26,
"total_price_excluding_tax": 4.87,
"total_price_including_tax": 5.9,
"total_tax_amount": 1.03,
Here And here With my latest commit, the values look correct when comparing to the order screenshot (Shop VAT is 21%). "subtotal_price_excluding_tax": 6.61,
"subtotal_price_including_tax": 8,
"shipping_price_excluding_tax": 8.26,
"shipping_price_including_tax": 10,
"total_price_excluding_tax": 4.87,
"total_price_including_tax": 5.9,
"total_tax_amount": 1.03, What threw it off in my previous commit was that I included filter The filter could also be implemented in the following format: $extra_line_item_types = apply_filters( 'smart_send_order_custom_line_item_types', array( 'fee' ), $this->order );
$extra_line_items = $this->order->get_items( $extra_line_item_types ); |
@Saggre thanks for the details. I think what is causing the confusion here is the WooCommerce config It seems that
I have not yet found test examples confirming the behaviour of
I had not originally spotted that you have added a "fee" and not a "discount" - of cause our plugin should support both. I have created these two test orders with different config option I guess it will be possible to use the method |
@bilfeldt Calling |
woocommerce/smart-send-logistics/includes/class-ss-shipping-shipment.php
Lines 370 to 383 in 4d754e4
I found that the plugin tries to send a negative subtotal into Smart Send API when there's a discount that is larger than subtotal, but smaller than total price. This is caused by the current subtotal calculation not taking discounts into account resulting in:
$order_shipping > $order_total
$order_subtotal = $order_total - $order_shipping;
$order_subtotal < 0
And this results in the following API error:
This is the new totals calculation. The fix is just to get subtotal directly from WC_Order and throw the calculations around a bit. I invite you to check it thoroughly because it's a sensitive and mistake-prone topic.
get_shipping_total()
andget_subtotal()
in my understanding return a taxless price, andget_total()
returns price including tax.