【问题标题】:WooCommerce prevent checkout until post code enteredWooCommerce 在输入邮政编码之前阻止结帐
【发布时间】:2018-07-31 11:47:22
【问题描述】:

试图阻止客户在输入邮政编码之前点击“继续付款”按钮。但是,我发现使用下面的功能,如果您更改数量并更新购物车 - 该功能不再起作用,您可以继续进行免运费付款。有什么想法吗?

add_action( 'wp_head', 'prevent_proceed_to_checkout' );

function prevent_proceed_to_checkout() {
    echo 'alert(Please enter postcode before payment!")';
}

【问题讨论】:

    标签: php wordpress woocommerce checkout postal-code


    【解决方案1】:

    已更新 - 3 种方式 - (添加了替代方法)

    1) 如果邮政编码未填写,您可以使用以下代码“避免继续结帐”结帐:

    // Avoiding checkout when postcode has not been entered
    add_action( 'woocommerce_check_cart_items', 'check_shipping_postcode' ); // Cart and Checkout
    function check_shipping_postcode() {
        $customer = WC()->session->get('customer');
        if( ! $customer['calculated_shipping'] || empty( $customer['shipping_postcode'] ) ){
            // Display an error message
            wc_add_notice( __("Please enter your postcode before checkout", "woocommerce"), 'error' );
        }
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。

    购物车页面:

    结帐页面中:


    2) 尝试这种替代方式(检查邮政编码并重定向到购物车以避免结帐):

    add_action('template_redirect', 'check_shipping_postcode');
    function check_shipping_postcode() {
        // Only on checkout page (and cart for the displayed message)
        if ( ( is_checkout() && ! is_wc_endpoint_url() ) || is_cart() ) {
            $customer = WC()->session->get('customer');
            if( ! $customer['calculated_shipping'] || empty( $customer['shipping_postcode'] ) ){
                wc_add_notice( __("Please enter your postcode before checkout", "woocommerce"), 'error' );
                if( ! is_cart() ){
                    wp_redirect(wc_get_cart_url());
                    exit();
                }
            }
        }
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。

    购物车页面:


    3)以上两者的组合(避免结帐页面):

    // Avoiding checkout when postcode has not been entered
    add_action( 'woocommerce_check_cart_items', 'check_shipping_postcode' ); // Cart and Checkout
    function check_shipping_postcode() {
        $customer = WC()->session->get('customer');
        if( ! $customer['calculated_shipping'] || empty( $customer['shipping_postcode'] ) ){
            // Display an error message
            wc_add_notice( __("Please enter your postcode before checkout", "woocommerce"), 'error' );
        }
    }
    
    add_action('template_redirect', 'shipping_postcode_redirection');
    function shipping_postcode_redirection() {
        // Only on checkout page
        if ( is_checkout() && ! is_wc_endpoint_url() ) {
            $customer = WC()->session->get('customer');
            if( ! $customer['calculated_shipping'] || empty( $customer['shipping_postcode'] ) ){
                wp_redirect(wc_get_cart_url());
                exit();
            }
        }
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。

    购物车页面:

    【讨论】:

    • 感谢您的回复,但不幸的是没有工作,也许是因为我有第三方支付网关?除了在购物车中立即显示通知之外,没有真正发生任何事情
    • @ViktorFonster Strange...我添加了第二个替代方案...请尝试一下。即使您有自定义支付网关,它也应该可以工作。
    • 酷!它几乎奏效了!即使您更改数量等。唯一的问题是购物车简单地重新加载并且没有显示错误通知。也许一个懒惰的解决方案是,如果我将用户重定向到带有通知和返回购物车按钮的页面(这不会那么好),或者只是有一个总是显示需要邮政编码的大通知?
    • 不幸的是:(
    • @ViktorFonster 我有第三种选择,应该满足您的需求。
    猜你喜欢
    • 2016-10-20
    • 2018-05-31
    • 2021-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-14
    相关资源
    最近更新 更多