【问题标题】:Get the shipping method title from its method rate ID in Woocommerce从 Woocommerce 中的方法费率 ID 获取运输方法标题
【发布时间】:2018-05-08 16:54:55
【问题描述】:

我有我的自定义函数,它检索每个运输方式 ID 的 ID(例如 flat_rate:3

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

快速发货

根据文档,我找到了WC_Shipping_Method::get_method_title(),但我无法通过 id 检索。

还看到运输方式标签与option_id存储在wp_options表中,因此无法执行SQL请求。

有什么办法吗?

【问题讨论】:

    标签: php sql wordpress woocommerce shipping


    【解决方案1】:

    更新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;
        }
    }
    

    代码进入您的活动子主题(或主题)的 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>';
    

    它将显示您的自定义运输方式标题

    【讨论】:

    • 嗨,我已经尝试过这个 SQL 请求,但不是我做的。我将编辑我的问题
    • 好的,谢谢,我会尽快与您联系。我把它放在这里让其他人也找到了
    • 哦,我不认为我们可以使用 get_option() 这样的函数!谢谢@LoicTheAztec
    猜你喜欢
    • 2018-05-05
    • 2017-11-09
    • 1970-01-01
    • 1970-01-01
    • 2019-02-11
    • 1970-01-01
    • 2019-08-04
    • 1970-01-01
    • 2014-11-18
    相关资源
    最近更新 更多