WooCommerce在输入邮政编码之前阻止结帐

时间:2018-07-31 11:47:22

标签: php wordpress woocommerce cart postal-code

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

Error: StaticInjectorError(DynamicTestModule)[UploadPage -> Toaster]: 
  StaticInjectorError(Platform: core)[UploadPage -> Toaster]: 
    NullInjectorError: No provider for Toaster!

1 个答案:

答案 0 :(得分:2)

已更新-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文件中。经过测试,可以正常工作。

在购物车页面:

enter image description here

在结帐页面:

enter image description here


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文件中。经过测试,可以正常工作。

在购物车页面:

enter image description here


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文件中。经过测试,可以正常工作。

在购物车页面:

enter image description here