WooCommerce - 如果未选择,则取消选择默认网关和更改订单按钮

时间:2021-07-22 16:18:12

标签: javascript php wordpress woocommerce

所以这个对我来说真的很痛苦。

我遇到了一个问题,客户在下订单后联系我们说“糟糕,我选择了错误的付款方式,我可以更改吗?”

这是由于客户试图尽快结帐并快速向下滚动到“下订单”按钮造成的。

为了解决我想在结账时取消选择默认网关并阻止他们在未选择任何网关的情况下结账的问题(抛出错误和/或禁用下订单按钮)

我现在的情况是我可以取消选择眼睛的输入(或者看起来是这样),但是页面加载时我的 var_dump 仍然显示一个已选中。

即使当我尝试将此变量传递给下订单按钮时​​,尽管第一个转储显示了一个值,它还是返回 NULL

我做错了什么?

// -------------------------------------------
// CHECKOUT PAYMENT METHOD WORK - NOT FINISHED
// -------------------------------------------
function disable_default_payment_method() {
    if ( is_checkout() ) {
    ?>
        <script>
            jQuery(document).ready(function ($) {
    function deselectDefaultGateway() {
        $('input[name="payment_method"]').each(function (index, item) {
            $(item).attr('checked', false);
        })
        $('.payment_box').hide();
    }

    $(document.body).on('updated_checkout', deselectDefaultGateway);

    $(document).on('click', '.wc_payment_method', function(e){
        if (e.originalEvent !== undefined) {
            $(document.body).off('updated_checkout', deselectDefaultGateway);
        }
    });
});
        </script>
    <?php
    }
}
add_action( 'wp_head', 'disable_default_payment_method' );

add_action( 'woocommerce_before_template_part', 'custom_before_template_part', 10, 4 );
function custom_before_template_part($template_name, $template_path, $located, $args) {
    if ( 'checkout/payment-method.php' == $template_name ) {
        $gateway = $args['gateway'];
        $gateway->chosen = false;
    }
}

function get_payment_method() {
    $the_method = WC()->session->get('chosen_payment_method');
    return $the_method;
}

function check_method() {
    $method = get_payment_method();
    var_dump($method);
}

add_action( 'woocommerce_review_order_before_payment', 'check_method' );

function the_gateways_function() {

if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;

   $the_method = get_payment_method();

   if ( $the_method ) {
        add_filter( 'woocommerce_order_button_html', 'replace_order_button_html', 10, 2 );
        function replace_order_button_html( $order_button ) {

            if( $order ) return $order_button;

            $order_button_text = __( "Payment Not Selected", "woocommerce" );

            $style = ' style="color:#fff;cursor:not-allowed;background-color:#999;"';
            var_dump($the_method);
            //return '<a class="button alt"'.$style.' name="woocommerce_checkout_place_order" id="place_order" >' . esc_html( $order_button_text ) . '</a>';
        }
    }
}

add_action('woocommerce_checkout_update_order_review', 'the_gateways_function');

0 个答案:

没有答案