WooCommerce Select Dropdown With Optgroup On Checkout

后端 未结 2 517
南旧
南旧 2020-12-20 07:27

I\'m using WordPress 5.0.2 with WooCommerce 3.5.3 and I have created a select dropdown on the checkout page, it works good, however I want to add

相关标签:
2条回答
  • 2020-12-20 08:28

    You can do it in 2 ways:

    1) Enabling <optgroup> in Woocommerce select form fields, you can use from GitHub:

    • This new fresh thread: lomars - Woocommerce select form field with options group

    • This older thread: QWp6t - Add optgroup support to WooCommerce select form fields

    2) Enabling <optgoup> manually in a select field like:

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

    Code goes in functions.php file of your active child theme (or active theme). Tested and works.


    Related thread: Add a custom field below billing country in Woocommerce Checkout

    0 讨论(0)
  • 2020-12-20 08:30

    You can use this code it will works for you

    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);
    
    0 讨论(0)
提交回复
热议问题