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

Commit 8878491

Browse files
authored
Fixed manual discounts on pay for order flow (#837)
1 parent d48fb25 commit 8878491

File tree

2 files changed

+12
-21
lines changed

2 files changed

+12
-21
lines changed

changelog.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
*** WooCommerce Subscriptions Core Changelog ***
22

3+
= 8.3.0 - 2025-xx-xx =
4+
* Fix - Manual discounts on pay for order flow
5+
6+
37
= 8.2.0 - 2025-04-14 =
48
* Update - Increase the number of args accepted by wcs_get_subscriptions(), to bring about parity with wc_get_orders().
59
* Dev - Update wcs_maybe_prefix_key() and wcs_maybe_unprefix_key() to support an array of keys.

includes/class-wcs-cart-renewal.php

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,26 +1407,15 @@ 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-
14131410
// Get all coupon line items as coupon objects.
14141411
if ( ! empty( $coupon_items ) ) {
14151412
$coupons = $this->get_line_item_coupons( $coupon_items );
14161413
}
14171414

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 );
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 coupon for the difference.
1418+
$coupons[] = $this->create_manual_discount_coupon( $order_discount - $total_coupon_discount );
14301419
}
14311420

14321421
foreach ( $coupons as $coupon ) {
@@ -1453,7 +1442,7 @@ protected function get_line_item_coupons( $coupon_line_items ) {
14531442
continue;
14541443
}
14551444

1456-
$coupon = $this->get_pseudo_coupon( $coupon_item->get_discount() );
1445+
$coupon = $this->create_manual_discount_coupon( $coupon_item->get_discount() );
14571446
$coupon->set_code( $coupon_item->get_code() );
14581447
} elseif ( 'subscription_renewal' === $this->cart_item_key ) {
14591448
$coupon_type = $coupon->get_discount_type();
@@ -1471,12 +1460,12 @@ protected function get_line_item_coupons( $coupon_line_items ) {
14711460
}
14721461

14731462
/**
1474-
* Apply a pseudo coupon to the cart for a specific discount amount.
1463+
* Apply a coupon to the cart for a specific discount amount.
14751464
*
14761465
* @param float $discount The discount amount.
14771466
* @return WC_Coupon
14781467
*/
1479-
protected function get_pseudo_coupon( $discount ) {
1468+
protected function create_manual_discount_coupon( $discount ) {
14801469
$cart_types = array(
14811470
'subscription_initial_payment' => 'initial',
14821471
'subscription_renewal' => 'renewal',
@@ -1486,9 +1475,7 @@ protected function get_pseudo_coupon( $discount ) {
14861475

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

14941481
return $coupon;

0 commit comments

Comments
 (0)