Skip to content
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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 27 additions & 19 deletions smart-send-logistics/includes/class-ss-shipping-shipment.php
Original file line number Diff line number Diff line change
Expand Up @@ -367,20 +367,28 @@ protected function make_single_shipment_api_payload( $return )
*/
$order_note = apply_filters('smart_send_order_note', $order_note, $this->order);

$extra_line_items = $this->order->get_fees();
apply_filters( 'smart_send_order_custom_line_items', $extra_line_items, $this->order );
$extra_line_item_tax = 0.0;

foreach ( $extra_line_items as $line_item ) {
$extra_line_item_tax += (float) $line_item->get_total_tax();
}

// Order totals
$order_total = $this->order->get_total();
$order_total_tax = $this->order->get_total_tax();
$order_total_excl = $order_total - $order_total_tax;
$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 totals
$order_shipping = $this->order->get_shipping_total();
$order_shipping_tax = $this->order->get_shipping_tax();
$order_shipping_excl = $order_shipping - $order_shipping_tax;
// Shipping
$order_shipping_excl_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_incl_tax = $order_shipping_excl_tax + $order_shipping_tax;

// Order totals without shipping
$order_subtotal = $order_total - $order_shipping;
$order_subtotal_tax = $order_total_tax - $order_shipping_tax;
$order_subtotal_excl = $order_total_excl - $order_subtotal_tax;
// Order subtotals (without shipping but with discount)
$order_subtotal_tax = $order_total_tax - $order_shipping_tax - $this->order->get_discount_tax() - $extra_line_item_tax;
$order_subtotal_incl_tax = $this->order->get_subtotal() + $order_subtotal_tax;
$order_subtotal_excl_tax = $this->order->get_subtotal();

$parcels = array();
if (!empty($ss_args['ss_parcels'])) {
Expand Down Expand Up @@ -447,8 +455,8 @@ protected function make_single_shipment_api_payload( $return )
->setLength(null)
->setFreetext($order_note ?: null)
->setItems($items)// Alternatively add each item using $parcel->addItem(Item $item)
->setTotalPriceExcludingTax($order_subtotal_excl ?: null)
->setTotalPriceIncludingTax($order_subtotal ?: null)
->setTotalPriceExcludingTax($order_subtotal_excl_tax ?: null)
->setTotalPriceIncludingTax($order_subtotal_incl_tax ?: null)
->setTotalTaxAmount($order_subtotal_tax ?: null);
}
}
Expand All @@ -470,12 +478,12 @@ protected function make_single_shipment_api_payload( $return )
->setShippingDate(date('Y-m-d'))
->setParcels($parcels)// Alternatively add each parcel using $this->shipment->addParcel(Parcel $parcel);
->setServices($services)
->setSubtotalPriceExcludingTax($order_subtotal_excl ?: null)
->setSubtotalPriceIncludingTax($order_subtotal ?: null)
->setTotalPriceExcludingTax($order_total_excl ?: null)
->setTotalPriceIncludingTax($order_total ?: null)
->setShippingPriceExcludingTax($order_shipping_excl ?: null)
->setShippingPriceIncludingTax($order_shipping ?: null)
->setSubtotalPriceExcludingTax($order_subtotal_excl_tax ?: null)
->setSubtotalPriceIncludingTax($order_subtotal_incl_tax ?: null)
->setTotalPriceExcludingTax($order_total_excl_tax ?: null)
->setTotalPriceIncludingTax($order_total_incl_tax ?: null)
->setShippingPriceExcludingTax($order_shipping_excl_tax ?: null)
->setShippingPriceIncludingTax($order_shipping_incl_tax ?: null)
->setTotalTaxAmount($order_total_tax ?: null)
->setCurrency($order_currency ?: null);
}
Expand Down