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

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

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

添加字段正确,但是,该字段添加在电话字段下面的末尾,我需要在第二个位置添加此自定义字段,在街道地址字段下面。


解决方案

您需要使用优先级参数来设置签出字段的正确位置,如:

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

它应该可以工作。

相关文章