WC_Abstract_Order::sync_subtotals_with_manually_edited_totals │ private │ WC 1.0

Sync the subtotal of line items whose total was manually edited, adopting the edited total as the new pre-discount price that discounts are calculated from.

Only called when the order has no coupons applied, since applied coupons make the subtotal/total difference ambiguous (coupon discount vs manual adjustment).

Method of the class: WC_Abstract_Order{}

No Hooks.

Returns

array. Original tax and subtotal values of the changed items, keyed by item ID.

Usage

// private - for code of main (parent) class only
$result = $this->sync_subtotals_with_manually_edited_totals();

WC_Abstract_Order::sync_subtotals_with_manually_edited_totals() code WC 11.2.1

private function sync_subtotals_with_manually_edited_totals() {
	$original_subtotals = array();

	foreach ( $this->get_items() as $item_id => $item ) {
		if ( ! $item instanceof WC_Order_Item_Product ) {
			continue;
		}

		if ( (float) $item->get_subtotal( 'edit' ) === (float) $item->get_total( 'edit' ) && (float) $item->get_subtotal_tax( 'edit' ) === (float) $item->get_total_tax( 'edit' ) ) {
			continue;
		}

		$taxes = $item->get_taxes( 'edit' );

		$original_subtotals[ $item_id ] = array(
			'subtotal'     => $item->get_subtotal( 'edit' ),
			'subtotal_tax' => $item->get_subtotal_tax( 'edit' ),
			'total_tax'    => $item->get_total_tax( 'edit' ),
			'taxes'        => $taxes,
		);

		$item->set_subtotal( $item->get_total( 'edit' ) );

		// set_taxes() keeps the per-rate tax array and the subtotal_tax/total_tax totals in
		// sync, so consumers see the same value whichever one they read. It is skipped when
		// there is no per-rate data, since it would then zero out the stored tax totals.
		if ( ! empty( $taxes['total'] ) ) {
			$taxes['subtotal'] = $taxes['total'];
			$item->set_taxes( $taxes );
		} else {
			$item->set_subtotal_tax( $item->get_total_tax( 'edit' ) );
		}
	}

	return $original_subtotals;
}