WordPress как на ладони
Недорогой хостинг для сайтов на WordPress: wordpress.jino.ru
функция не описана

WC_Discounts::apply_coupon_percent() protected WC 3.2.0

Apply percent discount to items and return an array of discounts granted.

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

Возвращает

int. Total discounted.

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

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->apply_coupon_percent( $coupon, $items_to_apply );
$coupon(WC_Coupon) (обязательный)
Coupon object. Passed through filters.
$items_to_apply(массив) (обязательный)
Array of items to apply the coupon to.

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

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

Код WC_Discounts::apply_coupon_percent() WC 5.6.0

protected function apply_coupon_percent( $coupon, $items_to_apply ) {
	$total_discount        = 0;
	$cart_total            = 0;
	$limit_usage_qty       = 0;
	$applied_count         = 0;
	$adjust_final_discount = true;

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

	$coupon_amount = $coupon->get_amount();

	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 : NumberUtil::round( $item->price );

		// See how many and what price to apply to.
		$apply_quantity    = $limit_usage_qty && ( $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 ) );
		$price_to_discount = ( $price_to_discount / $item->quantity ) * $apply_quantity;

		// Run coupon calculations.
		$discount = floor( $price_to_discount * ( $coupon_amount / 100 ) );

		if ( is_a( $this->object, 'WC_Cart' ) && has_filter( 'woocommerce_coupon_get_discount_amount' ) ) {
			// Send through the legacy filter, but not as cents.
			$filtered_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 ) );

			if ( $filtered_discount !== $discount ) {
				$discount              = $filtered_discount;
				$adjust_final_discount = false;
			}
		}

		$discount       = wc_round_discount( min( $discounted_price, $discount ), 0 );
		$cart_total     = $cart_total + $price_to_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;
	}

	// Work out how much discount would have been given to the cart as a whole and compare to what was discounted on all line items.
	$cart_total_discount = wc_round_discount( $cart_total * ( $coupon_amount / 100 ), 0 );

	if ( $total_discount < $cart_total_discount && $adjust_final_discount ) {
		$total_discount += $this->apply_coupon_remainder( $coupon, $items_to_apply, $cart_total_discount - $total_discount );
	}

	return $total_discount;
}