首页 > 解决方案 > 在 Woocommerce 中的特定国家/地区的购物车和结帐总额后显示文本

问题描述

我需要在价格后的 Woocommerce 订单总数单元格中显示一条消息,但前提是客户的国家/地区是美国或加拿大。我有这个

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

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

标签: phpwordpresswoocommercecartcountry

解决方案


如果您正在使用 Geolocation 函数并有一个 API 密钥,则可以使用WC_Geolocation该类。

add_filter( 'woocommerce_cart_totals_order_total_html', 'custom_total_message_html', 10, 1 );
function custom_total_message_html( $value ) {
    $geolocation = WC_Geolocation::geolocate_ip();
    if (in_array($geolocation['country'], array('US', 'CA'))){
        $value .= __('<small>My text.</small>');
    }
    return $value;
}

推荐阅读