@@ -454,13 +454,30 @@ public function isSubscriptionRequired($order = null): bool {
454
454
}
455
455
return false ;
456
456
}
457
+
458
+ public function angelleye_ppcp_get_discount_amount_from_cart_item () {
459
+ $ cart_item_discount_amount = 0 ;
460
+ foreach (WC ()->cart ->cart_contents as $ cart_item_key => $ values ) {
461
+ $ amount = angelleye_ppcp_round ($ values ['line_subtotal ' ] / $ values ['quantity ' ], $ decimals );
462
+ if ($ amount < 0 ) {
463
+ $ cart_item_discount_amount += angelleye_ppcp_round ($ amount * $ values ['quantity ' ], $ decimals );
464
+ }
465
+ }
466
+ foreach (WC ()->cart ->get_fees () as $ cart_item_key => $ fee_values ) {
467
+ if ($ fee_values ->amount < 0 ) {
468
+ $ cart_item_discount_amount += angelleye_ppcp_round ($ fee_values ->amount * 1 , $ decimals );
469
+ }
470
+ }
471
+ return $ cart_item_discount_amount ;
472
+ }
457
473
458
474
public function angelleye_ppcp_get_details_from_cart () {
459
475
try {
460
476
$ decimals = $ this ->angelleye_ppcp_get_number_of_decimal_digits ();
461
477
$ rounded_total = $ this ->angelleye_ppcp_get_rounded_total_in_cart ();
462
478
$ discounts = WC ()->cart ->get_cart_discount_total ();
463
-
479
+ $ cart_item_discount_amount = $ this ->angelleye_ppcp_get_discount_amount_from_cart_item ();
480
+ $ cart_item_discount_amount + $ discounts ;
464
481
// TODO Verify why this has been added here and in HPOS branch??
465
482
$ cart_contents_total = $ rounded_total + $ discounts ;
466
483
$ order_tax = WC ()->cart ->tax_total + WC ()->cart ->shipping_tax_total ;
@@ -499,6 +516,9 @@ public function angelleye_ppcp_get_details_from_cart() {
499
516
'shipping_address ' => $ this ->angelleye_ppcp_get_address_from_customer (),
500
517
'email ' => WC ()->customer ->get_billing_email (),
501
518
);
519
+ if ((float ) $ details ['total_item_amount ' ] == 0 ) {
520
+ $ details ['total_item_amount ' ] = WC ()->cart ->fee_total ;
521
+ }
502
522
return $ this ->angelleye_ppcp_get_details ($ details , $ discounts , $ rounded_total , $ cart_total );
503
523
} catch (Exception $ ex ) {
504
524
$ this ->api_log ->log ("The exception was created on line: " . $ ex ->getFile () . ' ' . $ ex ->getLine (), 'error ' );
@@ -611,12 +631,16 @@ public function angelleye_ppcp_get_rounded_total_in_cart() {
611
631
$ rounded_total = 0 ;
612
632
foreach (WC ()->cart ->cart_contents as $ cart_item_key => $ values ) {
613
633
$ amount = angelleye_ppcp_round ($ values ['line_subtotal ' ] / $ values ['quantity ' ], $ decimals );
614
- $ rounded_total += angelleye_ppcp_round ($ amount * $ values ['quantity ' ], $ decimals );
634
+ if ($ amount > 0 ) {
635
+ $ rounded_total += angelleye_ppcp_round ($ amount * $ values ['quantity ' ], $ decimals );
636
+ }
615
637
}
616
638
foreach (WC ()->cart ->get_fees () as $ cart_item_key => $ fee_values ) {
617
- $ rounded_total += angelleye_ppcp_round ($ fee_values ->amount * 1 , $ decimals );
639
+ if ($ fee_values ->amount > 0 ) {
640
+ $ rounded_total += angelleye_ppcp_round ($ fee_values ->amount * 1 , $ decimals );
641
+ }
618
642
}
619
- return $ rounded_total ;
643
+ return angelleye_ppcp_round ( $ rounded_total, $ decimals ) ;
620
644
} catch (Exception $ ex ) {
621
645
$ this ->api_log ->log ("The exception was created on line: " . $ ex ->getFile () . ' ' . $ ex ->getLine (), 'error ' );
622
646
$ this ->api_log ->log ($ ex ->getMessage (), 'error ' );
@@ -671,24 +695,29 @@ public function angelleye_ppcp_get_paypal_line_items_from_cart() {
671
695
$ desc = strip_shortcodes ($ desc );
672
696
$ desc = str_replace ("\n" , " " , $ desc );
673
697
$ desc = preg_replace ('/\s+/ ' , ' ' , $ desc );
674
- $ item = array (
675
- 'name ' => $ product_name ,
676
- 'description ' => apply_filters ('angelleye_ppcp_product_description ' , $ desc ),
677
- 'sku ' => $ sku ,
678
- 'category ' => $ category ,
679
- 'quantity ' => $ values ['quantity ' ],
680
- 'amount ' => $ amount ,
681
- );
682
- $ items [] = $ item ;
698
+ if ($ amount > 0 ) {
699
+ $ item = array (
700
+ 'name ' => $ product_name ,
701
+ 'description ' => apply_filters ('angelleye_ppcp_product_description ' , $ desc ),
702
+ 'sku ' => $ sku ,
703
+ 'category ' => $ category ,
704
+ 'quantity ' => $ values ['quantity ' ],
705
+ 'amount ' => $ amount ,
706
+ );
707
+ $ items [] = $ item ;
708
+ }
683
709
}
684
710
foreach (WC ()->cart ->get_fees () as $ cart_item_key => $ fee_values ) {
685
- $ fee_item = array (
686
- 'name ' => html_entity_decode (wc_trim_string ($ fee_values ->name ? $ fee_values ->name : __ ('Fee ' , 'paypal-for-woocommerce ' ), 127 ), ENT_NOQUOTES , 'UTF-8 ' ),
687
- 'description ' => '' ,
688
- 'quantity ' => 1 ,
689
- 'amount ' => AngellEYE_Gateway_Paypal::number_format ($ fee_values ->amount ),
690
- );
691
- $ items [] = $ fee_item ;
711
+ $ amount = AngellEYE_Gateway_Paypal::number_format ($ fee_values ->amount );
712
+ if ($ amount > 0 ) {
713
+ $ fee_item = array (
714
+ 'name ' => html_entity_decode (wc_trim_string ($ fee_values ->name ? $ fee_values ->name : __ ('Fee ' , 'paypal-for-woocommerce ' ), 127 ), ENT_NOQUOTES , 'UTF-8 ' ),
715
+ 'description ' => '' ,
716
+ 'quantity ' => 1 ,
717
+ 'amount ' => AngellEYE_Gateway_Paypal::number_format ($ fee_values ->amount ),
718
+ );
719
+ $ items [] = $ fee_item ;
720
+ }
692
721
}
693
722
return $ items ;
694
723
} catch (Exception $ ex ) {
@@ -740,10 +769,11 @@ public function angelleye_ppcp_get_details($details, $discounts, $rounded_total,
740
769
$ details ['order_total ' ] = angelleye_ppcp_round (
741
770
$ details ['total_item_amount ' ] + $ details ['order_tax ' ] + $ details ['shipping ' ] - $ discounts , $ decimals
742
771
);
772
+
743
773
$ diff = 0 ;
744
774
if ($ details ['total_item_amount ' ] != $ rounded_total ) {
745
775
$ diff = round ($ details ['total_item_amount ' ] + $ discounts - $ rounded_total , $ decimals );
746
- if (abs ( $ diff) > 0.000001 && 0.0 !== (float ) $ diff ) {
776
+ if ($ diff > 0.000001 && 0.0 !== (float ) $ diff ) {
747
777
$ extra_line_item = $ this ->angelleye_ppcp_get_extra_offset_line_item ($ diff );
748
778
$ details ['items ' ][] = $ extra_line_item ;
749
779
$ details ['total_item_amount ' ] += $ extra_line_item ['amount ' ];
@@ -1436,6 +1466,9 @@ public function angelleye_ppcp_get_details_from_order($order_id) {
1436
1466
'shipping ' => angelleye_ppcp_round ($ order ->get_shipping_total (), $ decimals ),
1437
1467
'items ' => $ this ->angelleye_ppcp_get_paypal_line_items_from_order ($ order ),
1438
1468
);
1469
+ if ((float ) $ details ['total_item_amount ' ] == 0 ) {
1470
+ $ details ['total_item_amount ' ] = $ order ->get_total_fees ();
1471
+ }
1439
1472
$ details = $ this ->angelleye_ppcp_get_details ($ details , $ order ->get_total_discount (), $ rounded_total , $ order ->get_total ());
1440
1473
return $ details ;
1441
1474
} catch (Exception $ ex ) {
@@ -1451,11 +1484,15 @@ public function angelleye_ppcp_get_rounded_total_in_order($order) {
1451
1484
$ rounded_total = 0 ;
1452
1485
foreach ($ order ->get_items () as $ cart_item_key => $ values ) {
1453
1486
$ amount = angelleye_ppcp_round ($ values ['line_subtotal ' ] / $ values ['qty ' ], $ decimals );
1454
- $ rounded_total += angelleye_ppcp_round ($ amount * $ values ['qty ' ], $ decimals );
1487
+ if ($ amount > 0 ) {
1488
+ $ rounded_total += angelleye_ppcp_round ($ amount * $ values ['qty ' ], $ decimals );
1489
+ }
1455
1490
}
1456
1491
foreach ($ order ->get_fees () as $ cart_item_key => $ fee_values ) {
1457
1492
$ amount = $ order ->get_line_total ($ fee_values );
1458
- $ rounded_total += angelleye_ppcp_round ($ amount * 1 , $ decimals );
1493
+ if ($ amount > 0 ) {
1494
+ $ rounded_total += angelleye_ppcp_round ($ amount * 1 , $ decimals );
1495
+ }
1459
1496
}
1460
1497
return $ rounded_total ;
1461
1498
} catch (Exception $ ex ) {
@@ -1499,27 +1536,30 @@ public function angelleye_ppcp_get_paypal_line_items_from_order($order) {
1499
1536
$ desc = strip_shortcodes ($ desc );
1500
1537
$ desc = str_replace ("\n" , " " , $ desc );
1501
1538
$ desc = preg_replace ('/\s+/ ' , ' ' , $ desc );
1502
- $ item = array (
1503
- 'name ' => $ product_name ,
1504
- 'description ' => apply_filters ('angelleye_ppcp_product_description ' , $ desc ),
1505
- 'sku ' => $ sku ,
1506
- 'category ' => $ category ,
1507
- 'quantity ' => $ values ['quantity ' ],
1508
- 'amount ' => $ amount ,
1509
- );
1510
- $ items [] = $ item ;
1539
+ if ($ amount > 0 ) {
1540
+ $ item = array (
1541
+ 'name ' => $ product_name ,
1542
+ 'description ' => apply_filters ('angelleye_ppcp_product_description ' , $ desc ),
1543
+ 'sku ' => $ sku ,
1544
+ 'category ' => $ category ,
1545
+ 'quantity ' => $ values ['quantity ' ],
1546
+ 'amount ' => $ amount ,
1547
+ );
1548
+ $ items [] = $ item ;
1549
+ }
1511
1550
}
1512
- foreach ($ order ->get_fees () as $ cart_item_key => $ fee_values ) {
1551
+ foreach ($ order ->get_fees () as $ fee_values ) {
1513
1552
$ fee_item_name = $ fee_values ->get_name ();
1514
1553
$ amount = $ order ->get_line_total ($ fee_values );
1515
- $ item = array (
1516
- 'name ' => html_entity_decode (wc_trim_string ($ fee_item_name ? $ fee_item_name : __ ('Fee ' , 'paypal-for-woocommerce ' ), 127 ), ENT_NOQUOTES , 'UTF-8 ' ),
1517
- 'desc ' => '' ,
1518
- 'qty ' => 1 ,
1519
- 'amt ' => AngellEYE_Gateway_Paypal::number_format ($ amount , $ order ),
1520
- 'number ' => ''
1521
- );
1522
- $ items [] = $ item ;
1554
+ if ($ amount > 0 ) {
1555
+ $ item = array (
1556
+ 'name ' => html_entity_decode (wc_trim_string ($ fee_item_name ? $ fee_item_name : __ ('Fee ' , 'paypal-for-woocommerce ' ), 127 ), ENT_NOQUOTES , 'UTF-8 ' ),
1557
+ 'description ' => '' ,
1558
+ 'quantity ' => 1 ,
1559
+ 'amount ' => angelleye_ppcp_round ($ order ->get_line_total ($ fee_values ), $ decimals )
1560
+ );
1561
+ $ items [] = $ item ;
1562
+ }
1523
1563
}
1524
1564
return $ items ;
1525
1565
} catch (Exception $ ex ) {
0 commit comments