Please note: This forum is now closed. All the support related enquiries should be reported on wordpress support forum for jigoshop plugin – thanks, Jigoshop Team

Product Search

joshrodgers's Avatar

joshrodgers

26 Apr, 2012 04:29 PM

Hello there,

I am trying to create a product search feature and ran into a little roadblock...I cannot return Jigoshop search results :(

So, this is where I'm at...

I copied the search form from the product_search widget and came up with the following code:

<form method="get" id="searchform" action="<?php echo home_url(); ?>">
    <input type="text" value="Search..." name="s" id="s" />
    <input type="hidden" name="post_type" value="product" />
    <input type="submit" id="searchsubmit" value="Search" />
</form>

Then, I enhanced the code a little by adding some default text that disappears when the user clicks on the input, as well as, a couple of dropdowns that allow the user to select multiple categories:

<form method="get" id="searchform" action="<?php echo home_url(); ?>">
    <input type="text" onclick="this.value='';" onfocus="this.select()" onblur="this.value=!this.value?'Search...':this.value;" value="Search..." name="s" id="s" />
    <input type="hidden" name="post_type" value="product" />
    <?php wp_dropdown_categories('taxonomy=product_cat&id=make&child_of=15&show_option_all=Select Make...'); ?>
    <?php wp_dropdown_categories('taxonomy=product_cat&id=model&child_of=21&show_option_all=Select Model...'); ?>
    <input type="submit" id="searchsubmit" value="Search" />
</form>

Before I added the code above, I tested it with normal WordPress posts and it worked fine, so there is nothing wrong with this code.

After that, I added a search page to return my search results, the code looks like:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <?php the_title(); ?>
    <?php the_content(); ?>
<?php endwhile; else: ?>
    <?php _e('No content!'); ?>
<?php endif; ?>

This code is working fine with normal WordPress posts, but for some reason it doesn't return the Jigoshop search results.

So, I created an example site at: joshrodgers.com. If you search for title and choose Volvo as the make and XC90 as the model you can see this issue. The results page says No Content!

I thought I was going crazy, so I added a small piece of code to echo the search term and tell me how many results were found. This is working. If you run my suggested search from above you'll notice this code says 5 results. I know its working properly because if you search for the word two, instead of title, it shows 1 result.

So, what's wrong with my search result code? I am so close...I just can't seem to figure out what I'm missing.

Any Ideas?

Thanks,
Josh

  1. Support Staff 1 Posted by chriscct7 on 26 Apr, 2012 05:20 PM

    chriscct7's Avatar

    Hi Josh,
    Your search works perfectly if you only search by title or by title and model. It is your code for the make (subcategory) is wrong. Haven't put my finger on it yet.
    -Chris

  2. Support Staff 2 Posted by chriscct7 on 26 Apr, 2012 05:25 PM

    chriscct7's Avatar

    You sure the Taxonomy on the subcat isn't is product_subcat?
    Since Jigoshop publishes a custom post type, my guess is they have a custom subcat type.
    -Chris

  3. 3 Posted by joshrodgers on 26 Apr, 2012 05:37 PM

    joshrodgers's Avatar

    Chris,

    Thank you for your reply. I went and looked into the database and saw the following (see screenshot)...only references to product_cat.

    I also opened jigoshop_taxonomy.php and looked for all of the registered taxonomies and didn't find anything for child categories or sub categories.

    Thanks,
    Josh

  4. Support Staff 4 Posted by chriscct7 on 26 Apr, 2012 05:47 PM

    chriscct7's Avatar

    Hmm,
    Well it simply has to be the subcat because if you search without it it
    works just fine.
    My inclination would be to write a quick php script to return the first
    product of a particular subcategory. If it works, then something else is
    wrong. If it fails, one has to assume there is a "hidden" taxonomy somehow.

    -Chris

  5. 5 Posted by joshrodgers on 30 Apr, 2012 10:49 PM

    joshrodgers's Avatar

    Ok, I have been doing some investigating and came across a function that will let me search across multiple categories...the only problem is that it doesn't work with my custom taxonomy or custom post type...any ideas?

    The search form looks like:

    <form method="get" id="searchform" action="<?php echo home_url(); ?>">
        <input type="text" onclick="this.value='';" onfocus="this.select()" onblur="this.value=!this.value?'Search...':this.value;" value="Search..." name="s" id="s" />
        <?php wp_dropdown_categories('show_option_all=Select Make...'); ?>
        <?php wp_dropdown_categories('name=cats&show_option_all=Select Model...'); ?>
        <input type="submit" id="searchsubmit" value="Search" />
    </form>
    

    Functions looks like:

    add_action( 'parse_request', 'category_search_logic', 11 );
    function category_search_logic( $query ) {
        if ( ! isset( $query->query_vars[ 'cat' ] ) )
            return $query;
        // split cat query on a space to get IDs separated by '+' in URL
        $cats = explode( ' ', $query->query_vars[ 'cat' ] );
        if ( count( $cats ) > 1 ) {
            unset( $query->query_vars[ 'cat' ] );
            $query->query_vars[ 'category__and' ] = $cats;
        }
        return $query;
    }
    
    function check()
    {
        $url = $_SERVER["REQUEST_URL"];
        $query = str_replace('&cats=', '+', $url);
        if(isset($query) && ($query != $url))
        {
            header('Location:'.$query);
        }
    }
    
    add_action('parse_query', 'check');
    

    It's getting so close...any ideas?

    Thanks,
    Josh

  6. Support Staff 6 Posted by chriscct7 on 30 Apr, 2012 10:56 PM

    chriscct7's Avatar

    Yep, change 'cat' to 'product_cat' .
    and see if it works
    -Chris

  7. 7 Posted by joshrodgers on 30 Apr, 2012 11:03 PM

    joshrodgers's Avatar

    Chris,

    I thought for sure that was going to work!! Unfortunately, that didn't do anything...I tried it in two different browsers just to make sure there was no cache :(

    Thanks,
    Josh

  8. Support Staff 8 Posted by chriscct7 on 30 Apr, 2012 11:16 PM

    chriscct7's Avatar

    You replaced all "cats" in there as well? Including $cats and all those?

    -Chris

  9. 9 Posted by joshrodgers on 01 May, 2012 05:15 AM

    joshrodgers's Avatar

    Chris,

    Yep, just tried it:

    add_action( 'parse_request', 'category_search_logic', 11 );
    function category_search_logic( $query ) {
        if ( ! isset( $query->query_vars[ 'product_cat' ] ) )
            return $query;
        // split cat query on a space to get IDs separated by '+' in URL
        $product_cats = explode( ' ', $query->query_vars[ 'product_cat' ] );
        if ( count( $product_cats ) > 1 ) {
            unset( $query->query_vars[ 'product_cat' ] );
            $query->query_vars[ 'category__and' ] = $product_cats;
        }
        return $query;
    }
    
    function check()
    {
        $url = $_SERVER["REQUEST_URL"];
        $query = str_replace('&product_cats=', '+', $url);
        if(isset($query) && ($query != $url))
        {
            header('Location:'.$query);
        }
    }
    
    add_action('parse_query', 'check');
    

    The dropdowns remained the same, so I added the taxonomy to them as well - which didn't work :(

    Thanks,
    Josh

  10. 10 Posted by joshrodgers on 03 May, 2012 06:55 PM

    joshrodgers's Avatar

    Ok,

    I found something I think is going to work, it just needs a small tweak...

    While searching, I found the following plugin: Query Multiple Taxonomies

    All I have to do is activate the plugin and add the following code to my template:

    <?php
        the_widget('Taxonomy_Drill_Down_Widget', array(
            'title' => '',
            'mode' => 'dropdowns',
            'taxonomies' => array( 'product_cat','product_cat' ) // list of taxonomy names
        ));
    ?>
    

    This works...you can check it out at: http://www.joshrodgers.com/

    The only thing is, is that the dropdown has both the make and the model, is there any way to restrict it so that the first one just has "Make" and the second just has "Model"?

    NOTE: I created my own template for the dropdown (Reference Here) which looks like:

    <form method="get" id="searchform" action="{{base-url}}">
        <input type="text" onclick="this.value='';" onfocus="this.select()" onblur="this.value=!this.value?'Search...':this.value;" value="Search..." name="s" id="s" />
            {{#taxonomy}}
            <select name="{{name}}">
                <option></option>
                {{{options}}}
            </select>
            {{/taxonomy}}
        <input type="submit" id="searchsubmit" value="Search" />
    </form>
    

    Thanks in advance!
    Josh

  11. 11 Posted by joshrodgers on 04 May, 2012 02:28 PM

    joshrodgers's Avatar

    After looking at this code, I may be able to tweak my original code to make it work. Let me explain...

    When I do a search with the plugin the url that shows up is: http://joshrodgers.com/?s=title&product_cat=saturn&product_cat=view

    So, when I went back to my original code, I just tweaked the names of the dropdowns to match and now it's almost working!! My code now looks like:

    <form method="get" id="searchform" action="<?php echo home_url(); ?>">
        <input type="text" onclick="this.value='';" onfocus="this.select()" onblur="this.value=!this.value?'Search...':this.value;" value="Search..." name="s" id="s" />
        <?php wp_dropdown_categories('name=product_cat&taxonomy=product_cat&child_of=15&id=make&show_option_all=Select Make...'); ?>
        <?php wp_dropdown_categories('name=product_cat&taxonomy=product_cat&child_of=21&id=model&show_option_all=Select Model...'); ?>
        <input type="submit" id="searchsubmit" value="Search" />
    </form>
    

    When I do a search the url that shows up is: http://joshrodgers.com/?s=title&product_cat=20&product_cat=26

    The only difference between my code, that doesn't work and their code that does is that their code uses names for the product_cat, where mine uses numbers...is there anyway to change this so that mine also uses names?

    Thanks,
    Josh

  12. 12 Posted by joshrodgers on 04 May, 2012 11:30 PM

    joshrodgers's Avatar

    Found a solution!!

    This goes into your functions file:

    // dropdown filter
    class dropdown extends Walker_CategoryDropdown {
    
        function start_el(&$output, $category, $depth, $args) {
            $pad = str_repeat('&nbsp;', $depth * 3);
    
            $cat_name = apply_filters('list_cats', $category->name, $category);
            $output .= "\t<option class=\"level-$depth\" value=\"".$category->slug."\"";
            if ( $category->term_id == $args['selected'] )
                $output .= ' selected="selected"';
            $output .= '>';
            $output .= $pad.$cat_name;
            if ( $args['show_count'] )
                $output .= '&nbsp;&nbsp;('. $category->count .')';
            if ( $args['show_last_update'] ) {
                $format = 'Y-m-d';
                $output .= '&nbsp;&nbsp;' . gmdate($format, $category->last_update_timestamp);
            }
            $output .= "</option>\n";
        }
    }
    

    This goes into your theme:

    <form method="get" id="searchform" action="<?php echo home_url(); ?>">
        <input type="text" onclick="this.value='';" onfocus="this.select()" onblur="this.value=!this.value?'Search...':this.value;" value="Search..." name="s" id="s" />
        <?php
            wp_dropdown_categories(
                array(
                    'child_of' => 15,
                    'id' => 'make',
                    'name' => 'product_cat',
                    'show_option_all' => 'Select Make...',
                    'taxonomy' => 'product_cat',
                    'walker' => new dropdown
                ))
        ?>
        <?php
            wp_dropdown_categories(
                array(
                    'child_of' => 21,
                    'id' => 'model',
                    'name' => 'product_cat',
                    'show_option_all' => 'Select Model...',
                    'taxonomy' => 'product_cat',
                    'walker' => new dropdown
                ))
        ?>
        <input type="submit" id="searchsubmit" value="Search" />
    </form>
    

    The walker provides the functionality :)

    Hope this helps someone!!!

    Thanks,
    Josh

  13. joshrodgers closed this discussion on 04 May, 2012 11:31 PM.

  14. joshrodgers re-opened this discussion on 04 May, 2012 11:31 PM

  15. joshrodgers closed this discussion on 04 May, 2012 11:31 PM.

  16. joshrodgers re-opened this discussion on 05 May, 2012 04:48 PM

  17. 13 Posted by joshrodgers on 05 May, 2012 04:52 PM

    joshrodgers's Avatar

    I've been looking at this code and I tweaked it to remove a couple of things I don't need...here is the simplified version, this goes in functions.php:

    // dropdown filter
    class dropdown extends Walker_CategoryDropdown {
    
        function start_el(&$output, $category, $depth, $args) {
            $pad = str_repeat('&nbsp;', $depth * 3);
    
            $cat_name = apply_filters('list_cats', $category->name, $category);
            $output .= "\t<option class=\"level-$depth\" value=\"".$category->slug."\"";
            if ( $category->term_id == $args['selected'] )
                $output .= ' selected="selected"';
            $output .= '>';
            $output .= $pad.$cat_name;
            $output .= "</option>\n";
        }
    }
    

    NOTE: *The lines of code I removed displays the last update and includes a counter.

    NOTE: *The search form from the previous post remains unchanged.

    Thanks,
    Josh

  18. 14 Posted by joshrodgers on 18 May, 2012 05:27 PM

    joshrodgers's Avatar

    Ok...after all that I discovered I still had a problem with my search...

    I have fixed the problem and the solution looks like this:

    My functions page now looks like:

    <?php
        class dropdown extends Walker_CategoryDropdown {
            function start_el(&$output, $category, $depth, $args) {
                $pad = str_repeat('&nbsp;', $depth * 3);
                $cat_name = apply_filters( 'list_cats', $category->name, $category );
                $output .= "\t<option class=\"level-$depth\" value=\"".$category->slug."\""; 
                $output .= '>';
                $output .= $pad.$cat_name;
                if ( $args['show_count'] )
                    $output .= '&nbsp;&nbsp;('. $category->count .')';
                if ( $args['show_last_update'] ) {
                    $format = 'Y-m-d';
                $output .= '&nbsp;&nbsp;' . gmdate($format, $category->last_update_timestamp);
                }
                $output .= "</option>\n";
            }
        }
    ?>
    

    My form now looks like:

    <form method="get" id="searchform" action="<?php echo home_url(); ?>">
        <input type="text" onclick="this.value='';" onfocus="this.select()" onblur="this.value=!this.value?'Product Search...':this.value;" value="Product Search..." name="s" id="s" class="left" />
        <input type="image" src="<?php bloginfo('template_directory') ?>/images/icosearch.png" id="searchsubmit" value=""/>
        <?php
            wp_dropdown_categories(
                array(
                    'child_of' => 426,
                    'class' => 'styled',
                    'id' => 'make',
                    'name' => 'make',
                    'show_option_all' => 'Make...',
                    'taxonomy' => 'product_cat',
                    'walker' => new dropdown
                ))
        ?>
        <?php
            wp_dropdown_categories(
                array(
                    'child_of' => 427,
                    'class' => 'styled',
                    'id' => 'model',
                    'name' => 'model',
                    'show_option_all' => 'Model...',
                    'taxonomy' => 'product_cat',
                    'walker' => new dropdown
                ))
        ?>
    </form>
    

    My search page now looks like:

    <?php
        $tmp[]=$_GET["model"];
        $tmp[]=$_GET["make"];
        $product_cat=array();                                          
        foreach($tmp as $v) {
            if($v!="0")
            $product_cat[]=$v;
        }
        if(count($product_cat)>0) {
            $product_cat="&product_cat=".implode(",",$product_cat);    
        }
        else {
            $product_cat="";
        }                                                           
        $query=query_posts("s=$text".$product_cat);
        $count =count($query);
        if ($count == 0) { 
            echo 'No Results'; 
        }
        elseif ($count == 1) { 
            echo ''.$count.' Result'; 
        } 
        else { 
            echo ''.$count.' Results'; 
        } 
    ?>
    
  19. 15 Posted by Matt on 18 May, 2012 07:54 PM

    Matt's Avatar

    Could you show us a screenshot of the end result on the front end?

  20. 16 Posted by joshrodgers on 18 May, 2012 08:44 PM

    joshrodgers's Avatar

    A shot of the form itself? See the attached image :)

    Thanks,
    Josh

  21. 17 Posted by Matt on 18 May, 2012 08:46 PM

    Matt's Avatar

    Was there something wrong with our widget? :P

  22. Support Staff 18 Posted by chriscct7 on 18 May, 2012 08:50 PM

    chriscct7's Avatar

    What theme are you on cause that looks nice.

    @Matt where have you been for the last 15 threads :P haha :)

  23. 19 Posted by joshrodgers on 18 May, 2012 08:55 PM

    joshrodgers's Avatar

    @Matt Which widget? I tried to use the code from the regular search widget and couldn't get it to work :( @Chris It's a theme I designed and developed...I would give you a link (see screenshot), but it's not live yet :(

    Thanks,
    Josh

  24. Support Staff 20 Posted by chriscct7 on 18 May, 2012 08:59 PM

    chriscct7's Avatar

    @JoshRogers well you could always give me the source :P

  25. 21 Posted by Matt on 18 May, 2012 09:06 PM

    Matt's Avatar

    Are you guys saying the widget doesn't work?

    I've just tested and it indeed works...Soo o.o

  26. 22 Posted by joshrodgers on 18 May, 2012 09:12 PM

    joshrodgers's Avatar

    Matt,

    I looked through the widget code in the plugin folder, hoping to find something I could copy and paste into my theme (because I don't have a widgetized sidebar) and couldn't figure it out...so, after a while I started exploring for an alternative solution :)

    Thanks,
    Josh

  27. Support Staff 23 Posted by chriscct7 on 18 May, 2012 09:14 PM

    chriscct7's Avatar

    @Matt Ref in addition,
    This is to allow a more advanced search function then the one Jigoshop currently has. Its a PHP fix to allow users to select a "make" and "model" but has several practical applications beyond that. Basically it allows for the filtering of Jigoshop search results by Product categories.

  28. 24 Posted by joshrodgers on 18 May, 2012 09:25 PM

    joshrodgers's Avatar

    Chris,

    I thought he was implying that the widget already does that, but I'm guessing that's not the case after reading your comment :)

    Thanks,
    Josh

  29. Support Staff 25 Posted by ivica on 18 May, 2012 09:27 PM

    ivica's Avatar

    Nice :-)

  30. Support Staff 26 Posted by chriscct7 on 18 May, 2012 09:31 PM

    chriscct7's Avatar

    No @Matt's was under the impression that you were referring that the original Jigoshop widget is broken, which you don't have. And we can add this functionality to the existing widget code.

    Chris

  31. 27 Posted by arcadence on 03 Jul, 2012 09:29 PM

    arcadence's Avatar

    Does the Jigoshop widget allow you to create an advanced search or even a step-by-step conditional search (i.e. You select Make which gives you certain options to the Model for that specific Make)? <--- Is there a specific terminology for this type of search?

    @Josh very nice layout.

  32. Support Staff 28 Posted by chriscct7 on 03 Jul, 2012 09:33 PM

    chriscct7's Avatar

    Out of box no, and no. I have seen this custom coded using taxonomy and custom fields however.

    -Chris

  33. 29 Posted by arcadence on 03 Jul, 2012 09:45 PM

    arcadence's Avatar

    I'm looking into custom taxonomy plugins now. I'm hoping to be able to get something that I can use in conjunction with jigoshop, as I'm already familiar with the way jigo works (setting up my store), but like Josh, I'm looking to utilize a Make/Model/Trim of cars so that people can see a list of specialized parts.

Comments are closed, but you can start a new discussion.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac