WC_Discounts::apply_coupon_fixed_product( WC_Coupon $coupon, array $items_to_apply, int $amount = null )
Apply fixed product discount to items.
Description Description
Parameters Parameters
- $coupon
-
(Required) Coupon object. Passed through filters.
- $items_to_apply
-
(Required) Array of items to apply the coupon to.
- $amount
-
(Optional) Fixed discount amount to apply in cents. Leave blank to pull from coupon.
Default value: null
Return Return
(int) Total discounted.
Source Source
File: includes/class-wc-discounts.php
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;
}
Changelog Changelog
| Version | Description |
|---|---|
| 3.2.0 | Introduced. |