【问题标题】:Display a custom message based on customer shipping zone in Woocommerce在 Woocommerce 中根据客户发货区域显示自定义消息
【发布时间】:2019-03-15 18:40:35
【问题描述】:

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

【问题讨论】:

  • 大家好,我忘了:)
  • 嗨 Loic,感谢您的快速回答。我尚未测试您的代码,但是否可以将您的示例调整到我为我创建的一种运输方式创建的运输区域? + 我有很多邮政编码要添加,而不仅仅是一个...非常感谢您的帮助!
  • 嗨,Loic,我不明白,我看不到你的第一个答案?
  • 对不起,我想你已经删除了你的答案……你能再发一次吗?
  • 我已经做出了不同的回答……更方便……如果用户更改送货区域,则在结帐时使用 woocommerce 通知将不起作用。

标签: php wordpress woocommerce shipping notice


【解决方案1】:

使用上面@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 语句。但是,此代码段在部署之前已被删除,以避免客户在屏幕上出现随机字符。

【讨论】:

    【解决方案2】:

    更新

    根据运输区域名称(有邮政编码限制)尝试以下代码,它将在运输总行上显示您的消息(但不会生成 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 文件中。经过测试并且可以工作。

    【讨论】:

      猜你喜欢
      • 2021-04-24
      • 2021-02-08
      • 1970-01-01
      • 2019-03-28
      • 1970-01-01
      • 2021-06-07
      • 1970-01-01
      • 2018-09-17
      • 1970-01-01
      相关资源
      最近更新 更多