在Woocomemrce结帐时,我有2个默认选择字段:billing_state和billing_city
当我选择billing_state时,我需要仅使用其子项填充billing_city。我使用select2插件来选择项目。
例如,当我选择意大利时,我想在billing_city只出现罗马和米兰。
<select name="billing_state" id="billing_state">
<option value="1">USA</option>
<option value="2">Italy</option>
</select>
<select name="billing_city" id="billing_city">
<option value="1" data-parent="1">New York</option>
<option value="2" data-parent="1">Washington</option>
<option value="3" data-parent="2">Rome</option>
<option value="4" data-parent="2">Milan</option>
</select>
对于select字段,我从自定义表中传递了带有id =&gt;名称的php $ array_cities,但我不知道如何传递data-parent =“”属性来仅选择那些带有jQuery的子项。
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
$fields['billing']['billing_city'] = array(
'label' => 'Cities',
'placeholder' => false,
'required' => false,
'class' => array('form-row-first'),
'clear' => false ,
'type' => 'select',
'options' => $array_cities
);
return $fields;
}
如果无法通过默认的Woocommerce字段来完成,我认为要自定义billing_city_custom选择以处理此选择。
答案 0 :(得分:1)
在woo commerce plugin下面安装:
将以下代码放入function.php文件中。
add_filter( 'wc_city_select_cities', 'my_cities' );
/*** Replace XX with the country code. Instead of YYY, ZZZ use actual state codes.*/
function my_cities( $cities ) {
$cities['XX'] = array(
'YYY' => array(
'City ',
'Another City'
),
'ZZZ' => array(
'City 3',
'City 4'
)
);
return $cities;
}
Below is an example of it:
add_filter( 'wc_city_select_cities', 'my_cities' );
function my_cities( $cities ) {
$cities['US'] = array(
'AL' => array(
'City1 of Alabama1',
'City2 of Alabama1'
),
'NJ' => array(
'City1 of New Jersey',
'City2 of New Jersey'
),
'OK' => array(
'City1 of Oklahoma',
'City2 of Oklahoma',
'City3 of Oklahoma',
'City4 of Oklahoma'
)
);
return $cities;
}