woocommerce offtopic - How can i create a custom product search?

I need to create category and subcategory drop down with search field.but here is the condition if category has subcate

I need to create category and subcategory drop down with search field. but here is the condition if category has subcategory then it'll show the category otherwise it'll show blank. i've success to show the category but the problem with the subcategory. When select the category then only show the subcategory.

Here is my code:

<form role="search" method="get" class="search-form" action="<?php echo home_url( '/' ); ?>">
                <div class="col-sm-2 brand">
                    <?php
                      $taxonomy     = 'product_cat';
                      $orderby      = 'name';  
                      $show_count   = 0;      // 1 for yes, 0 for no
                      $pad_counts   = 0;      // 1 for yes, 0 for no
                      $hierarchical = 1;      // 1 for yes, 0 for no  
                      $title        = '';  
                      $empty        = 0;

                      $args = array(
                             'taxonomy'     => $taxonomy,
                             'orderby'      => $orderby,
                             'show_count'   => $show_count,
                             'pad_counts'   => $pad_counts,
                             'hierarchical' => $hierarchical,
                             'title_li'     => $title,
                             'hide_empty'   => $empty
                      );
                     $all_categories = get_categories( $args );
                     ?>

                    <select>
                         <?php 
                         foreach ($all_categories as $cat) {
                            if($cat->category_parent == 0) {
                                $category_id = $cat->term_id;       
                                echo '<option value="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</option>';
                            }       
                        }
                        ?>
                    </select>
                </div>
                <div class="col-sm-2 product">
                    <select>
                        <?php

                          $taxonomy     = 'product_cat';
                          $orderby      = 'name';  
                          $show_count   = 0;      // 1 for yes, 0 for no
                          $pad_counts   = 0;      // 1 for yes, 0 for no
                          $hierarchical = 1;      // 1 for yes, 0 for no  
                          $title        = '';  
                          $empty        = 0;

                          $args = array(
                                 'taxonomy'     => $taxonomy,
                                 'orderby'      => $orderby,
                                 'show_count'   => $show_count,
                                 'pad_counts'   => $pad_counts,
                                 'hierarchical' => $hierarchical,
                                 'title_li'     => $title,
                                 'hide_empty'   => $empty
                          );
                         $all_categories = get_categories( $args );
                         foreach ($all_categories as $cat) {
                            if($cat->category_parent == 0) {
                                $category_id = $cat->term_id;

                                $args2 = array(
                                        'taxonomy'     => $taxonomy,
                                        'child_of'     => 0,
                                        'parent'       => $category_id,
                                        'orderby'      => $orderby,
                                        'show_count'   => $show_count,
                                        'pad_counts'   => $pad_counts,
                                        'hierarchical' => $hierarchical,
                                        'title_li'     => $title,
                                        'hide_empty'   => $empty
                                );
                                $sub_cats = get_categories( $args2 );
                                if($sub_cats) {
                                    foreach($sub_cats as $sub_category) {
                                        echo  '<br/><option value="'. get_term_link($sub_category->slug, 'product_cat') .'">'. $sub_category->name .'</option>';
                                    }
                                }
                            }       
                        }
                        ?>
                    </select>
                </div>
                <div class="col-sm-4 search">

                        <div class="search-wrapper input-group">
                            <input type="search" class="search-field form-control" placeholder="<?php echo esc_attr_x( 'Search …', 'placeholder' ) ?>" value="<?php echo get_search_query() ?>" name="s" title="<?php echo esc_attr_x( 'Search for:', 'label' ) ?>" />
                            <span class="input-group-btn">
                            <button class="btn btn-default" type="submit" value="<?php echo esc_attr_x( 'Search', 'submit button' ) ?>"><i class="fa fa-search" aria-hidden="true"></i></button>
                            </span>
                        </div>

                </div>
            </form>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745361732a4624392.html

相关推荐

  • woocommerce offtopic - How can i create a custom product search?

    I need to create category and subcategory drop down with search field.but here is the condition if category has subcate

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信