在WooCommerce中显示基于客户邮政编码的自定义邮件

在WooCommerce中显示基于客户邮政编码的自定义邮件,woocommerce,message,checkout,Woocommerce,Message,Checkout,我正在尝试使用应答码在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_notic

我正在尝试使用应答码在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>';
    }
}
add_action('woocommerce_cart_totals_在_shipping之后,'shipping_zone_targeted_postcodes_custom_notice');
添加操作('woocommerce\u review\u order\u after\u shipping'、'shipping\u zone\u targeted\u Postcode\u custom\u notice');
功能发货\区域\目标\邮政编码\自定义\通知(){
//在此定义您的装运区名称
$targeted_zones_names=array('France');//客户->获取_shipping_postcode()。'
) . '
';
}
}
要对代码进行哪些修改


有人能帮我做一下调整吗?

对于一组已定义的邮政编码,请使用以下内容(定义一组目标邮政编码):

add_action('woocommerce_cart_totals_在_shipping之后,'shipping_zone_targeted_postcodes_custom_notice');
添加操作('woocommerce\u review\u order\u after\u shipping'、'shipping\u zone\u targeted\u Postcode\u custom\u notice');
功能发货\区域\目标\邮政编码\自定义\通知(){
//在这里定义您的邮政编码
$targeted_postcodes=数组('89000'、'89200'、'89300');
//获取客户的邮政编码
$customer_postcode=WC()->customer->get_shipping_postcode();
$customer\u postcode=!empty($customer\u postcode)?WC()->customer->get\u billing\u postcode():$customer\u postcode;;
if(在数组中($customer\u postcode,$targeted\u postcodes)){
回声'
斯普林特先生(
__(“对于%s邮政编码,您将多收取%s费用”,“woocommerce”),
“”。wc_价格(100)。”,
“。$customer\u邮政编码。””
) . '
';
}
}

代码进入活动子主题(或活动主题)的functions.php文件。已测试且有效。

谢谢您的回复,如果我要添加的邮政编码约为150,这是否重要?我是否用逗号分隔,这是否会产生一个弹出窗口来确认为什么价格会上涨100英镑?再次感谢您的replyNo问题,您将在阵列中添加150个邮政编码。它将在totals表中显示一条消息(您可以使用一些CSS突出显示该消息)…我已更新代码,显示价格上涨时的100英镑。如果这个答案回答了你的问题,你可以请你回答,谢谢。你好,很抱歉这个项目已经贬值到另一个项目,这是现在的优先事项了。这将是产品范围内的还是与选定的产品,我需要定义这些产品,如果我不希望它在每一个产品?非常感谢。
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 POSTCODES
    $targeted_postcodes = array('89000','89200','89300');

    // Get the customer postcode
    $customer_postcode = WC()->customer->get_shipping_postcode();
    $customer_postcode = ! empty($customer_postcode) ? WC()->customer->get_billing_postcode() : $customer_postcode;;

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