Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

php - Display a custom message based on customer shipping zone in Woocommerce

In woocommerce, I need to display custom message on cart or checkout page, based on shipping zone, like "you'll be charged 10% more for this zip code".

I feel its easy but I can't get it work ! And its drive me crazy! Any help is appreciated.

My workaround is about customizing that kind of default message :

add_filter( 'woocommerce_no_shipping_available_html', 'wf_customize_default_message', 10, 1 );
// For Checkout page
add_filter( 'woocommerce_cart_no_shipping_available_html', 'wf_customize_default_message', 10, 1 );
function wf_customize_default_message( $default_msg ) {
    $zip_array = array(
        '30031',
    );

    if ( in_array( WC()->customer->get_shipping_postcode() , $zip_array) ) {
        $custom_msg = "Call us for quotation - 1-800-XXX-XXXX";
        if( empty( $custom_msg ) ) {
          return $default_msg;
        }
        return $custom_msg;
    }

    return $default_msg;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Updated

Try the following code based on a shipping Zones name (with postcodes restrictions) that will display your message on the shipping total lines (but that will not generate a woocommerce notice):

add_action( 'woocommerce_cart_totals_after_shipping' , 'shipping_zone_targeted_postcodes_custom_notice' );
add_action( 'woocommerce_review_order_after_shipping' , 'shipping_zone_targeted_postcodes_custom_notice' );
function shipping_zone_targeted_postcodes_custom_notice() {
    // HERE DEFINE YOUR SHIPPING ZONE NAME(S)
    $targeted_zones_names = array('France'); // <======  <======  <======  <======  <======  

    // 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[1] );
    $current_zone_name = $shipping_zone->get_zone_name();

    if( in_array( $current_zone_name, $targeted_zones_names ) ){
        echo '<tr class="shipping">
            <td colspan="2" style="text-align:center">' . sprintf(
                __( "You'll be charged %s more for %s zip code", "woocommerce"),
                '<strong>10%</strong>',
                '<strong>' . WC()->customer->get_shipping_postcode() . '</strong>'
            ) . '</td>
        </tr>';
    }
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...