最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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>
发布评论

评论列表(0)

  1. 暂无评论