简体   繁体   English

在 Woocommerce Checkout 中的帐单国家/地区下方添加自定义字段

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

I'm using WordPress 5.0.2 with WooCommerce 3.5.3 and I have a custom select dropdown field with optgroup on the checkout page, the field work as expected but it appear after the order note and I would like that it appear below the billing_country field.我将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>';
}

The code comes this previous thread: WooCommerce Select Dropdown With Optgroup On Checkout代码来自上一个线程: WooCommerce Select Dropdown With Optgroup On Checkout

I'm aware that this custom field is not hooked to the woocommerce_checkout_fields , And if I do so, it doesn't show the field because I guess that this custom select field is not pulled from the class-wc-countries.php .我知道这个自定义字段没有连接到woocommerce_checkout_fields ,如果我这样做,它不会显示该字段,因为我猜这个自定义选择字段不是从class-wc-countries.php提取的。

This Github thread adds to WooCommerce available form field types a select field with options group " select_og ".这个 Github 线程向 WooCommerce 可用表单字段添加了一个带有选项组select_og ”的选择字段 Get it on Github : lomars / Woocommerce select field with option groupGithub上获取它: lomars / Woocommerce 带有选项组的选择字段

This code is required for this answer.此答案需要此代码。

Now you will be able to include a custom select field with option groups in Woocommerce form fields like checkout billing and shipping fields.现在,您将能够在 Woocommerce 表单字段(如结帐账单和运输字段)中包含带有选项组的自定义选择字段。

Here is that code for your Billing and shipping region field:这是您的帐单和送货区域字段的代码:

// 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;
}

Code goes in function.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 function.php 文件中。 Tested and works.测试和工作。

在此处输入图片说明

Related: WooCommerce Select Dropdown With Optgroup On Checkout相关: WooCommerce 在结账时选择带有 Optgroup 的下拉菜单

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

相关问题 在 WooCommerce 中的姓氏下添加自定义结帐计费字段 - add a custom checkout billing field under the last name in WooCommerce 在Woocommerce的结算明细之前添加新的自定义结帐字段? - Add a new custom checkout field before billing details in Woocommerce? 如何从 WooCommerce 结帐帐单国家/地区获取字段选项数组? - How to get field options array from WooCommerce checkout billing country? 向 WooCommerce Billing 表单添加自定义字段? - Add a custom field to WooCommerce Billing form? 如果woocommerce中为空,则将帐单邮寄地址保存到自定义结帐字段 - Save billing address to custom checkout field if empty in woocommerce 从 WooCommerce 中的自定义结帐计费字段中获取价值 - Getting value from custom checkout billing field in WooCommerce 如何在 WooCommerce 管理员单笔订单中显示自定义结帐计费字段 - How to display custom checkout billing field in WooCommerce admin single orders 在帐单地址WooCommerce结帐字段下添加一个段落 - Add a paragraph under billing address WooCommerce checkout field 如何在 WooCommerce 结账时根据国家/地区制作所需的自定义字段? - How to make a custom field required based on country on WooCommerce checkout? 在 Woocommerce 结帐中根据国家/地区添加弹出窗口和自定义消息 - Add pop up and a custom message based on country in Woocommerce checkout
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM