diff --git a/includes/class-wc-discounts.php b/includes/class-wc-discounts.php index b0dd98b0c41..673d9748c41 100644 --- a/includes/class-wc-discounts.php +++ b/includes/class-wc-discounts.php @@ -271,19 +271,7 @@ class WC_Discounts { $this->apply_coupon_fixed_cart( $coupon, $items_to_apply ); break; default: - foreach ( $items_to_apply as $item ) { - $discounted_price = $this->get_discounted_price_in_cents( $item ); - $price_to_discount = wc_remove_number_precision( ( 'yes' === get_option( 'woocommerce_calc_discounts_sequentially', 'no' ) ) ? $discounted_price : $item->price ); - $discount = wc_add_number_precision( $coupon->get_discount_amount( $price_to_discount / $item->quantity, $item->object, true ) ) * $item->quantity; - $discount = min( $discounted_price, $discount ); - - // Store code and discount amount per item. - $this->discounts[ $coupon->get_code() ][ $item->key ] += $discount; - } - - // Allow post-processing for custom coupon types (e.g. calculating discrepancy, etc) - $this->discounts[ $coupon->get_code() ] = apply_filters( 'woocommerce_coupon_custom_discounts_array', $this->discounts[ $coupon->get_code() ], $coupon ); - + $this->apply_coupon_custom( $coupon, $items_to_apply ); break; } @@ -502,6 +490,31 @@ class WC_Discounts { return $total_discount; } + /** + * Apply custom coupon discount to items. + * + * @since 3.3 + * @param WC_Coupon $coupon Coupon object. Passed through filters. + * @param array $items_to_apply Array of items to apply the coupon to. + * @return int Total discounted. + */ + protected function apply_coupon_custom( $coupon, $items_to_apply ) { + foreach ( $items_to_apply as $item ) { + $discounted_price = $this->get_discounted_price_in_cents( $item ); + $price_to_discount = wc_remove_number_precision( ( 'yes' === get_option( 'woocommerce_calc_discounts_sequentially', 'no' ) ) ? $discounted_price : $item->price ); + $discount = wc_add_number_precision( $coupon->get_discount_amount( $price_to_discount / $item->quantity, $item->object, true ) ) * $item->quantity; + $discount = min( $discounted_price, $discount ); + + // Store code and discount amount per item. + $this->discounts[ $coupon->get_code() ][ $item->key ] += $discount; + } + + // Allow post-processing for custom coupon types (e.g. calculating discrepancy, etc) + $this->discounts[ $coupon->get_code() ] = apply_filters( 'woocommerce_coupon_custom_discounts_array', $this->discounts[ $coupon->get_code() ], $coupon ); + + return array_sum( $this->discounts[ $coupon->get_code() ] ); + } + /** * Deal with remaining fractional discounts by splitting it over items * until the amount is expired, discounting 1 cent at a time.