Global Product Search (Searching Criteria)

Hello,

I hope you are having a great day.

I have a problem with the search function as it allow the visitor to choice only between Categories or tags in their search criteria, but i must allow them to use both as im using tags for cities.

and i need to allow the visitor to choice the city and the category and finally the price range and the website should fetch him products form across the network that fits his criteria,

as of now it works perfectly except for the (filter by) it asks to choice between categories or tags and i need to allow him to use both.

Thanks & Warm Regards
Mohammed Almajed

  • Hoang Ngo

    Hi @Mohammed,

    I hope you are well today.

    Actually, if you use a custom search form, you can append the category and tag criteria via taxonomy_query, example of the usage, here is what the code in file global-site-search.php, line 279 look like

    ob_start();
            $network_query_posts = network_query_posts($parameters);
            include global_site_search_locate_template('global-site-search.php');
            $content .= ob_get_clean();
    
            return $content;

    If you want to append the product_category, so just modify it similar this

    if (isset($_GET['product_category']) && !empty($_GET['product_category']) {
                $p_cat = $_GET['product_category'];
                $parameters['tax_query'] = array(
                    array(
                        'taxonomy' => 'product_category',
                        'field' => 'slug',
                        'terms' => $p_cat,
                    ),
                );
            }
    ob_start();
            $network_query_posts = network_query_posts($parameters);
            include global_site_search_locate_template('global-site-search.php');
            $content .= ob_get_clean();
    
            return $content;

    Ofcourse, you will need to modify the form page, to make sure it display the drop down of product category.

    If you have any issues please don't hesitate to let us know so we can assist

    Best regards,
    Hoang Ngo

  • Hoang Ngo

    Hi @Mohammed,

    I hope you are well today.

    Here is what from the usage

    First locate the /templates/ folder in the plugin download. Inside you will find 2 files:

    global-site-search.php – Displays the search results.
    global-site-search-form.php – Displays the search form.
    Simply copy those files to the root of your theme to modify them as you wish. Be careful not to remove any functions though.

    So first, you will need to copy the global-site-search-form.php to your active theme folder. And the content should like this

    <?php global $current_blog ?>
    <form action="<?php echo esc_url(trailingslashit($current_blog->path . global_site_search_get_search_base())) ?>">
        <table border="0" cellpadding="2" cellspacing="2" style="width:100%">
            <tr>
                <td style="width:80%">
                    <input type="text" name="phrase" style="width:100%"
                           value="<?php echo esc_attr(stripslashes(global_site_search_get_phrase())) ?>">
                </td>
                <td style="width:80%">
                    <?php wp_dropdown_categories(array('taxonomy' => 'product_category', 'name' => 'product_category')) ?>
                </td>
                <td style="width:80%">
                    <?php wp_dropdown_categories(array('taxonomy' => 'product_tag', 'name' => 'product_tag')) ?>
                </td>
                <td style="text-align:right;width:20%">
                    <input type="submit" value="<?php _e('Search', 'globalsitesearch') ?>">
                </td>
            </tr>
        </table>
    </form>

    This will add the product category dropdown and product tag dropdown.
    And then, modify the file /global-site-search/global-site-search.php, line 279, you will see the code like this

    ob_start();
            $network_query_posts = network_query_posts($parameters);
            include global_site_search_locate_template('global-site-search.php');
            $content .= ob_get_clean();
    
            return $content;

    Please modify it like this

    $parameters['tax_query'] = array();
            if (isset($_GET['product_category']) && !empty($_GET['product_category'])) {
                $p_cat = $_GET['product_category'];
                $parameters['tax_query'][] = array(
                    'taxonomy' => 'product_category',
                    'field' => 'slug',
                    'terms' => $p_cat,
                );
            }
    
            if (isset($_GET['product_tag']) && !empty($_GET['product_tag'])) {
                $p_cat = $_GET['product_tag'];
                $parameters['tax_query'][] = array(
                    'taxonomy' => 'product_tag',
                    'field' => 'slug',
                    'terms' => $p_cat,
                );
            }
    
            ob_start();
            $network_query_posts = network_query_posts($parameters);
            include global_site_search_locate_template('global-site-search.php');
            $content .= ob_get_clean();
    
            return $content;

    This will make it filter all the products by category and tag. Also, please make sure you has index the products, not normal post.

    If you have any issues please don't hesitate to let us know so we can assist

    Best regards,
    Hoang Ngo

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.