如何在WooCommerce的特定位置添加自定义结帐字段

时间:2020-10-23 23:03:15

标签: php wordpress woocommerce checkout custom-fields

我在结帐时使用以下代码添加了自定义地址字段:

add_filter( 'woocommerce_checkout_fields' , 'checkout_address_details_fields' );

// Our hooked in function – $fields is passed via the filter!
function checkout_address_details_fields( $fields ) {
    $fields['shipping']['shipping_address_details'] = array(
        'label'     => __('Añade más detalles a tu dirección', 'woocommerce'),
    'placeholder'   => _x('Bloque X Apartemento XXX (Opcional)', 'placeholder', 'woocommerce'),
    'required'  => false,
    'class'     => array('form-row-wide'),
    'clear'     => true
     );

     $fields['billing']['billing_address_details'] = array(
        'label'     => __('Añade más detalles a tu dirección', 'woocommerce'),
    'placeholder'   => _x('Bloque X Apartamento XXX (Opcional)', 'placeholder', 'woocommerce'),
    'required'  => false,
    'class'     => array('form-row-wide'),
    'clear'     => true
    );

     return $fields;
}

它正确添加了字段,但是,该字段被添加到了电话字段的末尾,我需要将此自定义字段添加到第二个位置,即“街道地址”字段下方。

1 个答案:

答案 0 :(得分:1)

您需要使用“ priority” 参数设置结帐字段的正确位置,例如:

add_filter( 'woocommerce_checkout_fields' , 'checkout_address_details_fields' );
function checkout_address_details_fields( $fields ) {
    $fields['shipping']['shipping_address_details'] = array(
        'label'       => __('Añade más detalles a tu dirección', 'woocommerce'),
        'placeholder' => _x('Bloque X Apartemento XXX (Opcional)', 'placeholder', 'woocommerce'),
        'required'    => false,
        'class'       => array('form-row-wide'),
        'clear'       => true,
        'priority'    => 55, // <===== Here
    );

    $fields['billing']['billing_address_details'] = array(
        'label'       => __('Añade más detalles a tu dirección', 'woocommerce'),
        'placeholder' => _x('Bloque X Apartamento XXX (Opcional)', 'placeholder', 'woocommerce'),
        'required'    => false,
        'class'       => array('form-row-wide'),
        'clear'       => true,
        'priority'    => 55, // <===== Here
    );

    return $fields;
}

应该可以。