【问题标题】:Display a text after cart and checkout totals for specific countries in Woocommerce在 Woocommerce 中的特定国家/地区的购物车和结帐总额后显示文本
【发布时间】:2021-01-02 17:09:19
【问题描述】:

我需要在价格后的 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;
}

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

【问题讨论】:

    标签: php wordpress woocommerce cart country


    【解决方案1】:

    要在您的代码中定位特定国家/地区,您将使用 WC_Customer get_shipping_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;
    }
    

    代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。

    【讨论】:

    • 对不起,我刚刚意识到我忘了提到它也应该显示在订单电子邮件的总数之后 - 可以调整 sn-p 吗?
    • @hara55...首先在堆栈溢出答案中搜索类似问题...然后如果找不到正确的方法,您应该提出一个新问题。
    • 我做了很多研究......无法让它发挥作用。我又开了一个问题:stackoverflow.com/questions/65581987/…
    【解决方案2】:

    如果您正在使用 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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-10
      • 1970-01-01
      • 2021-08-04
      • 1970-01-01
      相关资源
      最近更新 更多