Skip to content

Commit 1d3f3ed

Browse files
Merge pull request #1974 from angelleye/PFW-1687
PFW-1687, Handle discount line item
2 parents 2227109 + 9db1298 commit 1d3f3ed

File tree

2 files changed

+82
-42
lines changed

2 files changed

+82
-42
lines changed

ppcp-gateway/class-angelleye-paypal-ppcp-admin-action.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ public function angelleye_ppcp_order_action_list($order_id) {
340340
$this->angelleye_ppcp_order_actions = array();
341341
$paypal_order_id = angelleye_ppcp_get_post_meta($order, '_paypal_order_id');
342342
if (empty($paypal_order_id)) {
343-
echo __('PayPal order id does not exist for this order.', 'paypal-for-woocommerce');
343+
//echo __('PayPal order id does not exist for this order.', 'paypal-for-woocommerce');
344344
return;
345345
}
346346
$this->payment_response = $this->payment_request->angelleye_ppcp_get_paypal_order_details($paypal_order_id);

ppcp-gateway/class-angelleye-paypal-ppcp-payment.php

Lines changed: 81 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -454,13 +454,30 @@ public function isSubscriptionRequired($order = null): bool {
454454
}
455455
return false;
456456
}
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+
}
457473

458474
public function angelleye_ppcp_get_details_from_cart() {
459475
try {
460476
$decimals = $this->angelleye_ppcp_get_number_of_decimal_digits();
461477
$rounded_total = $this->angelleye_ppcp_get_rounded_total_in_cart();
462478
$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;
464481
// TODO Verify why this has been added here and in HPOS branch??
465482
$cart_contents_total = $rounded_total + $discounts;
466483
$order_tax = WC()->cart->tax_total + WC()->cart->shipping_tax_total;
@@ -499,6 +516,9 @@ public function angelleye_ppcp_get_details_from_cart() {
499516
'shipping_address' => $this->angelleye_ppcp_get_address_from_customer(),
500517
'email' => WC()->customer->get_billing_email(),
501518
);
519+
if((float) $details['total_item_amount'] == 0) {
520+
$details['total_item_amount'] = WC()->cart->fee_total;
521+
}
502522
return $this->angelleye_ppcp_get_details($details, $discounts, $rounded_total, $cart_total);
503523
} catch (Exception $ex) {
504524
$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() {
611631
$rounded_total = 0;
612632
foreach (WC()->cart->cart_contents as $cart_item_key => $values) {
613633
$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+
}
615637
}
616638
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+
}
618642
}
619-
return $rounded_total;
643+
return angelleye_ppcp_round($rounded_total, $decimals);
620644
} catch (Exception $ex) {
621645
$this->api_log->log("The exception was created on line: " . $ex->getFile() . ' ' . $ex->getLine(), 'error');
622646
$this->api_log->log($ex->getMessage(), 'error');
@@ -671,24 +695,29 @@ public function angelleye_ppcp_get_paypal_line_items_from_cart() {
671695
$desc = strip_shortcodes($desc);
672696
$desc = str_replace("\n", " ", $desc);
673697
$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+
}
683709
}
684710
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+
}
692721
}
693722
return $items;
694723
} catch (Exception $ex) {
@@ -740,10 +769,11 @@ public function angelleye_ppcp_get_details($details, $discounts, $rounded_total,
740769
$details['order_total'] = angelleye_ppcp_round(
741770
$details['total_item_amount'] + $details['order_tax'] + $details['shipping'] - $discounts, $decimals
742771
);
772+
743773
$diff = 0;
744774
if ($details['total_item_amount'] != $rounded_total) {
745775
$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) {
747777
$extra_line_item = $this->angelleye_ppcp_get_extra_offset_line_item($diff);
748778
$details['items'][] = $extra_line_item;
749779
$details['total_item_amount'] += $extra_line_item['amount'];
@@ -1436,6 +1466,9 @@ public function angelleye_ppcp_get_details_from_order($order_id) {
14361466
'shipping' => angelleye_ppcp_round($order->get_shipping_total(), $decimals),
14371467
'items' => $this->angelleye_ppcp_get_paypal_line_items_from_order($order),
14381468
);
1469+
if((float) $details['total_item_amount'] == 0) {
1470+
$details['total_item_amount'] = $order->get_total_fees();
1471+
}
14391472
$details = $this->angelleye_ppcp_get_details($details, $order->get_total_discount(), $rounded_total, $order->get_total());
14401473
return $details;
14411474
} catch (Exception $ex) {
@@ -1451,11 +1484,15 @@ public function angelleye_ppcp_get_rounded_total_in_order($order) {
14511484
$rounded_total = 0;
14521485
foreach ($order->get_items() as $cart_item_key => $values) {
14531486
$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+
}
14551490
}
14561491
foreach ($order->get_fees() as $cart_item_key => $fee_values) {
14571492
$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+
}
14591496
}
14601497
return $rounded_total;
14611498
} catch (Exception $ex) {
@@ -1499,27 +1536,30 @@ public function angelleye_ppcp_get_paypal_line_items_from_order($order) {
14991536
$desc = strip_shortcodes($desc);
15001537
$desc = str_replace("\n", " ", $desc);
15011538
$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+
}
15111550
}
1512-
foreach ($order->get_fees() as $cart_item_key => $fee_values) {
1551+
foreach ($order->get_fees() as $fee_values) {
15131552
$fee_item_name = $fee_values->get_name();
15141553
$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+
}
15231563
}
15241564
return $items;
15251565
} catch (Exception $ex) {

0 commit comments

Comments
 (0)