从 WooCommerce 中的特定国家位置获取运费 ID
Get the shipping rate IDs from specific country location in WooCommerce
我尝试按 flat_rate:1、local_pickup:2 等国家/地区获取运输 ID 列表。
我在下面试过这个
$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();
}
以下将为您提供您正在寻找的运费 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>';
已测试并可以使用。
将这一行 $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>';
已测试并可以使用
我尝试按 flat_rate:1、local_pickup:2 等国家/地区获取运输 ID 列表。 我在下面试过这个
$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();
}
以下将为您提供您正在寻找的运费 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>';
已测试并可以使用。
将这一行 $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>';
已测试并可以使用