【问题标题】:Add a custom field below billing country in Woocommerce Checkout在 Woocommerce Checkout 的计费国家下方添加自定义字段
【发布时间】:2019-05-26 01:50:59
【问题描述】:

我正在使用 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 中提取的。

【问题讨论】:

    标签: php wordpress select woocommerce checkout


    【解决方案1】:

    此 Github 线程向 WooCommerce 中添加了可用的表单字段类型带有选项组的选择字段select_og”。在 Github 上获取它:lomars / Woocommerce select field with option group

    此答案需要此代码。

    现在,您将能够在 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 Select Dropdown With Optgroup On Checkout

    【讨论】:

      猜你喜欢
      • 2017-07-09
      • 1970-01-01
      • 2015-06-01
      • 2020-05-28
      • 1970-01-01
      • 2015-04-09
      • 2023-04-07
      • 2020-08-28
      • 2022-11-14
      相关资源
      最近更新 更多