I`m trying to make a switch on (COD) cash on delivery label if the delivery option is selected
The user has 2 options on delivery:
<input type="radio" name="shipping_method[0]" data-index="0" id="shipping_method_0_samedaycourier724" value="samedaycourier:7:24" class="shipping_method">
<input type="radio" name="shipping_method[0]" data-index="0" id="shipping_method_0_samedaycourier15ln" value="samedaycourier:15:LN" class="shipping_method" checked="checked">
If the value="samedaycourier:15:LN" is checked I want the COD label to be Credit Card on Delivery instead of Cash on Delivery
I copied a similar code and tried to adapt it to my need, unfortunately without the desired result. Any advice?
add_action( 'woocommerce_review_order_before_payment', 'customizing_payment_option', 10, 0 );
function customizing_payment_option(){
$text1 = __( 'Cash on Delivery', 'woocommerce' );
$text2 = __( 'Credit Card on EasyBox', 'woocommerce' );
?>
<script>
jQuery(function($){
// 1. Initialising once loaded
if($('input[name^="shipping_method[0]"]:checked').val() == 'samedaycourier:15:LN' )
$('input[id^="payment_method_cod"]').text('<?php echo $text2; ?>');
else
$('input[id^="payment_method_cod"]').text('<?php echo $text1; ?>');
// 2. Live event detection:When shipping method is changed
$( 'form.checkout' ).on( 'change', 'input[name^="shipping_method[0]"]', function() {
var choosenDeliveryMethod = $('input[name^="shipping_method[0]"]:checked').val(); // Chosen
if( choosenDeliveryMethod == 'samedaycourier:15:LN' )
$('input[id^="payment_method_cod"]').text('<?php echo $text2; ?>');
else
$('input[id^="payment_method_cod"]').text('<?php echo $text1; ?>');
});
});
</script>
<?php
}
2
Answers
No need to use jQuery, you can use the
woocommerce_gateway_title
filter hook andWC()->session->get( 'chosen_shipping_methods' )
Adjust
$payment_id === ''
and the result of$chosen_shipping_methods
to your needsSo you get:
Note: to find the correct
$chosen_shipping_methods
(ID), you can use (part 2) "For debugging purposes" from this answerThis is what i used for EasyBox services (is a modified code from bbloomer):