WooCommerce将城市州显示为下拉列表

时间:2016-02-24 作者:Josh Jimenez

当用户在woocommerce上选择一个州时,我试图显示一个城市列表,有些人很幸运能做到这一点??

我发现以下代码:

add_filter( \'woocommerce_checkout_fields\' , \'override_checkout_fields\' );

// Our hooked in function - $fields is passed via the filter!
function override_checkout_fields( $fields ) {
     unset($fields[\'billing\'][\'billing_city\']);
     unset($fields[\'shipping\'][\'shipping_city\']);
     return $fields;
}

add_filter( \'woocommerce_checkout_fields\' , \'custom_override_checkout_fields\' );

// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields( $fields ) {
     $fields[\'billing\'][\'billing_ciudad\']= array(
    \'type\' => \'select\',
    \'label\'     => __(\'Ciudad\', \'woocommerce\'),
    \'placeholder\'   => _x(\'San Salvador\', \'placeholder\', \'woocommerce\'),
    \'required\'  => true,
    \'class\'     => array(\'form-row-wide\'),
    \'clear\'     => true,
     );


     $fields[\'billing\'][\'billing_ciudad\'][\'options\'] = array(
        \'option_1\' => \'Col. Escalon\',
        \'option_2\' => \'Otro ciudad o colonia\'
        );


     return $fields;
}
我试图只在选择一个州时显示此字段,并显示每个州的特定城市。

1 个回复
SO网友:Crintus

我使用woocommerce_default_address_fields 滤器

您可以按照以下步骤创建新的城市:

// UPDATE CHECKOUT CITY FIELD
add_filter( \'woocommerce_default_address_fields\' , \'custom_override_default_address_fields\' );

// Our hooked in function - $address_fields is passed via the filter!
function custom_override_default_address_fields( $address_fields ) {

    // Billing City
    $address_fields[\'city\'][\'type\'] = \'select\';
    $address_fields[\'city\'][\'label\'] = \'Suburb\';

    $address_fields[\'city\'][\'options\'] = array(
        \'\' => \'\',
        \'bo_kaap\' => \'Bo-Kaap\',
        \'devils_peak_estate\' => \'Devil\\\'s Peak Estate\',
        \'de_waterkant\' => \'De Waterkant\',
        \'foreshore\' => \'Foreshore\'
    );

    // Sort
    ksort($address_fields[\'city\'][\'options\']);

    // to be added last and not sorted
    $address_fields[\'city\'][\'options\'][\'other\'] = \'Other\';

    return $address_fields;
}
然后使用javascript检测何时选择了您的州,并显示您刚刚创建的城市下拉列表。我希望这是你一直在寻找的,我想我会分享