繁体   English   中英

在 Woocommerce 管理页面中设置默认的自定义计费 state 字段值

[英]Set a default custom billing state field value in Woocommerce Admin page

我想添加一些自定义计费状态,然后在管理面板中设置默认 state。 到目前为止,我已经添加了如下状态(下面的代码;也不确定这是正确的):

add_filter( 'woocommerce_states', 'custom_woocommerce_states' );
function custom_woocommerce_states( $states ) {

  $states['PE'] = array(
    'PE1' => __('StateA', 'woocommerce')
    'PE2' => __('StateB', 'woocommerce')
  );
  return $states;
}

如何在管理面板中将默认值设置为 StateA?

首先,您当前的代码中存在错误。

在管理员 WooCommerce 订单页面中添加(或编辑)帐单(或送货)地址时,当所选国家/地区为秘鲁 (PE) 时,将自定义“PE1”设置为默认 state,请改用以下内容:

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['XX'] = array( // XX is Country Code 'XX1' => 'State 1', 'XX2' => 'State 2' ); return $states; }

然后您可以使用以下代码更改国家和 state 默认值:

 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 }

如果您尝试使其动态化并从管理面板中选择默认 state,则可以在 Woocommerce 设置中添加一个部分和一个选项,并使用get_option('custom_id')获取它。 您可以在此处获得有关为 Woocommerce 创建自定义设置的帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM