WC_Coupon::get_discount_amount()publicWC 1.0

Get discount amount for a cart item.

Метод класса: WC_Coupon{}

Хуки из метода

Возвращает

float. Amount this coupon has discounted.

Использование

$WC_Coupon = new WC_Coupon();
$WC_Coupon->get_discount_amount( $discounting_amount, $cart_item, $single );
$discounting_amount(float) (обязательный)
Amount the coupon is being applied to.
$cart_item(массив|null)
Cart item being discounted if applicable.
По умолчанию: null
$single(true|false)
True if discounting a single qty item, false if its the line.
По умолчанию: false

Код WC_Coupon::get_discount_amount() WC 8.7.0

public function get_discount_amount( $discounting_amount, $cart_item = null, $single = false ) {
	$discount      = 0;
	$cart_item_qty = is_null( $cart_item ) ? 1 : $cart_item['quantity'];

	if ( $this->is_type( array( 'percent' ) ) ) {
		$discount = (float) $this->get_amount() * ( $discounting_amount / 100 );
	} elseif ( $this->is_type( 'fixed_cart' ) && ! is_null( $cart_item ) && WC()->cart->subtotal_ex_tax ) {
		/**
		 * This is the most complex discount - we need to divide the discount between rows based on their price in.
		 * proportion to the subtotal. This is so rows with different tax rates get a fair discount, and so rows.
		 * with no price (free) don't get discounted.
		 *
		 * Get item discount by dividing item cost by subtotal to get a %.
		 *
		 * Uses price inc tax if prices include tax to work around https://github.com/woocommerce/woocommerce/issues/7669 and https://github.com/woocommerce/woocommerce/issues/8074.
		 */
		if ( wc_prices_include_tax() ) {
			$discount_percent = ( wc_get_price_including_tax( $cart_item['data'] ) * $cart_item_qty ) / WC()->cart->subtotal;
		} else {
			$discount_percent = ( wc_get_price_excluding_tax( $cart_item['data'] ) * $cart_item_qty ) / WC()->cart->subtotal_ex_tax;
		}
		$discount = ( (float) $this->get_amount() * $discount_percent ) / $cart_item_qty;

	} elseif ( $this->is_type( 'fixed_product' ) ) {
		$discount = min( $this->get_amount(), $discounting_amount );
		$discount = $single ? $discount : $discount * $cart_item_qty;
	}

	return apply_filters(
		'woocommerce_coupon_get_discount_amount',
		NumberUtil::round( min( $discount, $discounting_amount ), wc_get_rounding_precision() ),
		$discounting_amount,
		$cart_item,
		$single,
		$this
	);
}