从Woocommerce中的方法费率ID获取送货方法标题

Efb*_*fbi 1 php sql wordpress shipping woocommerce

我有我的自定义函数,它检索每个送货方式ID的id(ex flat_rate:3)

我需要通过id检索方法标题(如下图所示,我需要:

快速送货

运输方式标题

根据文档,我发现WC_Shipping_Method::get_method_title()但我无法通过id检索.

还看到发货方法标签存储在wp_options表中option_id,因此无法管理做SQL请求.

有什么办法吗?

Loi*_*tec 5

更新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;
    }
}
Run Code Online (Sandbox Code Playgroud)

代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中.

经过测试和工作.


用法示例 (输出Shipping方法标题):

// 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>';
Run Code Online (Sandbox Code Playgroud)

它将显示您的自定义送货方式标题