我从事 Woocommerce 项目,我的客户问了我两件事:
- 不想取消选中结帐页面上所有可用的付款方式。
- 想要在选择“cod”作为付款方式时显示一个确认弹出窗口(
<script>confirm("Are you sure?")</script>
),在确认弹出窗口上单击“否”或“取消”后,将付款方式更改为在线支付或其他人接受“cod”。
请任何帮助表示赞赏。
我从事 Woocommerce 项目,我的客户问了我两件事:
<script>confirm("Are you sure?")</script>
),在确认弹出窗口上单击“否”或“取消”后,将付款方式更改为在线支付或其他人接受“cod”。请任何帮助表示赞赏。
我已经成功地实现了你所需要的。我不知道这是否是正确的方法。
add_action( 'woocommerce_review_order_before_payment', 'refresh_payment_methods' );
function refresh_payment_methods(){
?>
<script type="text/javascript">
(function($){
$( 'form.checkout' ).on( 'change', 'input[name^="payment_method"]', function(event) {
var payment_method = $('form.checkout').find('input[name^="payment_method"]:checked').val();
if(payment_method == 'cod'){
var method_select = confirm('Are you sure ?');
if(!method_select){
$('form.checkout').find('input[name^="payment_method"]').each(function(index, el) {
if($(this).val() == 'cod'){
$(this).prop('checked',false);
}else{
$(this).prop('checked',true);
}
});
}else{
$('body').trigger('update_checkout');
}
}else{
$('body').trigger('update_checkout');
}
});
})(jQuery);
</script>
<?php
}
在使用 name 更改表单输入时payment_method
,将采用检查值并将其与可用的付款方式进行比较。如果付款方式是cod
相应的复选框,则未选中。(由于代码对您来说太紧急了,我无法使 else 部分完美)
上面的代码对我来说不能正常工作!
在下面的代码中,如果取消按钮被选中,单选按钮将返回到之前的状态。
以下代码已正确执行:
add_action( 'woocommerce_review_order_before_payment', 'refresh_payment_methods' );
function refresh_payment_methods(){
$chosen_payment_method = WC()->session->get('chosen_payment_method');
?>
<script type="text/javascript">
var j_method = <?='"'. $chosen_payment_method .'"'?>;
(function($){
$( 'form.checkout' ).on( 'change', 'input[name^="payment_method"]', function(event) {
var payment_method = $('form.checkout').find('input[name^="payment_method"]:checked').val();
if(payment_method == 'cod')
{
var method_select = confirm( 'Are you sure ?' );
if( method_select )//OK:
setTimeout(function(){$('body').trigger('update_checkout'); }, 250 );
else//Cancel:
$( '#payment_method_' + j_method ).prop('checked',true);
}else{
setTimeout(function(){$('body').trigger('update_checkout'); }, 250 );
}
//set new value
j_method = $('form.checkout').find('input[name^="payment_method"]:checked').val();
});
})(jQuery);
</script>
<?php
}