【问题标题】:WooCommerce Select Dropdown With Optgroup On CheckoutWooCommerce 在结帐时选择带有 Optgroup 的下拉菜单
【发布时间】:2019-05-25 08:06:36
【问题描述】:

我将WordPress 5.0.2WooCommerce 3.5.3 一起使用,并且我在结帐页面上创建了一个选择下拉菜单,效果很好,但是我想在其中添加一些选项组来组织选择选项

这是我的 functions.php 中的代码:

add_action('woocommerce_before_order_notes', 'add_select_checkout_field');
function add_select_checkout_field( $checkout ) {
  echo '<p>New Select Dropdown</p>';
  woocommerce_form_field( 'region', array(
      'type'          => 'select',
      'class'         => array( 'region-dropdown' ),
      'label'         => __( 'Select your region' ),
      'options'       => array(
          'region1'   => __( 'Region 1', 'woocommerce' ),
          'region2' => __( 'Region 2', 'woocommerce' ),
          'region3' => __( 'Region 3', 'woocommerce' ),
          'region4'   => __( 'Region 4', 'woocommerce' )
      )
 ),
  $checkout->get_value( 'region' ));
}

我希望结果是这样输出的:

<select>
    <optgroup label="North Region">
        <option>Region 1</option>
        <option>Region 2</option>
    </optgroup>
    <optgroup label="South Region">
        <option>Region 3</option>
        <option>Region 4</option>
    </optgroup>
</select>

我不知道为什么 woocommerce 没有实现这个功能,但我希望有办法做到这一点。

任何帮助将不胜感激

【问题讨论】:

    标签: php wordpress select woocommerce optgroup


    【解决方案1】:

    您可以通过两种方式做到这一点:

    1) 在 Woocommerce 选择表单字段中启用 &lt;optgroup&gt;,您可以从 GitHub 使用:

    2) 在如下选择字段中手动启用&lt;optgoup&gt;

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

    代码在您的活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。


    相关话题:Add a custom field below billing country in Woocommerce Checkout

    【讨论】:

    • 非常感谢您,它运行良好。但是,有没有办法将 billing_state 字段下的这个字段添加到 woocommerce-billing-fields__field-wrapper 而不是 before_order_notes ?非常感谢,提前感谢
    • 我把问题贴在那里了,如果你有时间来回答就太好了stackoverflow.com/questions/53955108/…
    • @colapsnux 已回答...试试看。
    • 您如何将所选值输入电子邮件订单?,我需要添加一个自定义字段 optgroup,其中包含我也获得的区域名称,但无法将其添加到电子邮件中跨度>
    • 如果可能的话,希望它成为帐单信息中的第一个文本,或者只要它是可见的就可以。我在订单注释下有选择输入。带有他的标签的显示器... expample your region - region 1
    【解决方案2】:

    您可以使用此代码,它会为您工作

    add_filter('woocommerce_form_field_select', function ($html, $unused, $args, $value) {
    if (empty($args['options'])) {
        return $html;
    }
    $option_groups = ['-' => []];
    $options = '';
    foreach ($args['options'] as $option_key => $option_text) {
        $option = array_map('trim', explode(':', $option_text));
        if (count($option) >= 2) {
            $option_groups[array_shift($option)][$option_key] = implode(':', $option);
        } else {
            $option_groups['-'][$option_key] = $option[0];
        }
    }
    foreach ($option_groups as $group => $option) {
        if ($group !== '-') $options .= '<optgroup label="' . esc_attr($group) . '">';
        foreach ($option as $option_key => $option_text) {
            if ($option_key === '') {
                // If we have a blank option, select2 needs a placeholder
                if (empty($args['placeholder'])) {
                    $args['placeholder'] = $option_text ?: __( 'Choose an option', 'woocommerce' );
                }
                $custom_attributes[] = 'data-allow_clear="true"';
            }
            $options .= '<option value="' . esc_attr($option_key) . '" '. selected($value, $option_key, false) . '>' . esc_attr($option_text) . '</option>';
        }
        if ($group !== '-') $options .= '</optgroup>';
    }
    return preg_replace('/(?:<select[^>]+>)\\K(.*)(?:<\\/option>)/s',$options, $html);}, 10, 4);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-19
      • 1970-01-01
      • 2015-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-01
      • 1970-01-01
      相关资源
      最近更新 更多