1010use Logeecom \Infrastructure \Logger \Logger ;
1111use Logeecom \Infrastructure \ORM \RepositoryRegistry ;
1212use Logeecom \Infrastructure \ServiceRegister ;
13+ use Packlink \BusinessLogic \CashOnDelivery \Services \OfflinePaymentsServices ;
1314use Packlink \BusinessLogic \Location \LocationService ;
1415use Packlink \BusinessLogic \ShippingMethod \Models \ShippingMethod ;
1516use Packlink \WooCommerce \Components \Order \Order_Drop_Off_Map ;
1617use Packlink \WooCommerce \Components \Order \Paid_Order_Handler ;
18+ use Packlink \WooCommerce \Components \Services \Offline_Payments_Service ;
1719use Packlink \WooCommerce \Components \ShippingMethod \Packlink_Shipping_Method ;
1820use Packlink \WooCommerce \Components \ShippingMethod \Shipping_Method_Helper ;
1921use Packlink \WooCommerce \Components \Utility \Script_Loader ;
@@ -40,6 +42,17 @@ class Checkout_Handler {
4042 */
4143 const DEFAULT_SHIPPING = 'shipping cost ' ;
4244
45+ /**
46+ * @var Offline_Payments_Service
47+ */
48+ private $ offline_payments_service ;
49+
50+ public function __construct ()
51+ {
52+ $ this ->offline_payments_service = ServiceRegister::getService (
53+ OfflinePaymentsServices::CLASS_NAME );
54+ }
55+
4356 /**
4457 * This hook is triggered after shipping method label, and it will insert hidden input values.
4558 *
@@ -57,11 +70,27 @@ public function after_shipping_rate( WC_Shipping_Rate $rate, $index ) {
5770 return ;
5871 }
5972
60- $ fields = array (
73+ $ cart = WC ()->cart ;
74+ $ totals = $ cart ->get_totals ();
75+
76+ $ subtotal = isset ($ totals ['cart_contents_total ' ]) ? (float ) $ totals ['cart_contents_total ' ] : 0 ;
77+ $ shipping = isset ($ totals ['shipping_total ' ]) ? (float ) $ totals ['shipping_total ' ] : 0 ;
78+ $ discount = isset ($ totals ['discount_total ' ]) ? (float ) $ totals ['discount_total ' ] : 0 ;
79+
80+ $ current_total = $ subtotal + $ shipping - $ discount ;
81+
82+ $ offlinePaymentName = $ this ->getOfflinePaymentName ();
83+
84+
85+ $ fields = array (
6186 'packlink_image_url ' => $ shipping_method ->getLogoUrl () ?: Shop_Helper::get_plugin_base_url () . 'resources/images/box.svg ' ,
6287 'packlink_show_image ' => $ shipping_method ->isDisplayLogo () ? 'yes ' : 'no ' ,
6388 'packlink_is_drop_off ' => $ shipping_method ->isDestinationDropOff () ? 'yes ' : 'no ' ,
64- );
89+ 'packlink_cash_on_delivery ' => $ this ->is_cash_on_delivery_enabled ($ shipping_method ) ? 'yes ' : 'no ' ,
90+ 'packlink_cash_on_delivery_fee ' => $ this ->offline_payments_service ->calculateFee ($ shipping_method ->getId (), $ current_total ),
91+ 'packlink_cash_on_delivery_name ' => $ offlinePaymentName ?: '' ,
92+
93+ );
6594
6695 foreach ( $ fields as $ field => $ value ) {
6796 $ this ->print_hidden_input ( $ field , $ value );
@@ -264,6 +293,49 @@ public function get_drop_off_locations_missing_message() {
264293 return __ ( 'There are no drop-off locations available for the entered address ' , 'packlink-pro-shipping ' );
265294 }
266295
296+ /**
297+ * Returns the display name of the current offline payment method or null.
298+ *
299+ * @return string|null
300+ */
301+ private function getOfflinePaymentName ()
302+ {
303+ try {
304+ $ offlinePayments = $ this ->offline_payments_service ->getOfflinePayments ();
305+ $ accountConfig = $ this ->offline_payments_service ->getAccountConfiguration ();
306+ $ offlinePaymentName = null ;
307+
308+ if ($ accountConfig && $ accountConfig ->account ) {
309+ $ id = $ accountConfig ->account ->getOfflinePaymentMethod ();
310+
311+ foreach ($ offlinePayments as $ payment ) {
312+ if ($ payment ['name ' ] === $ id ) {
313+ $ offlinePaymentName = $ payment ['displayName ' ];
314+ break ;
315+ }
316+ }
317+ }
318+
319+ return $ offlinePaymentName ;
320+ } catch (\Exception $ e ) {
321+ return null ;
322+ }
323+ }
324+ /**
325+ * @param ShippingMethod $shippingMethod
326+ *
327+ * @return bool
328+ */
329+ private function is_cash_on_delivery_enabled ($ shippingMethod ) {
330+ foreach ($ shippingMethod ->getShippingServices () as $ service ) {
331+ if ($ service ->cashOnDeliveryConfig && $ service ->cashOnDeliveryConfig ->offered ) {
332+ return true ;
333+ }
334+ }
335+
336+ return false ;
337+ }
338+
267339 /**
268340 * Returns Packlink shipping method.
269341 *
0 commit comments