Php 在Woocommerce中特定国家/地区的购物车和结帐总计后显示文本

Php 在Woocommerce中特定国家/地区的购物车和结帐总计后显示文本,php,wordpress,woocommerce,cart,country,Php,Wordpress,Woocommerce,Cart,Country,我需要在价格后的Woocommerce order total单元格中显示一条消息,但仅当客户的国家是美国或加拿大时。我有这个 add_filter( 'woocommerce_cart_totals_order_total_html', 'custom_total_message_html', 10, 1 ); function custom_total_message_html( $value ) { $value .= __('<small>My text.</s

我需要在价格后的Woocommerce order total单元格中显示一条消息,但仅当客户的国家是美国或加拿大时。我有这个

add_filter( 'woocommerce_cart_totals_order_total_html', 'custom_total_message_html', 10, 1 );
function custom_total_message_html( $value ) {
    $value .= __('<small>My text.</small>');

return $value;
}
add_filter('woocommerce_cart_totals_order_total_html','custom_total_message_html',10,1);
函数自定义\总\消息\ html($value){
$value.=_;(“我的文本”);
返回$value;
}

如果客户的国家是美国或加拿大,我如何才能添加文本?

如果您使用的是地理定位函数,并且有API密钥,则可以使用
WC\u Geolocation

add_filter('woocommerce_cart_totals_order_total_html','custom_total_message_html',10,1);
函数自定义\总\消息\ html($value){
$geolocation=WC_geolocation::geolocation_ip();
如果(在数组中($geolocation['country',数组('US','CA')){
$value.=_;(“我的文本”);
}
返回$value;
}

要在代码中针对特定国家/地区,您将使用WC\u Customer
get\u shipping\u country()
方法(对未登录的用户使用地理位置国家/地区),如下所示:

add_filter( 'woocommerce_cart_totals_order_total_html', 'custom_total_message_html', 10, 1 );
function custom_total_message_html( $value ) {
    if( in_array( WC()->customer->get_shipping_country(), array('US', 'CA') ) ) {
        $value .= '<small>' . __('My text.', 'woocommerce') . '</small>';
    }
    return $value;
}
add_filter('woocommerce_cart_totals_order_total_html','custom_total_message_html',10,1);
函数自定义\总\消息\ html($value){
如果(在数组中(WC()->customer->get\u shipping\u country(),数组('US','CA')){
$value.=''.'我的文字','woocommerce'';
}
返回$value;
}

代码进入活动子主题(或活动主题)的functions.php文件。测试和工作。

抱歉,我刚刚意识到我忘了提到它也应该在总数之后显示,以便发送电子邮件-代码片段可以被改编吗?@hara55…首先在堆栈溢出中搜索一点类似问题的答案…然后如果你没有找到正确的方法,你应该问一个新问题。我做了很多研究…无法让它工作。我提出了另一个问题: