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