在 Woocommerce 管理页面中设置默认的自定义账单状态字段值
Set a default custom billing state field value in Woocommerce Admin page
我想添加一些自定义账单状态,然后在管理面板中设置默认状态。到目前为止,我已经添加了如下状态(下面的代码;也不确定这是正确的):
add_filter( 'woocommerce_states', 'custom_woocommerce_states' );
function custom_woocommerce_states( $states ) {
$states['PE'] = array(
'PE1' => __('StateA', 'woocommerce')
'PE2' => __('StateB', 'woocommerce')
);
return $states;
}
如何在管理面板中将默认值设置为 StateA?
您可以使用此代码为国家/地区添加一些新的自定义状态:
add_filter( 'woocommerce_states', 'custom_woocommerce_states' );
function custom_woocommerce_states( $states ) {
$states['XX'] = array( // XX is Country Code
'XX1' => 'State 1',
'XX2' => 'State 2'
);
return $states;
}
然后您可以使用此代码更改国家和州的默认值:
add_filter( 'default_checkout_billing_country', 'change_default_checkout_country' );
add_filter( 'default_checkout_billing_state', 'change_default_checkout_state' );
function change_default_checkout_country() {
return 'XX'; // country code
}
function change_default_checkout_state() {
return 'XX'; // state code
}
如果您尝试使其动态化并从您的管理面板中选择您的默认状态,您可以在您的 Woocommerce 设置中添加一个部分和一个选项,并使用 get_option('custom_id')
获取它。您可以获得有关为 Woocommerce 创建自定义设置的帮助 here。
首先,您当前的代码有错误。
在管理员 WooCommerce 订单页面中添加(或编辑)帐单(或送货)地址时,要将自定义 'PE1' 设置为所选国家/地区为秘鲁 (PE) 时的默认状态,请改用以下内容:
add_filter( 'woocommerce_states', 'custom_woocommerce_states_for_peru' );
function custom_woocommerce_states_for_peru( $states ) {
// For PERU
$states['PE'] = array(
'PE1' => __('StateA', 'woocommerce'),
'PE2' => __('StateB', 'woocommerce')
);
return $states;
}
// Admin orders: Set a default state for PERU country
add_action( 'admin_footer', 'custom_admin_shop_order_js' );
function custom_admin_shop_order_js() {
global $pagenow, $post_type;
if ( in_array( $pagenow, array('post-new.php', 'post.php') ) && 'shop_order' === $post_type ) :
?><script type='text/javascript'>
jQuery( function($) {
// Billing state
$(document.body).on( 'change', 'select#_billing_country,select#_shipping_country', function(){
var country = 'PE', // Set country
defaultState = 'PE1', // Set default state (for country)
parent = $(this).parent().parent(),
billingState = parent.find('select#_billing_state'),
shippingState = parent.find('select#_shipping_state');
if( country === $(this).val() ) {
if ( '' === billingState.val() ) {
billingState.val(defaultState).trigger("change");
} else if ( '' === shippingState.val() ) {
shippingState.val(defaultState).trigger("change");
}
}
});
});
</script><?php
endif;
}
代码进入活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
我想添加一些自定义账单状态,然后在管理面板中设置默认状态。到目前为止,我已经添加了如下状态(下面的代码;也不确定这是正确的):
add_filter( 'woocommerce_states', 'custom_woocommerce_states' );
function custom_woocommerce_states( $states ) {
$states['PE'] = array(
'PE1' => __('StateA', 'woocommerce')
'PE2' => __('StateB', 'woocommerce')
);
return $states;
}
如何在管理面板中将默认值设置为 StateA?
您可以使用此代码为国家/地区添加一些新的自定义状态:
add_filter( 'woocommerce_states', 'custom_woocommerce_states' );
function custom_woocommerce_states( $states ) {
$states['XX'] = array( // XX is Country Code
'XX1' => 'State 1',
'XX2' => 'State 2'
);
return $states;
}
然后您可以使用此代码更改国家和州的默认值:
add_filter( 'default_checkout_billing_country', 'change_default_checkout_country' );
add_filter( 'default_checkout_billing_state', 'change_default_checkout_state' );
function change_default_checkout_country() {
return 'XX'; // country code
}
function change_default_checkout_state() {
return 'XX'; // state code
}
如果您尝试使其动态化并从您的管理面板中选择您的默认状态,您可以在您的 Woocommerce 设置中添加一个部分和一个选项,并使用 get_option('custom_id')
获取它。您可以获得有关为 Woocommerce 创建自定义设置的帮助 here。
首先,您当前的代码有错误。
在管理员 WooCommerce 订单页面中添加(或编辑)帐单(或送货)地址时,要将自定义 'PE1' 设置为所选国家/地区为秘鲁 (PE) 时的默认状态,请改用以下内容:
add_filter( 'woocommerce_states', 'custom_woocommerce_states_for_peru' );
function custom_woocommerce_states_for_peru( $states ) {
// For PERU
$states['PE'] = array(
'PE1' => __('StateA', 'woocommerce'),
'PE2' => __('StateB', 'woocommerce')
);
return $states;
}
// Admin orders: Set a default state for PERU country
add_action( 'admin_footer', 'custom_admin_shop_order_js' );
function custom_admin_shop_order_js() {
global $pagenow, $post_type;
if ( in_array( $pagenow, array('post-new.php', 'post.php') ) && 'shop_order' === $post_type ) :
?><script type='text/javascript'>
jQuery( function($) {
// Billing state
$(document.body).on( 'change', 'select#_billing_country,select#_shipping_country', function(){
var country = 'PE', // Set country
defaultState = 'PE1', // Set default state (for country)
parent = $(this).parent().parent(),
billingState = parent.find('select#_billing_state'),
shippingState = parent.find('select#_shipping_state');
if( country === $(this).val() ) {
if ( '' === billingState.val() ) {
billingState.val(defaultState).trigger("change");
} else if ( '' === shippingState.val() ) {
shippingState.val(defaultState).trigger("change");
}
}
});
});
</script><?php
endif;
}
代码进入活动子主题(或活动主题)的 functions.php 文件。已测试并有效。