WC_Abstract_Order::set_item_discount_amounts()protectedWC 3.2.0

After applying coupons via the WC_Discounts class, update line items.

Method of the class: WC_Abstract_Order{}

No Hooks.

Return

null. Nothing (null).

Usage

// protected - for code of main (parent) or child class
$result = $this->set_item_discount_amounts( $discounts );
$discounts(WC_Discounts) (required)
Discounts class.

Changelog

Since 3.2.0 Introduced.

WC_Abstract_Order::set_item_discount_amounts() code WC 8.7.0

protected function set_item_discount_amounts( $discounts ) {
	$item_discounts = $discounts->get_discounts_by_item();
	$tax_location   = $this->get_tax_location();
	$tax_location   = array( $tax_location['country'], $tax_location['state'], $tax_location['postcode'], $tax_location['city'] );

	if ( $item_discounts ) {
		foreach ( $item_discounts as $item_id => $amount ) {
			$item = $this->get_item( $item_id, false );

			// If the prices include tax, discounts should be taken off the tax inclusive prices like in the cart.
			if ( $this->get_prices_include_tax() && wc_tax_enabled() && 'taxable' === $item->get_tax_status() ) {
				$taxes = WC_Tax::calc_tax( $amount, $this->get_tax_rates( $item->get_tax_class(), $tax_location ), true );

				// Use unrounded taxes so totals will be re-calculated accurately, like in cart.
				$amount = $amount - array_sum( $taxes );
			}

			$item->set_total( max( 0, $item->get_total() - $amount ) );
		}
	}
}