WC_Discounts::apply_coupon_remainder
Deal with remaining fractional discounts by splitting it over items until the amount is expired, discounting 1 cent at a time.
Method of the class: WC_Discounts{}
No Hooks.
Returns
Int. Total discounted.
Usage
// protected - for code of main (parent) or child class $result = $this->apply_coupon_remainder( $coupon, $items_to_apply, $amount );
- $coupon(WC_Coupon) (required)
- Coupon object if applicable. Passed through filters.
- $items_to_apply(array) (required)
- Array of items to apply the coupon to.
- $amount(int) (required)
- Fixed discount amount to apply.
Changelog
| Since 3.2.0 | Introduced. |
WC_Discounts::apply_coupon_remainder() WC Discounts::apply coupon remainder code WC 10.7.0
protected function apply_coupon_remainder( $coupon, $items_to_apply, $amount ) {
$total_discount = 0;
foreach ( $items_to_apply as $item ) {
$quantity = NumberUtil::ceil( $item->quantity );
for ( $i = 0; $i < $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;
}