wordpress - 重新排列(按优先级排序)Woocommerce 我的帐户字段

标签 wordpress plugins woocommerce hook-woocommerce account

如何重新排列 my-account 字段。我想在 billing_statebilling_city 之后找到 billing_address_1 字段字段。

我使用了 woocommerce_form_field_args 钩子(Hook),然后使用 switch 来查找每个 arg

function bcod_wc_form_field_args( $args, $key, $value ){
switch ($key) {
        case 'billing_country':
            $args['class'] = array('hidden');
            break;
        case 'billing_address_1' :
            $args['priority'] = '85';// push down this field to bottom of state and city 
            $args['label'] = $args['label'] . ' Priority : ' . $args['priority'];
            $args['placeholder'] = 'خیابان اصلی ، خیابان فرعی ، کوچه ، پلاک';
            break;
        case 'billing_state' :
            $args['priority'] = '50';
            $args['label'] = $args['label'] . ' Priority : ' . $args['priority'];
            $args['class'] = array('form-row-first');
            break;
        case 'billing_city' :
            $args['priority'] = '55';
            $args['label'] = $args['label'] . ' Priority : ' . $args['priority'];
            $args['class'] = array('form-row-last');
            break;
        case 'billing_postcode' :
            $args['label'] = $args['label'] . ' Priority : ' . $args['priority'];
            $args['priority'] = '190';
            $args['class'] = array('form-row-first');
            break;
        case 'billing_email' :
            $args['label'] = $args['label'] . ' Priority : ' . $args['priority'];
            $args['priority'] = '80';
            $args['class'] = array('form-row-last');
            break;
        default:
            return $args;
            break;
        }
    return $args;
}

我期望 billing_address_1 字段位于 statecity 之后,但此代码不会影响字段的排序。优先级已更改,但字段位于默认位置。这张图片是我的结果: the-result-of-my-code
(来源:imgurl.ir)

最佳答案

要重新排列“我的帐户”地址结算字段,您需要使用如下内容:

// Change billing fields location
add_filter( 'woocommerce_billing_fields', 'arrange_billing_fields', 10, 1 );
function arrange_billing_fields( $fields ){
    // Only on my account
    if( is_account_page() ) :

    $fields['billing_country']['class']         = array('hidden');

    $fields['billing_state']['priority']        = '50';
    $fields['billing_state']['label']          .= ' (Priority : '.$fields['billing_state']['priority'].')';
    $fields['billing_state']['class']           = array('form-row-first');

    $fields['billing_city']['priority']         = '55';
    $fields['billing_city']['label']           .= ' (Priority : '.$fields['billing_city']['priority'].')';
    $fields['billing_city']['class']            = array('form-row-last');

    $fields['billing_email']['priority']        = '80';
    $fields['billing_email']['label']          .= ' (Priority : '.$fields['billing_email']['priority'].')';
    $fields['billing_email']['class']           = array('form-row-last');

    $fields['billing_address_1']['priority']    = '85';
    $fields['billing_address_1']['label']      .= ' (Priority : '.$fields['billing_address_1']['priority'].')';
    $fields['billing_address_1']['placeholder'] = 'خیابان اصلی ، خیابان فرعی ، کوچه ، پلاک';

    $fields['billing_postcode']['priority']     = '190';
    $fields['billing_postcode']['label']       .= ' (Priority : '.$fields['billing_postcode']['priority'].')';
    $fields['billing_postcode']['class']        = array('form-row-first');

    endif;

    return $fields;
}

// For the state field priority location for some countries
add_filter( 'woocommerce_get_country_locale', 'custom_country_locale', 10, 1 );
function custom_country_locale( $countries_locale ){
    // Only on my account
    if( is_account_page() ):

    // Loop Through country locale array
    foreach( $countries_locale as $country_code => $fields ){
        // Loop through defined fields (targeting the state field)
        foreach( $fields as $field_key => $data ) {
            if( $field_key == 'state' && isset($data['priority']) ){
                $countries_locale[$country_code][$field_key]['priority'] = '50';
            }
        }
    }

    endif;

    return $countries_locale;
}

代码位于事件子主题(或事件主题)的 function.php 文件中。经过测试并有效。

关于wordpress - 重新排列(按优先级排序)Woocommerce 我的帐户字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54542246/

相关文章:

wordpress - woocommerce Hook 用于改变价格

javascript - 如果没有 stripslashes,json_decode 将无法工作(WordPress 中的 magic_quotes)

mysql - 使用 MySql 在 Wordpress 中插入帖子

jQuery 插件创建和面向公众的方法

java - Eclipse 的免费 UML 工具/插件可以生成 Java 代码吗?

javascript - 删除项目时,如何处理 ul > li 项目未按预期排列

mysql - 如何使用 docker 容器将后端和前端的 WordPress 分开?

javascript - ACF Google map - 标记聚类

plugins - 基本插件 (NPAPI/npruntime) Hello world

jquery - 单击父级时触发单击