根据 WooCommerce 结帐中选择的运输方式更改付款方式标题
Change payment method title based on selected shipping method in WooCommerce checkout
如果选择了送货选项,我正在尝试打开 (COD) 货到付款标签
用户有 2 个交货选项:
<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">
如果选中 value="samedaycourier:15:LN" 我希望 COD 标签是信用卡货到付款而不是货到付款
我复制了一个类似的代码并尝试根据我的需要对其进行调整,不幸的是没有得到想要的结果。有什么建议吗?
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
}
无需使用jQuery,可以使用woocommerce_gateway_title
过滤挂钩和WC()->session->get( 'chosen_shipping_methods' )
根据您的需要调整 $payment_id === ''
和 $chosen_shipping_methods
的结果
所以你得到:
function filter_woocommerce_gateway_title( $title, $payment_id ) {
// Only on checkout page and for COD
if ( is_checkout() && ! is_wc_endpoint_url() && $payment_id === 'cod' ) {
// Get chosen shipping methods
$chosen_shipping_methods = (array) WC()->session->get( 'chosen_shipping_methods' );
// Compare
if ( in_array( 'local_pickup:1', $chosen_shipping_methods ) ) {
$title = __( 'My title', 'woocommerce' );
} elseif ( in_array( 'lsamedaycourier:7:24', $chosen_shipping_methods ) ) {
$title = __( 'Credit Card on Delivery', 'woocommerce' );
} else {
$title = __( 'Another title', 'woocommerce' );
}
}
return $title;
}
add_filter( 'woocommerce_gateway_title', 'filter_woocommerce_gateway_title', 10, 2 );
注意: 要找到正确的 $chosen_shipping_methods
(ID),您可以使用(第 2 部分)来自 [= 的“用于调试目的” 17=]
如果选择了送货选项,我正在尝试打开 (COD) 货到付款标签
用户有 2 个交货选项:
<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">
如果选中 value="samedaycourier:15:LN" 我希望 COD 标签是信用卡货到付款而不是货到付款
我复制了一个类似的代码并尝试根据我的需要对其进行调整,不幸的是没有得到想要的结果。有什么建议吗?
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
}
无需使用jQuery,可以使用woocommerce_gateway_title
过滤挂钩和WC()->session->get( 'chosen_shipping_methods' )
根据您的需要调整 $payment_id === ''
和 $chosen_shipping_methods
的结果
所以你得到:
function filter_woocommerce_gateway_title( $title, $payment_id ) {
// Only on checkout page and for COD
if ( is_checkout() && ! is_wc_endpoint_url() && $payment_id === 'cod' ) {
// Get chosen shipping methods
$chosen_shipping_methods = (array) WC()->session->get( 'chosen_shipping_methods' );
// Compare
if ( in_array( 'local_pickup:1', $chosen_shipping_methods ) ) {
$title = __( 'My title', 'woocommerce' );
} elseif ( in_array( 'lsamedaycourier:7:24', $chosen_shipping_methods ) ) {
$title = __( 'Credit Card on Delivery', 'woocommerce' );
} else {
$title = __( 'Another title', 'woocommerce' );
}
}
return $title;
}
add_filter( 'woocommerce_gateway_title', 'filter_woocommerce_gateway_title', 10, 2 );
注意: 要找到正确的 $chosen_shipping_methods
(ID),您可以使用(第 2 部分)来自