Skip to content
This repository was archived by the owner on May 21, 2025. It is now read-only.

Commit 48c3fd9

Browse files
committed
Revert "Fixed manual discounts on pay for order flow"
This reverts commit 12d7b6a.
1 parent 12d7b6a commit 48c3fd9

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

includes/class-wcs-cart-renewal.php

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,15 +1407,26 @@ public function setup_discounts( $order ) {
14071407
$total_coupon_discount += floatval( array_sum( wc_list_pluck( $coupon_items, 'get_discount_tax' ) ) );
14081408
}
14091409

1410+
// If the order total discount is different from the discount applied from coupons we have a manually applied discount.
1411+
$order_has_manual_discount = $order_discount !== $total_coupon_discount;
1412+
14101413
// Get all coupon line items as coupon objects.
14111414
if ( ! empty( $coupon_items ) ) {
14121415
$coupons = $this->get_line_item_coupons( $coupon_items );
14131416
}
14141417

1415-
// If the order total discount is different from the discount applied from coupons we have a manually applied discount.
1416-
if ( $order_discount !== $total_coupon_discount && $order_discount > $total_coupon_discount ) {
1417-
// If there is a manually applied discount, we need to add a pseudo coupon for the difference.
1418-
$coupons[] = $this->create_manual_discount_coupon( $order_discount - $total_coupon_discount );
1418+
if ( $order_has_manual_discount ) {
1419+
// Remove any coupon line items which don't grant free shipping.
1420+
foreach ( $coupons as $index => $coupon ) {
1421+
if ( ! $coupon->get_free_shipping() ) {
1422+
unset( $coupons[ $index ] );
1423+
}
1424+
1425+
// We're going to apply a coupon for the full order discount so make sure free shipping coupons don't apply any discount.
1426+
$coupon->set_amount( 0 );
1427+
}
1428+
1429+
$coupons[] = $this->get_pseudo_coupon( $order_discount );
14191430
}
14201431

14211432
foreach ( $coupons as $coupon ) {
@@ -1442,7 +1453,7 @@ protected function get_line_item_coupons( $coupon_line_items ) {
14421453
continue;
14431454
}
14441455

1445-
$coupon = $this->create_manual_discount_coupon( $coupon_item->get_discount() );
1456+
$coupon = $this->get_pseudo_coupon( $coupon_item->get_discount() );
14461457
$coupon->set_code( $coupon_item->get_code() );
14471458
} elseif ( 'subscription_renewal' === $this->cart_item_key ) {
14481459
$coupon_type = $coupon->get_discount_type();
@@ -1460,12 +1471,12 @@ protected function get_line_item_coupons( $coupon_line_items ) {
14601471
}
14611472

14621473
/**
1463-
* Apply a coupon to the cart for a specific discount amount.
1474+
* Apply a pseudo coupon to the cart for a specific discount amount.
14641475
*
14651476
* @param float $discount The discount amount.
14661477
* @return WC_Coupon
14671478
*/
1468-
protected function create_manual_discount_coupon( $discount ) {
1479+
protected function get_pseudo_coupon( $discount ) {
14691480
$cart_types = array(
14701481
'subscription_initial_payment' => 'initial',
14711482
'subscription_renewal' => 'renewal',
@@ -1475,7 +1486,9 @@ protected function create_manual_discount_coupon( $discount ) {
14751486

14761487
// Generate a unique coupon code from the cart type.
14771488
$coupon = new WC_Coupon( "discount_{$cart_type}" );
1478-
$coupon->set_discount_type( 'fixed_cart' );
1489+
1490+
// Apply our cart style pseudo coupon type and the set the amount.
1491+
$coupon->set_discount_type( "{$cart_type}_cart" );
14791492
$coupon->set_amount( $discount );
14801493

14811494
return $coupon;

0 commit comments

Comments
 (0)