Skip to content

Commit f7ae2ac

Browse files
committed
Add a message for cash on delivery for classic checkout
ISSUE: PKLPM-71
1 parent 67eded2 commit f7ae2ac

5 files changed

Lines changed: 107 additions & 4 deletions

File tree

1007 Bytes
Binary file not shown.

src/Components/Checkout/class-checkout-handler.php

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
use Logeecom\Infrastructure\Logger\Logger;
1111
use Logeecom\Infrastructure\ORM\RepositoryRegistry;
1212
use Logeecom\Infrastructure\ServiceRegister;
13+
use Packlink\BusinessLogic\CashOnDelivery\Services\OfflinePaymentsServices;
1314
use Packlink\BusinessLogic\Location\LocationService;
1415
use Packlink\BusinessLogic\ShippingMethod\Models\ShippingMethod;
1516
use Packlink\WooCommerce\Components\Order\Order_Drop_Off_Map;
1617
use Packlink\WooCommerce\Components\Order\Paid_Order_Handler;
18+
use Packlink\WooCommerce\Components\Services\Offline_Payments_Service;
1719
use Packlink\WooCommerce\Components\ShippingMethod\Packlink_Shipping_Method;
1820
use Packlink\WooCommerce\Components\ShippingMethod\Shipping_Method_Helper;
1921
use 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
*

src/Components/Services/class-offline-payments-service.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ public function calculateFee( $shippingMethodId, $amount ) {
166166
$cod = null;
167167
}
168168

169-
if ( $cod && $cod->account && $cod->account->getCashOnDeliveryFee() ) {
169+
if ( $cod && $cod->account && $cod->account->getCashOnDeliveryFee() !== null) {
170170
return $cod->account->getCashOnDeliveryFee();
171171
}
172172

src/class-plugin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,7 @@ private function checkout_hooks_and_actions() {
725725
add_action( 'woocommerce_checkout_update_order_meta', array( $handler, 'checkout_update_drop_off' ), 10, 2 );
726726
add_action( 'wp_enqueue_scripts', array( $handler, 'load_scripts' ) );
727727

728-
add_action('woocommerce_checkout_create_order', array($surcharge_handle, 'add_surcharge_to_order'), 20, 1); // Classic checkout
728+
add_action('woocommerce_checkout_create_order', array($surcharge_handle, 'add_surcharge_to_order'), 20, 1);
729729

730730
add_action( 'woocommerce_store_api_checkout_update_order_from_request', [ $surcharge_handle, 'add_surcharge_block' ], 10, 2 );
731731

src/resources/js/packlink-checkout.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,21 @@ var Packlink = window.Packlink || {};
3939
let button = parent.querySelector( '#packlink-drop-off-picker' );
4040
let isDropOff = parent.querySelector( 'input[name="packlink_is_drop_off"]' );
4141

42+
let isCOD = parent.querySelector('input[name="packlink_cash_on_delivery"]');
43+
44+
if (isCOD && isCOD.value === 'yes') {
45+
const codFeeInput = parent.querySelector('input[name="packlink_cash_on_delivery_fee"]');
46+
let codNameInput = parent.querySelector('input[name="packlink_cash_on_delivery_name"]');
47+
const codName = codNameInput ? codNameInput.value : '';
48+
49+
const codFee = codFeeInput ? parseFloat(codFeeInput.value) : 0;
50+
51+
const shippingInput = parent.querySelector('input[name^="shipping_method"]');
52+
if (shippingInput && shippingInput.checked) {
53+
addCODMessage(parent, codName, codFee);
54+
}
55+
}
56+
4257
if (showImage === 'yes' && imageInput && parent.querySelector('.pl-checkout-carrier-image') === null) {
4358
injectImage( imageInput );
4459
}
@@ -110,6 +125,22 @@ var Packlink = window.Packlink || {};
110125
}
111126
}
112127

128+
function addCODMessage(dataDiv, codName, codFee) {
129+
if (!dataDiv) return;
130+
if (!codName || !codFee || codFee <= 0) return;
131+
132+
if (dataDiv.querySelector('.packlink-cod-message')) return;
133+
134+
const messageDiv = document.createElement('div');
135+
messageDiv.className = 'packlink-cod-message';
136+
messageDiv.innerHTML = `This service supports <strong>${codName}</strong>. If you choose the <strong>${codName}</strong> payment method, an additional fee of <strong>${codFee}</strong> will be applied.`;
137+
messageDiv.style.marginTop = '8px';
138+
messageDiv.style.fontSize = '12px';
139+
messageDiv.style.color = '#555';
140+
141+
dataDiv.lastChild.before(messageDiv);
142+
}
143+
113144
/**
114145
* Sets locations.
115146
*

0 commit comments

Comments
 (0)