簡體   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