WC_Discounts::apply_coupon_fixed_product()protectedWC 3.2.0

Apply fixed product discount to items.

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

Возвращает

int. Total discounted.

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

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->apply_coupon_fixed_product( $coupon, $items_to_apply, $amount );
$coupon(WC_Coupon) (обязательный)
Coupon object. Passed through filters.
$items_to_apply(массив) (обязательный)
Array of items to apply the coupon to.
$amount(int)
Fixed discount amount to apply in cents. Leave blank to pull from coupon.
По умолчанию: null

Список изменений

С версии 3.2.0 Введена.

Код WC_Discounts::apply_coupon_fixed_product() WC 8.6.1

protected function apply_coupon_fixed_product( $coupon, $items_to_apply, $amount = null ) {
	$total_discount  = 0;
	$amount          = $amount ? $amount : wc_add_number_precision( $coupon->get_amount() );
	$limit_usage_qty = 0;
	$applied_count   = 0;

	if ( null !== $coupon->get_limit_usage_to_x_items() ) {
		$limit_usage_qty = $coupon->get_limit_usage_to_x_items();
	}

	foreach ( $items_to_apply as $item ) {
		// Find out how much price is available to discount for the item.
		$discounted_price = $this->get_discounted_price_in_cents( $item );

		// Get the price we actually want to discount, based on settings.
		$price_to_discount = ( 'yes' === get_option( 'woocommerce_calc_discounts_sequentially', 'no' ) ) ? $discounted_price : $item->price;

		// Run coupon calculations.
		if ( $limit_usage_qty ) {
			$apply_quantity = $limit_usage_qty - $applied_count < $item->quantity ? $limit_usage_qty - $applied_count : $item->quantity;
			$apply_quantity = max( 0, apply_filters( 'woocommerce_coupon_get_apply_quantity', $apply_quantity, $item, $coupon, $this ) );
			$discount       = min( $amount, $item->price / $item->quantity ) * $apply_quantity;
		} else {
			$apply_quantity = apply_filters( 'woocommerce_coupon_get_apply_quantity', $item->quantity, $item, $coupon, $this );
			$discount       = $amount * $apply_quantity;
		}

		if ( is_a( $this->object, 'WC_Cart' ) && has_filter( 'woocommerce_coupon_get_discount_amount' ) ) {
			// Send through the legacy filter, but not as cents.
			$discount = wc_add_number_precision( apply_filters( 'woocommerce_coupon_get_discount_amount', wc_remove_number_precision( $discount ), wc_remove_number_precision( $price_to_discount ), $item->object, false, $coupon ) );
		}

		$discount       = min( $discounted_price, $discount );
		$total_discount = $total_discount + $discount;
		$applied_count  = $applied_count + $apply_quantity;

		// Store code and discount amount per item.
		$this->discounts[ $coupon->get_code() ][ $item->key ] += $discount;
	}
	return $total_discount;
}