【问题标题】:want to check payment method on checkout page after a confirmation确认后想在结帐页面上查看付款方式
【发布时间】:2019-01-27 06:32:50
【问题描述】:

我在做一个 Woocommerce 项目,我的客户问了我两件事:

  1. 不想取消选中结帐页面上所有可用的付款方式。
  2. 想要在选择“cod”作为付款方式时显示确认弹出窗口(<script>confirm("Are you sure?")</script>),在确认弹出窗口中单击“否”或“取消”后,将付款方式更改为在线付款或其他人接受“鳕鱼”。

感谢任何帮助。

【问题讨论】:

  • 如何在结帐页面上以编程方式更改付款方式,不想隐藏任何付款方式。
  • 试试woocommerce_available_payment_gateways钩子
  • @melvin 感谢您的快速回复,我已经尝试过了,但没有运气。
  • add_filter('woocommerce_available_payment_gateways', 'show_custom_payment_gateways',90);功能 show_custom_payment_gateways($available_gateways){ 全球 $woocommerce; $available_gateways = $woocommerce->payment_gateways->get_available_payment_gateways(); $chosen_payment_method = WC()->session->get('chosen_payment_method'); if($chosen_payment_method=='cod'){ $available_gateways['payuindia']->chosen = true; $available_gateways['cod']->chosen = false; // 默认为 false 未选中。 } } 这是我的代码。
  • 我有一个解决方案,即将完成。你已经有解决方案了吗? @sushil

标签: wordpress woocommerce payment-gateway payment-method


【解决方案1】:

我已经成功实现了您的需求。我不知道这是否是正确的方法。

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
}

在更改名称为payment_method 的表单输入时,会采用检查的值并将其与可用的付款方式进行比较。如果付款方式为cod,则未选中相应的复选框。 (由于代码对你来说太紧急了,我无法使 else 部分完美)

【讨论】:

  • @SushilGupta 我强烈建议您尽可能优化代码。另外请进行深入测试,因为它会影响付款方式。问候
  • 亲爱的@melvin,代码运行良好,但在使用优惠券时会出现问题,如果选择 COD,我想从购物车中删除所有优惠券,如果选择“在线支付”模式,我想重新申请
  • @SushilGupta 这有点工作要做。是紧急的还是你能给我时间吗?
  • @SushilGupta 很高兴为您提供帮助。欢迎询问
【解决方案2】:

上面的代码对我来说不能正常工作!

在下面的代码中,如果取消按钮被选中,单选按钮会返回之前的状态。

以下代码已正确执行:

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
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-05
    • 2017-08-19
    • 2015-04-22
    • 2017-08-13
    • 2017-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多