@@ -1407,26 +1407,15 @@ public function setup_discounts( $order ) {
1407
1407
$ total_coupon_discount += floatval ( array_sum ( wc_list_pluck ( $ coupon_items , 'get_discount_tax ' ) ) );
1408
1408
}
1409
1409
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
-
1413
1410
// Get all coupon line items as coupon objects.
1414
1411
if ( ! empty ( $ coupon_items ) ) {
1415
1412
$ coupons = $ this ->get_line_item_coupons ( $ coupon_items );
1416
1413
}
1417
1414
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 );
1430
1419
}
1431
1420
1432
1421
foreach ( $ coupons as $ coupon ) {
@@ -1453,7 +1442,7 @@ protected function get_line_item_coupons( $coupon_line_items ) {
1453
1442
continue ;
1454
1443
}
1455
1444
1456
- $ coupon = $ this ->get_pseudo_coupon ( $ coupon_item ->get_discount () );
1445
+ $ coupon = $ this ->create_manual_discount_coupon ( $ coupon_item ->get_discount () );
1457
1446
$ coupon ->set_code ( $ coupon_item ->get_code () );
1458
1447
} elseif ( 'subscription_renewal ' === $ this ->cart_item_key ) {
1459
1448
$ coupon_type = $ coupon ->get_discount_type ();
@@ -1471,12 +1460,12 @@ protected function get_line_item_coupons( $coupon_line_items ) {
1471
1460
}
1472
1461
1473
1462
/**
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.
1475
1464
*
1476
1465
* @param float $discount The discount amount.
1477
1466
* @return WC_Coupon
1478
1467
*/
1479
- protected function get_pseudo_coupon ( $ discount ) {
1468
+ protected function create_manual_discount_coupon ( $ discount ) {
1480
1469
$ cart_types = array (
1481
1470
'subscription_initial_payment ' => 'initial ' ,
1482
1471
'subscription_renewal ' => 'renewal ' ,
@@ -1486,9 +1475,7 @@ protected function get_pseudo_coupon( $discount ) {
1486
1475
1487
1476
// Generate a unique coupon code from the cart type.
1488
1477
$ 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 ' );
1492
1479
$ coupon ->set_amount ( $ discount );
1493
1480
1494
1481
return $ coupon ;
0 commit comments