我尝试按国家/地区获取运输 ID 列表,例如 flat_rate:1、local_pickup:2 等。 我在下面试过这个
$all_zones = WC_Shipping_Zones::get_zones();
$country_code = 'BE';
foreach ($all_zones as $zone) {
foreach ($zone['zone_locations'] as $location) {
if ($country_code === $location->code) {
foreach ($zone['shipping_methods'] as $flat_rate) {
print_r($flat_rate);//ID here
}
}
}
}
我也试过这个,但是我不能自己设置一个国家?
$shippingmethods = WC()->session->get( 'shipping_for_package_0')['rates'];
foreach ($shippingmethods as $shippingmethod ) {
shippingmethod->get_id();
}
答案 0 :(得分:2)
以下将为您提供您正在寻找的运费 ID……它适用于所有定义的运输区域以及世界其他地区的运输区域(当没有匹配的国家/地区时)。
代码:
// $country_code = WC()->customer->get_shipping_country();
$country_code = 'BE';
$defined_zones = WC_Shipping_Zones::get_zones();
$shipping_rate_ids = array(); // Initializing
$country_found = false;
// Loop through defined shipping zones
foreach ($defined_zones as $zone) {
foreach ($zone['zone_locations'] as $location ) {
if ( 'country' === $location->type && $country_code === $location->code ) {
foreach ($zone['shipping_methods'] as $shipping_method ) {
$method_id = $shipping_method->id;
$instance_id = $shipping_method->instance_id;
$rate_id = $method_id . ':' . $instance_id;
$shipping_rate_ids[$instance_id] = $rate_id;
}
$country_found = true;
break; // Country found stop "locations" loop
}
}
}
// Rest of the word (shipping zone)
if( ! $country_found ) {
$zone = new \WC_Shipping_Zone(0); // Rest of the word (zone)
foreach ( $zone->get_shipping_methods( true, 'values' ) as $shipping_method ) {
$method_id = $shipping_method->id;
$instance_id = $shipping_method->instance_id;
$rate_id = $method_id . ':' . $instance_id;
$shipping_rate_ids[$instance_id] = $rate_id;
}
}
// testing raw output
echo '<pre>' . print_r( $shipping_rate_ids, true ) . '</pre>';
经过测试和工作。
答案 1 :(得分:1)
将此行 $method_id = $shipping_method->method_id; 更改为 $shipping_method->id;
这解决了问题注意:未定义的属性:WC_Shipping_Flat_Rate::$method_id
// $country_code = WC()->customer->get_shipping_country();
$country_code = 'NL';
$defined_zones = WC_Shipping_Zones::get_zones();
$shipping_rate_ids = array(); // Initializing
$country_found = false;
// Loop through defined shipping zones
foreach ($defined_zones as $zone) {
foreach ($zone['zone_locations'] as $location ) {
if ( 'country' === $location->type && $country_code === $location->code ) {
foreach ($zone['shipping_methods'] as $shipping_method ) {
$method_id = $shipping_method->id;
$instance_id = $shipping_method->instance_id;
$rate_id = $method_id . ':' . $instance_id;
$shipping_rate_ids[$instance_id] = $rate_id;
}
$country_found = true;
break; // Country found stop "locations" loop
}
}
}
// Rest of the word (shipping zone)
if( ! $country_found ) {
$zone = new \WC_Shipping_Zone(0); // Rest of the word (zone)
foreach ( $zone->get_shipping_methods( true, 'values' ) as $shipping_method ) {
$method_id = $shipping_method->id;
$instance_id = $shipping_method->instance_id;
$rate_id = $method_id . ':' . $instance_id;
$shipping_rate_ids[$instance_id] = $rate_id;
}
}
echo '<pre>' . print_r( $shipping_rate_ids, true ) . '</pre>';
经过测试和工作