在WooCommerce中检索客户可用的运输方式详细信息



如何检索最终用户可见的运输方式?(并非Woocommerce中定义的所有运输方式(。我正在使用"通过绘制WooCommerce的运输区域"插件。我需要在商店周围使用多个半径。问题是我有多个,这个插件只会隐藏用户位于它外面的半径,并显示其余的(我只需要显示其中一个,最便宜的(。

我尝试打印woocommerce_package_ratesWC()->session的费率,但这些将显示定义的所有运输方式,包括未向用户显示的运输方式。

要在定义送货地点和购物车不为空时获取客户可用的运输方式,您可以使用:

// Get shipping packages
$shipping_packages = WC()->cart->get_shipping_packages();
foreach( array_keys( $shipping_packages ) as $key ) {
if( $shipping_for_package = WC()->session->get('shipping_for_package_'.$key) ) {
if( isset($shipping_for_package['rates']) ) {
// Loop through customer available shipping methods
foreach ( $shipping_for_package['rates'] as $rate_key => $rate ) {
$rate_id = $rate->id; // the shipping method rate ID (or $rate_key)
$method_id = $rate->method_id; // the shipping method label
$instance_id = $rate->instance_id; // The instance ID
$cost = $rate->label; // The cost
$label = $rate->label; // The label name
$taxes = $rate->taxes; // The taxes (array)
print_pr($label);
}
}
}
}

现在要获得所选的运输方式,您将使用:

WC()->session->get('chosen_shipping_methods'); // (Array)

最新更新