簡體   English   中英

在 Woocommerce Checkout 中的帳單國家/地區下方添加自定義字段

[英]Add a custom field below billing country in Woocommerce Checkout

我將WordPress 5.0.2WooCommerce 3.5.3一起使用,並且在結帳頁面上有一個帶有 optgroup 的自定義選擇下拉字段,該字段按預期工作,但它出現在訂單說明之后,我希望它出現在billing_country場地。

在此處輸入圖片說明

add_action('woocommerce_before_order_notes', 'custom_checkout_select_field_with_optgroup', 10, 1 );
function custom_checkout_select_field_with_optgroup( $checkout ) {
    $domain  = 'woocommerce';
    $title   = __("Region", $domain);
    $slug    = sanitize_title($title);
    $default = __("Select your region", $domain);
    $value   = $checkout->get_value($slug);

    // Region option data array with optgroup
    $options = array(
        __("North Region", $domain) => array(
            'region1' => __("Region 1", $domain),
            'region2' => __("Region 2", $domain),
        ),
        __("South Region", $domain) => array(
            'region3' => __("Region 3", $domain),
            'region4' => __("Region 4", $domain),
        )
    );

    // The field
    echo '<p class="form-row form-row-wide '.$slug.'-dropdown" id="'.$slug.'_field" data-priority="">
    <label for="'.$slug.'" class="">'.$title.'</label>
    <span class="woocommerce-input-wrapper">
    <select name="'.$slug.'" id="'.$slug.'" class="select " data-placeholder="" autocomplete="'.$slug.'">
    <option value="">'.$default.'</option>';

    // Loop through "optgroup"
    foreach( $options as $optgroup_label => $optgroup_options ) {
        echo '<optgroup label="'.$optgroup_label.'">';
        // Loop through "options" in the "optgroup"
        foreach( $optgroup_options as $key => $label ) {
            $selected = $value === $key ? ' selected="selected"': '';
            echo '<option value="'.$key.'"'.$selected.'>'.$label.'</option>';
        }
        echo '</optgroup>';
    }

    echo '</select></span></p>';
}

代碼來自上一個線程: WooCommerce Select Dropdown With Optgroup On Checkout

我知道這個自定義字段沒有連接到woocommerce_checkout_fields ,如果我這樣做,它不會顯示該字段,因為我猜這個自定義選擇字段不是從class-wc-countries.php提取的。

這個 Github 線程向 WooCommerce 可用表單字段添加了一個帶有選項組select_og ”的選擇字段 Github上獲取它: lomars / Woocommerce 帶有選項組的選擇字段

此答案需要此代碼。

現在,您將能夠在 Woocommerce 表單字段(如結帳賬單和運輸字段)中包含帶有選項組的自定義選擇字段。

這是您的帳單和送貨區域字段的代碼:

// Custom function that returns the options data array for "Region" field
function wc_get_region_options_data( $domain ){
    return [
        '' => __("Choose an option…"),
        __("North Region", $domain) => [
            'region1'   => __("Region 1", $domain),
            'region2'   => __("Region 2", $domain),
        ],
        __("South Region", $domain) => [
            'region3'   => __("Region 3", $domain),
            'region4'   => __("Region 4", $domain),
            'region5'   => __("Region 5", $domain),
            'region6'   => __("Region 6", $domain),
        ],
        __("East Region", $domain)  => [
            'region7'   => __("Region 7", $domain),
            'region8'   => __("Region 8", $domain),
            'region9'   => __("Region 9", $domain),
        ],
    ];
}

// Custom function that returns the "Region" field data array
function wc_get_region_field( $fields, $group ){
    $domain   = 'woocommerce';
    $options  = wc_get_region_options_data( $domain );
    $priority = (int) $fields[$group.'_country']['priority'];

    $fields[$group.'_region'] = array(
        'label'    => __("Region", $domain),
        'type'     => 'select_og',
        'class'    => array( 'form-row-wide' ),
        'required' => true,
        'priority' => $priority + 5,
        'options'  => $options,
        'clear'    => true,
    );

    return $fields;
}

// Include region field in billing section after billing country
add_filter('woocommerce_billing_fields', 'region_select_billing_field_with_optgroup', 10, 1 );
function region_select_billing_field_with_optgroup( $billing_fields ) {
    $billing_fields = wc_get_region_field( $billing_fields, 'billing' );
    return $billing_fields;
}

// Include region field in shipping section after shipping country
add_filter('woocommerce_shipping_fields', 'region_select_shipping_field_with_optgroup', 10, 1 );
function region_select_shipping_field_with_optgroup( $shipping_fields ) {
    $shipping_fields = wc_get_region_field( $shipping_fields, 'shipping' );
    return $shipping_fields;
}

代碼位於活動子主題(或活動主題)的 function.php 文件中。 測試和工作。

在此處輸入圖片說明

相關: WooCommerce 在結賬時選擇帶有 Optgroup 的下拉菜單

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM