当输入的邮政编码未被 woocommerce 中的任何送货区域覆盖时,有没有办法显示自定义消息?

问题描述 投票:0回答:1

我只有一个统一费率的送货区域,仅覆盖悉尼境内的几个邮政编码。我希望在输入不在该区域内的任何邮政编码时,在总计部分(显示运费)中显示自定义消息,并禁用“下订单”按钮。目前,当客户输入未包含的邮政编码时,运费字段显示为空,并且只有在选择“下订单”后才会收到错误消息。

谢谢你。

我尝试过一些代码,如下所示:

// Validate
function ts_woocommerce_after_checkout_validation( $data, $error ) {
// The accepted delivery zones
$del_zones_array = array( 2000, 2170, 2007, 2008, 2009, 2010, 2011, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024, 2025, 2026, 2027, 2028, 2029, 2030, 2031, 2033, 2032, 2034, 2035, 2036, 2037, 2038, 2039, 2040, 2041, 2042, 2043, 2044, 2045, 2046, 2047, 2048, 2049, 2059, 2060, 2061, 2062, 2063, 2064, 2065, 2066, 2067, 2068, 2069, 2070, 2071, 2072, 2073, 2074, 2075, 2076, 2077, 2079, 2080, 2081, 2082, 2084, 2085, 2086, 2087, 2088, 2089, 2090, 2110, 2111, 2112, 2113, 2114, 2115, 2116, 2117, 2118, 2119, 2120, 2121, 2122, 2125, 2126, 2127, 2130, 2131, 2132, 2133, 2134, 2135, 2136, 2137, 2138, 2140, 2145, 2146, 2147, 2148, 2150, 2151, 2152, 2153, 2154, 2155, 2156, 2158, 2159, 2190, 2191, 2192, 2193, 2194, 2195, 2196, 2204, 2205, 2206, 2207, 2208, 2209, 2210, 2211, 2216, 2217, 2218, 2219, 2220, 2221, 2222, 2223, 2234, 2763, 2765, 2224, 2161, 2769, 2160, 2762, 2199, 2075, 2225, 2203 );

// If the postal is not within the array, deny checkout
if( ! in_array( $data\['shipping_postcode'\], $del_zones_array ) ) {
$error-\>add( 'validation', 'Sorry, we currently do not offer shipping to your area.' );
}
}
add_action('woocommerce_after_checkout_validation', 'ts_woocommerce_after_checkout_validation', 10, 2 );

但是达不到我想要达到的目标。

woocommerce checkout shipping-method
1个回答
0
投票

我找到了这段代码并尝试在我的 woocommerce 上使用它,但没有成功。我想定位我的任何送货区域未覆盖的邮政编码。

add_action( 'woocommerce_before_checkout_form', 'shipping_zone_targeted_postcodes_custom_notice' );
add_action( 'woocommerce_before_cart_table', 'shipping_zone_targeted_postcodes_custom_notice' );
function shipping_zone_targeted_postcodes_custom_notice() {

    // HERE DEFINE YOUR SHIPPING ZONE NAME(S)
    $targeted_zones_names = array('Locations not covered by your other zones'); // <======  <======  <======  <====== 

    // Get the customer shipping zone name
    $chosen_methods    = WC()->session->get( 'chosen_shipping_methods' ); // The chosen shipping mehod
    $chosen_method     = explode(':', reset($chosen_methods) );
    $shipping_zone     = WC_Shipping_Zones::get_zone_by( 'instance_id', $chosen_method[0] );
    $current_zone_name = $shipping_zone->get_zone_name();


    // Loop through cart items
    foreach ( WC()->cart->get_cart() as $cart_item ) {
        if ( has_term( $special_cat, 'product_cat', $cart_item['product_id'] ) )
            $bool = true;
    }

    if( ! in_array( $current_zone_name, $targeted_zones_names ) && $bool ){
        echo '<p class="message-text">(Sorry, we currently do not offer shipping to your area.)</p>';    
    }
}

© www.soinside.com 2019 - 2024. All rights reserved.