首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >wordpress插件开发中在woocommerce中使用custome区域

wordpress插件开发中在woocommerce中使用custome区域
EN

Stack Overflow用户
提问于 2021-04-09 19:03:02
回答 1查看 55关注 0票数 2

所以这是我的密码

代码语言:javascript
复制
function crudStatesOperations() {
        global $wpdb;
        $country_code = 'no';

        $user_id = get_current_user_id();
        $table_name = $wpdb->prefix . 'states';
        if (isset($_POST['newsubmit'])) {
         add_filter('woocommerce_states', function ($states)  {

                $states['BB'] = array(
        'BB001' => 'St. George',
        'BB002' => 'St. Michael',
        'BB003' => 'Christ Church',
        'BB004' => 'St. Philip',
        'BB005' => 'St. Lucy',
        'BB006' => 'St. Joseph',
        'BB007' => 'St. Peter',
        'BB008' => 'St. James',
        'BB009' => 'St. Thomas',
        'BB0010' => 'St. John',
        'BB0011' => 'St. Andrew',
    );

                return $states;

            });
        }
}

和add_filter(‘woocommerce_state’,'woo_custom_shipping_zones‘);它在我的函数中不起作用,但是如果我把它放在外部并重新激活它的工作

如何在函数内部使用此过滤器?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-09 19:41:00

您可以使用来解决问题:

代码语言:javascript
复制
// if the "$_POST['newsubmit']" field is set, update the "add_custom_states_woocommerce" option
function crudStatesOperations() {
    global $wpdb;
    $country_code = 'no';
    $user_id = get_current_user_id();
    $table_name = $wpdb->prefix . 'states';
    if ( isset( $_POST['newsubmit'] ) ) {
        update_option( 'add_custom_states_woocommerce', true );
    }
}

// adds custom states based on "add_custom_states_woocommerce" option
add_filter( 'woocommerce_states', 'woo_custom_shipping_zones', 10, 1 );
function woo_custom_shipping_zones( $states ) {

    if ( get_option( 'add_custom_states_woocommerce' ) ) {
        $states['BB'] = array(
            'BB001'  => 'St. George',
            'BB002'  => 'St. Michael',
            'BB003'  => 'Christ Church',
            'BB004'  => 'St. Philip',
            'BB005'  => 'St. Lucy',
            'BB006'  => 'St. Joseph',
            'BB007'  => 'St. Peter',
            'BB008'  => 'St. James',
            'BB009'  => 'St. Thomas',
            'BB0010' => 'St. John',
            'BB0011' => 'St. Andrew',
        );
    }

    return $states;

}

密码应该能用。将其添加到活动主题的functions.php中。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67026896

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档