WC_Discounts::apply_coupon_remainder( WC_Coupon $coupon, array $items_to_apply, int $amount )
Deal with remaining fractional discounts by splitting it over items until the amount is expired, discounting 1 cent at a time.
Description Description
Parameters Parameters
- $coupon
-
(Required) Coupon object if appliable. Passed through filters.
- $items_to_apply
-
(Required) Array of items to apply the coupon to.
- $amount
-
(Required) Fixed discount amount to apply.
Return Return
(int) Total discounted.
Source Source
File: includes/class-wc-discounts.php
protected function apply_coupon_remainder( $coupon, $items_to_apply, $amount ) {
$total_discount = 0;
foreach ( $items_to_apply as $item ) {
for ( $i = 0; $i < $item->quantity; $i ++ ) {
// Find out how much price is available to discount for the item.
$price_to_discount = $this->get_discounted_price_in_cents( $item );
// Run coupon calculations.
$discount = min( $price_to_discount, 1 );
// Store totals.
$total_discount += $discount;
// Store code and discount amount per item.
$this->discounts[ $coupon->get_code() ][ $item->key ] += $discount;
if ( $total_discount >= $amount ) {
break 2;
}
}
if ( $total_discount >= $amount ) {
break;
}
}
return $total_discount;
}
Changelog Changelog
| Version | Description |
|---|---|
| 3.2.0 | Introduced. |