在 Woocommerce 中根据客户送货区域显示自定义消息

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

在 woocommerce 中,我需要根据送货区域在购物车或结账页面上显示自定义消息,例如“您将为此邮政编码多付 10% 的费用”。

我的解决方法是自定义这种默认消息:

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;
}
php wordpress woocommerce notice
3个回答
4
投票

已更新

根据运输区域名称(具有邮政编码限制)尝试以下代码,该代码将在运输总行上显示您的消息(但不会生成 woocommerce 通知):

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>';
    }
}

代码位于活动子主题(或活动主题)的 function.php 文件中。已测试并有效。


2
投票

使用上面@LoicTheAztec的解决方案,我将其修改为根据客户所在的国家/地区给出消息。

add_action( 'woocommerce_cart_totals_after_shipping' , 'out_of_zone_shipping_notice' );
add_action( 'woocommerce_review_order_after_shipping' , 'out_of_zone_shipping_notice' );
function out_of_zone_shipping_notice() {
    // HERE DEFINE YOUR SHIPPING COUNTRY NAMES
    $targeted_country_names = array("CA", "US"); // 

    // Get the customer shipping country
    $shipping_country    = WC()->customer->get_shipping_country();
    
    if( !in_array( $shipping_country, $targeted_country_names ) ){
        echo '<tr class="shipping"><td colspan="2" style="text-align:center">You are outside of our regular shipping zone. Please <a href="../contact/">contact us</a> with your address so we can get an accurate cost to ship.</td></tr>';
    }
}

为了查看 $shipping_country 显示的格式,我使用了

echo $shipping_country

位于 $shipping_country = WC()->customer->get_country(); 下方,在购物车页面上显示实际的国家/地区代码语法,以便我可以将其与 if 语句进行匹配。但是,此代码段在部署之前已被删除,以避免客户屏幕上出现随机字符。


0
投票

有人可以解释一下,我如何使用 @LoicTheAztec 的代码并将其与小于 x€ 的购物车金额结合起来?

更详细: 我想根据送货区域显示一条消息,并且仅当购物车金额小于 x €

f.e. 运输区域 1 且购物车金额小于 29 €,应显示文本 1 运输区域 2 且购物车金额小于 49 €,应显示文本 2

这可能吗? 如果有人能帮助我,那就太好了!

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