【问题标题】:Reordering checkout fields in WooCommerce 3在 WooCommerce 3 中重新排序结帐字段
【发布时间】:2016-11-10 17:07:55
【问题描述】:

我正在尝试重新排序在 woocommerce_checkout_init 过滤器的帮助下添加的 2 个自定义结帐字段,只是当我将 woocommerce_checkout_fields 过滤器应用于重新排序字段时,它无法识别它们并且它们是 @ 987654324@.
我认为这是因为过滤器woocommerce_checkout_initwoocommerce_checkout_fields 之后。

我该如何解决这个问题?

这是我的代码:

add_action( 'woocommerce_checkout_init', 'wc_add_confirm_email_checkout', 10, 2 );
function wc_add_confirm_email_checkout( $checkout ) {
    $checkout->checkout_fields['billing']['billing_email2'] = array(
        'type'              => 'text',
        'label'             => __( 'Confirm Email Address', 'woocommerce' ),
        'required'          => true,
        'placeholder'       => _x( 'Confirm Email Address', 'placeholder', 'woocommerce' )
    );
}
add_action( 'woocommerce_checkout_init', 'wc_add_confirm_password_checkout', 10, 2 );
function wc_add_confirm_password_checkout( $checkout ) {
    //var_dump($checkout->checkout_fields);
    if ( get_option( 'woocommerce_registration_generate_password' ) == 'no' ) {
        $checkout->checkout_fields['account']['account_password2'] = array(
            'type'              => 'password',
            'label'             => __( 'Confirm password', 'woocommerce' ),
            'required'          => true,
            'placeholder'       => _x( 'Confirm Password', 'placeholder', 'woocommerce' )
        );
    }
}

add_filter('woocommerce_checkout_fields','reorder_woo_fields');
function reorder_woo_fields($fields) {
    $fields2['billing']['billing_first_name'] = $fields['billing']['billing_first_name'];
    $fields2['billing']['billing_last_name'] = $fields['billing']['billing_last_name'];
    $fields2['billing']['billingooglg_email'] = $fields['billing']['billing_email'];
    $fields2['billing']['billing_email2'] = $fields['billing']['billing_email2'];
    $fields2['billing']['account_password'] = $fields['account']['account_password'];
    $fields2['billing']['account_password2'] = $fields['account']['account_password2'];
    $fields2['billing']['billing_address_1'] = $fields['billing']['billing_address_1'];
    $fields2['billing']['billing_postcode'] = $fields['billing']['billing_postcode'];
    var_dump($fields2);
    //return $fields2;
}

【问题讨论】:

  • 任何人有任何想法,感谢投票。
  • @LoicTheAztec 感谢您的帮助,它使重新订购的部分起到了作用,因此用您给我的循环和数组替换了它(根据我的代码删除一些字段)并添加了电子邮件地址并确认我想要的密码。
  • 我好奇的问题:所以有可能将结帐帐单字段与结帐帐户字段融合在一起吗? …如果您想更新我的答案,欢迎您。您可以在最后使用您的代码添加更新。谢谢
  • 啊,不,我将它们分开添加,也许我可以将它们添加到单个数组中,例如帐户数组以将其更改为计费数组。

标签: php wordpress woocommerce field checkout


【解决方案1】:

对于 WooCommerce 3+ (更新)

由于 WooCommerce 3.0 结帐字段发生了一些变化,因此无法像以前那样重新排序字段。

有一个新的'priority'参数处理字段顺序,也适用于结帐字段和我的帐户字段。

这是 WooCommerce 3+ 的完整功能示例:

// REORDERING CHECKOUT BILLING FIELDS (WOOCOMMERCE 3+)
add_filter( "woocommerce_checkout_fields", "reordering_checkout_fields", 15, 1 );
function reordering_checkout_fields( $fields ) {

    ## ---- 1. REORDERING BILLING FIELDS ---- ##

    // Set the order of the fields
    $billing_order = array(
        'billing_first_name',
        'billing_last_name',
        'billing_email',
        'billing_phone',
        'billing_company',
        'billing_address_1',
        'billing_address_2',
        'billing_postcode',
        'billing_city',
        'billing_state',
        'billing_country'
    );

    $count = 0;
    $priority = 10;

    // Updating the 'priority' argument
    foreach($billing_order as $field_name){
        $count++;
        $fields['billing'][$field_name]['priority'] = $count * $priority;
    }

    ## ---- 2. CHANGING SOME CLASSES FOR BILLING FIELDS ---- ##

    $fields['billing']['billing_email']['class'] = array('form-row-first');
    $fields['billing']['billing_phone']['class'] = array('form-row-last');

    $fields['billing']['billing_postcode']['class'] = array('form-row-first');
    $fields['billing']['billing_city']['class'] = array('form-row-last');
    
    ## ---- RETURN THE BILLING FIELDS CUSTOMIZED ---- ##

    return $fields;
}

代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。


在 WooCommerce 3 之前

我不完全确定,但有些事情是您无法完成的,例如将帐单字段与帐户字段合并。如果您想这样做,那将比您在这里尝试做的要复杂得多。在这种情况下,您将需要重写/创建一些结帐模板……

另一件事是 billing_emailbilling_phone'class' => 'form-row-first''class' => 'form-row-last' 共享同一行。当这个类是定义'class' => 'form-row-wide'...所以你也需要覆盖这些'class'

之后就不需要使用'woocommerce_checkout_init'钩子了……
您仍然可以使用 'woocommerce_checkout_fields'
您也可以通过这种方式将所有这些合并到一个函数中:

/*
 * Creating, overriding and reordering custom fields.
 */
add_filter( "woocommerce_checkout_fields", "custom_override_checkout_fields", 11, 1 );
function custom_override_checkout_fields( $fields ) {

    // Creating 'billing_email2' field
    $fields['billing']['billing_email2'] = array(
        'type'          => 'text',
        'label'         => __( 'Confirm Email Address', 'woocommerce' ),
        'placeholder'   => _x( 'Confirm Email Address', 'placeholder', 'woocommerce' ),
        'required'      => true,
        'class'         => array('form-row-last'),
        'clear'         => true
    );

    // =======> I don't really know if you need this one  <========
    // it already exist (see in first reference link at bottom).

    // Creating 'account_password2' field 
    if ( get_option( 'woocommerce_registration_generate_password' ) == 'no' ) {
        $fields['account']['account_password2'] = array(
            'type'          => 'password',
            'label'         => __( 'Confirm password', 'woocommerce' ),
            'placeholder'   => _x( 'Confirm Password', 'placeholder', 'woocommerce' ),
            'required'      => true,
            'class'         => array('form-row-wide') //,
            // 'clear'         => true
        );
    }

    // Overriding existing billing_phone field 'class' property 
    $fields['billing']['billing_phone']['class'] = array('form-row-wide');


    // Reordering billing fields
    $order = array(
        "billing_first_name",
        "billing_last_name",
        "billing_email",
        "billing_email2",
        "billing_phone",
        "billing_company",
        "billing_address_1",
        "billing_address_2",
        "billing_postcode",
        "billing_country"
    );

    foreach($order as $field)
    {
        $ordered_fields[$field] = $fields["billing"][$field];
    }

    $fields["billing"] = $ordered_fields;

    return $fields;
}

代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。

正如我之前所说,我认为您不能将帐单字段与帐户字段合并。
由于 'account_password2' 已经存在,如果您参考官方文档(请参阅下面的第一个参考链接),您可能不需要创建它。您将不得不对此进行测试并对其进行微调。但这就是这样做的方法。


参考资料:

【讨论】:

  • 感谢答案,还有一件事,如果我想添加一个新部分,请说您的详细信息,包括姓名、电子邮件、电话和国家/地区等字段,我是否需要编辑 form-checkout.php 或是否可以通过函数和钩子实现。
  • @user3467855 不确定……但在结帐模板中,您只有 4 个字段部分:“帐单”、“运输”、“帐户”和“备注”。这也取决于你想在哪里拥有它。您不能有 2 次相同的字段(例如,一个在计费部分,另一个时间在另一个部分。姓名、电子邮件和电话已经存在……这就是问题所在……
  • @LoicTheAztec 嗨,我尝试使用此代码重新排序结帐字段。但我有同样的问题,当页面完全上传时,有些提交“jumb”。此视频中的示例:loom.com/share/82037629405440eea9ddf3115088e279 请帮我解决这个问题,我真的不明白会发生什么。
  • 您可以稍微修改代码以编程方式获取字段,并且只将新字段放在您想要的任何确切位置。其中 'billing_email2' 可以放在 first_name 之前。优先级参数如何覆盖这种重新排序?
【解决方案2】:

注意!

Woocommerce 进行了一些更改,对于安排地址字段,您应该使用“woocommerce_default_address_fields”过滤器。

add_filter( 'woocommerce_default_address_fields', 'custom_override_default_locale_fields' );
function custom_override_default_locale_fields( $fields ) {
    $fields['state']['priority'] = 5;
    $fields['address_1']['priority'] = 6;
    $fields['address_2']['priority'] = 7;
    return $fields;
}

Reorder Woocommerce checkout fields

【讨论】:

    【解决方案3】:

    添加到已接受的答案...

    如果您需要将表单字段从一个字段组移动到另一个字段组,例如将运输字段移动到帐单中或将帐户字段移动到帐单中,您可以执行以下操作。

    function move_password_field($checkout_fields){
        // Move Account Password into Billing
        $checkout_fields['billing']['account_password'] = $checkout_fields['account']['account_password']; 
    
        // Remove Password from Billing
        unset($checkout_fields['account']['account_password']);
    
        return $checkout_fields;
    }
    
    add_filter('woocommerce_checkout_fields', 'move_password_field', 999);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-30
      • 2015-08-26
      • 2019-12-28
      • 2021-08-16
      • 2019-06-29
      • 2017-01-09
      • 2018-12-20
      • 2015-04-20
      相关资源
      最近更新 更多