从 Woocommerce 中的方法费率 ID 获取运输方法标题
Get the shipping method title from its method rate ID in Woocommerce
我有我的自定义函数,它检索每个运输方式 ID 的 ID(例如 flat_rate:3
)
我需要通过id获取方法标题(如下图,我需要:
Fast Shipping
根据文档,我找到了 WC_Shipping_Method::get_method_title()
但我无法通过 id 检索。
还看到运输方式标签存储在 wp_options
table 和 option_id
中,所以无法执行 SQL 请求。
有什么办法吗?
更新2 (简化功能代码)
要从其完整 ID(方法费率 ID)中检索运输方式标题,可以使用简单轻量级的自定义函数来完成:
function get_title_shipping_method_from_method_id( $method_rate_id = '' ){
if( ! empty( $method_rate_id ) ){
$method_key_id = str_replace( ':', '_', $method_rate_id ); // Formating
$option_name = 'woocommerce_'.$method_key_id.'_settings'; // Get the complete option slug
return get_option( $option_name, true )['title']; // Get the title and return it
} else {
return false;
}
}
代码进入您的活动 child 主题(或主题)的 function.php 文件或任何插件文件。
已测试并有效。
用法示例 (输出运输方式标题):
// Define the Shipping Method rate ID example
$method_rate_id = 'flat_rate:3';
// Get the title and display it
$title = get_title_shipping_method_from_method_id( $method_rate_id );
echo '<p>' . $title . '</p>';
它将显示您的自定义送货方式标题
我有我的自定义函数,它检索每个运输方式 ID 的 ID(例如 flat_rate:3
)
我需要通过id获取方法标题(如下图,我需要:
Fast Shipping
根据文档,我找到了 WC_Shipping_Method::get_method_title()
但我无法通过 id 检索。
还看到运输方式标签存储在 wp_options
table 和 option_id
中,所以无法执行 SQL 请求。
有什么办法吗?
更新2 (简化功能代码)
要从其完整 ID(方法费率 ID)中检索运输方式标题,可以使用简单轻量级的自定义函数来完成:
function get_title_shipping_method_from_method_id( $method_rate_id = '' ){
if( ! empty( $method_rate_id ) ){
$method_key_id = str_replace( ':', '_', $method_rate_id ); // Formating
$option_name = 'woocommerce_'.$method_key_id.'_settings'; // Get the complete option slug
return get_option( $option_name, true )['title']; // Get the title and return it
} else {
return false;
}
}
代码进入您的活动 child 主题(或主题)的 function.php 文件或任何插件文件。
已测试并有效。
用法示例 (输出运输方式标题):
// Define the Shipping Method rate ID example
$method_rate_id = 'flat_rate:3';
// Get the title and display it
$title = get_title_shipping_method_from_method_id( $method_rate_id );
echo '<p>' . $title . '</p>';
它将显示您的自定义送货方式标题