1

我正在尝试根据 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 不返回此数据),我需要在这里找出运输国家和运输州/省 - 有没有办法做到这一点?

4

1 回答 1

3

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

因此,您询问了客户发货国家/地区并声明您可以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_CustomerObject 中设置新的国家和州。

于 2019-05-21T19:09:25.020 回答