【问题标题】:Get live customer shipping location on WooCommerce checkout page在 WooCommerce 结帐页面上获取实时客户发货位置
【发布时间】:2019-10-08 03:18:48
【问题描述】:

我正在尝试在结帐页面上的 WooCommerce 内的运费下为订单添加预计交货时间。

我已经弄清楚了如何做到这一点,例如:

add_action( 'woocommerce_after_shipping_rate', 'blm_action_after_shipping_rate', 20, 2 );

function blm_action_after_shipping_rate ( $method, $index ) {
    if( is_cart() ) {
        return; // Exit on cart page
    }

    // Use $method->method_id in here to check delivery option

}

现在,为了能够估计天数(不幸的是运输 API 不返回此数据),我需要在这里找出运输国家和运输州/省 - 有没有办法做到这一点?

【问题讨论】:

  • 是客户发货国家和州还是发货区国家和州?
  • 将是他们将商品运送到的那个(与计算运费相同的那个)。
  • 所以 WC_Customer 对象给出了你想要的……然后回答……

标签: php wordpress woocommerce hook-woocommerce shipping-method


【解决方案1】:

运费是根据客户送货地点计算的。

所以您询问了客户发货国家/地区并声明您可以使用以下专用方法从WC_Customer 对象获取:

add_action( 'woocommerce_after_shipping_rate', 'blm_action_after_shipping_rate', 20, 2 );
function blm_action_after_shipping_rate ( $method, $index ) {
    if( is_cart() ) {
        return; // Exit on cart page
    }

    $shipping_country = WC()->customer->get_shipping_country();
    $shipping_state   = WC()->customer->get_shipping_state();

    // Testing output
    echo '<br><small>Country code:' . $shipping_country . ' | State code: ' . $shipping_state . '</small>';
}

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

如果客户更改了国家和州,则刷新数据并在WC_Customer Object 中设置新的国家和州。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-08
    • 2021-11-12
    • 1970-01-01
    • 2020-06-08
    • 1970-01-01
    • 1970-01-01
    • 2016-09-05
    • 2015-01-17
    相关资源
    最近更新 更多