Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issues with discounts being applied to manually created parent orders #439

Draft
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions includes/class-wcs-cart-renewal.php
Original file line number Diff line number Diff line change
Expand Up @@ -1350,18 +1350,21 @@ public function maybe_update_shipping_packages( $packages ) {
* @since 1.0.0 - Migrated from WooCommerce Subscriptions v2.4.3
*/
public function setup_discounts( $order ) {
$order_discount = $order->get_total_discount();
$order_discount = $order->get_total_discount() + $order->get_discount_tax();
$coupon_items = $order->get_items( 'coupon' );

if ( empty( $order_discount ) && empty( $coupon_items ) ) {
return;
}

$total_coupon_discount = floatval( array_sum( wc_list_pluck( $coupon_items, 'get_discount' ) ) );
$total_coupon_discount = floatval( array_sum( wc_list_pluck( $coupon_items, 'get_discount' ) ) + array_sum( wc_list_pluck( $coupon_items, 'get_discount_tax' ) ) );
$coupons = array();

// If the order total discount is different from the discount applied from coupons we have a manually applied discount.
$order_has_manual_discount = $order_discount !== $total_coupon_discount;
// If the order total discount is different from the discount applied from coupons then we have a manually applied discount.
$delta_threshold = 1; // Allow for floating point rounding errors.

// Add the rounding number precision (eg convert to cents) to compare the order discount and coupon discount at the same precision.
$order_has_manual_discount = abs( wc_add_number_precision( $order_discount ) - wc_add_number_precision( $total_coupon_discount ) ) > $delta_threshold;

// Get all coupon line items as coupon objects.
if ( ! empty( $coupon_items ) ) {
Expand Down