首页 > 解决方案 > WooCommerce 输入选择不采用默认值

问题描述

使用下面的代码,在结帐页面上。圣地亚哥已经被选中,这不是我想要的。我希望默认设置在Seleccione Destino上。

奇怪的是,当我将圣地亚哥的数字从 1 更改为 44 之类的其他数字时,它可以正常工作,它选择 0 作为默认值。当 Santiago 为 1 时,默认值为 Santiago。

我也尝试过设置默认值 1287,但它仍然首先将 1 作为默认值。

add_filter( 'woocommerce_billing_fields', 'add_field', 20, 1 );
function add_field($billing_fields) {

    $billing_fields['billing_area'] = array(
        'label'     => __('Ubicación del Destino', 'woocommerce'),
        'required'  => true,
        'type'          => 'select',
        'default'           => 0,
        'class'     => array('select'),
        'priority' => 65,
        'options'       => array(
            0 => 'Seleccione Ubicación',  1 => 'SANTIAGO',  1287 => 'ACHAO'
       )
    );
    return $billing_fields;
}

标签: phpwordpresswoocommercehook-woocommerce

解决方案


这应该可以解决问题

function add_field($billing_fields) {

    $billing_fields['billing_area'] = array(
        'label'    => __('Ubicación del Destino', 'woocommerce'),
        'required' => true,
        'type'     => 'select',
        'default'  => 'seleccione_ubicación',
        'class'    => array('form-row-wide'),
        'priority' => 65,
        'options'  => array(
            'seleccione_ubicación' => 'Seleccione Ubicación',
            'santiago' => 'SANTIAGO',
            'achao' => 'ACHAO'
       )
    );

    return $billing_fields;
}
add_filter( 'woocommerce_billing_fields', 'add_field', 10, 1 );

推荐阅读