在 Woocommerce 中显示基于客户送货区域的自定义消息

Display a custom message based on customer shipping zone in Woocommerce(在 Woocommerce 中显示基于客户送货区域的自定义消息)
本文介绍了在 Woocommerce 中显示基于客户送货区域的自定义消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

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

推荐答案

更新

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

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

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

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

这篇关于在 Woocommerce 中显示基于客户送货区域的自定义消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Codeigniter htaccess to remove index.php and www(Codeigniter htaccess 删除 index.php 和 www)
htaccess mod_rewrite part of url to GET variable(htaccess mod_rewrite url 的一部分到 GET 变量)
Replacing a querystring parameter value using mod_rewrite(使用 mod_rewrite 替换查询字符串参数值)
.htaccess in subdirectory #39;overriding#39; parent htaccess(子目录“覆盖父 htaccess 中的 .htaccess)
How to rewrite SEO friendly url#39;s like stackoverflow(如何像stackoverflow一样重写SEO友好的url)
Is it okay to have a very long .htaccess file?(有一个很长的 .htaccess 文件可以吗?)