WC_Abstract_Order::apply_coupon_using_edited_totals │ public │ WC 11.2.0

Apply a coupon treating manually edited line item totals as the pre-discount price.

When the order has no coupons yet, line items whose total differs from their subtotal adopt that total as the new subtotal, so the discount is calculated from the edited price and recalculations keep the manual adjustment. On failure the original subtotals are restored. Call this only where a subtotal/total difference is meant to be treated as a manual edit, such as the admin order editor; otherwise use apply_coupon().

Method of the class: WC_Abstract_Order{}

No Hooks.

Returns

true|WP_Error. True if applied, error if not.

Usage

$WC_Abstract_Order = new WC_Abstract_Order();
$WC_Abstract_Order->apply_coupon_using_edited_totals( $raw_coupon );
$raw_coupon(string|WC_Coupon) (required)
Coupon code or object.

Changelog

Since 11.2.0 Introduced.

WC_Abstract_Order::apply_coupon_using_edited_totals() code WC 11.3.0

public function apply_coupon_using_edited_totals( $raw_coupon ) {
	// With coupons already applied the subtotal/total difference also contains their
	// discounts and the manual portion cannot be separated out, so it is left alone.
	$original_subtotals = empty( $this->get_items( 'coupon' ) ) ? $this->sync_subtotals_with_manually_edited_totals() : array();

	$applied = $this->apply_coupon( $raw_coupon );

	if ( is_wp_error( $applied ) ) {
		$this->restore_item_subtotals( $original_subtotals );
	}

	return $applied;
}