問(wèn)題描述
在 woocommerce 中,我需要根據(jù)送貨區(qū)域在購(gòu)物車(chē)或結(jié)帳頁(yè)面上顯示自定義消息,例如您將為此郵政編碼多支付 10% 的費(fèi)用".
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".
我覺(jué)得這很容易,但我不能讓它工作!它讓我發(fā)瘋!任何幫助表示贊賞.
I feel its easy but I can't get it work ! And its drive me crazy! Any help is appreciated.
我的解決方法是自定義那種默認(rèn)消息:
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;
}
推薦答案
更新
根據(jù)運(yùn)輸區(qū)域名稱(chēng)(有郵政編碼限制)嘗試以下代碼,該代碼將在運(yùn)輸總行上顯示您的消息(但不會(huì)生成 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>';
}
}
代碼位于活動(dòng)子主題(或活動(dòng)主題)的 function.php 文件中.經(jīng)測(cè)試有效.
Code goes in function.php file of your active child theme (or active theme). Tested and works.
這篇關(guān)于在 Woocommerce 中顯示基于客戶送貨區(qū)域的自定義消息的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!