Home Forums Basel support forum Modifying Basel Ajax Product Search with functions.php

Modifying Basel Ajax Product Search with functions.php

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #83365

    Greetings! I am about to go live with my company store and realized that when people search for “hat” they are getting product results that include any iteration of “hat” even if it’s in a word like “tHAT” or “wHAT” This is unacceptable. I’ve tried using other plugins, but they end up adding a search bar to the header and it throws the entire layout off balance.

    SO, to fix this I added the following code to the child functions.php file. BUT, it’s not working! All I want to do is be able to search products using tags, categories, and titles while also excluding results from partial words. What am I mising? Can you please provide the code needed to make this happen using the child functions.php?

    Your help is MUCH appreciated!!! THANKS IN ADVANCE!

    /**
     * Custom Woocommerce search
     */
    add_filter('the_posts', 'search_by_product_tag');
    
    function search_by_product_tag($posts, $query = '') {
        if (is_search()) {
            $tags = explode(',', get_search_query());
        }
        if ($query != '') {
            $tags = explode(',', $query);
        }
        if (!$tags)
            return $posts;
    
        foreach($tags as $tag)
        {
            //Ignore already found posts from query..
            $ignoreIds = array(0);
            foreach($posts as $post)
            {
                $ignoreIds[] = $post->ID;
            }
    
            $matchedTags = get_post_by_tag(trim($tag), $ignoreIds);
            //var_dump($ignoreIds,$matchedTags);
            //die();
            if ($matchedTags)
            {
                foreach($matchedTags as $product_id)
                {
                    $posts[] = get_post($product_id->post_id);
                }
    
            }
        }
    
        //var_dump($posts);
        return $posts;
    
    }
    
    function get_post_by_tag($tag, $ignoreIds) {
        //Check for
        global $wpdb, $wp_query;
    
        //$ordering_args = WC()->query->get_catalog_ordering_args( $orderby, $order );
    
        /**
         * Special code for removing duplicates if WPML plugin is installed
         * Its popular translation plugin that causes lots of product duplicate products that have different product ids
         * We need to check the wpml meta data of the product to work out the duplicates
         */
    
        $wmplEnabled = false;
    
        if(defined('WPML_TM_VERSION') && defined('WPML_ST_VERSION') && class_exists("woocommerce_wpml")){
            $wmplEnabled = true;
            //What language should we search for...
            $languageCode = ICL_LANGUAGE_CODE;
        }
        //  $wmplEnabled = false;
        $ignoreIdsForMySql = implode(",", $ignoreIds);
        //var_dump($ignoreIdsForMySql);
        $query = "
                select p.ID as post_id from $wpdb->terms t
                join $wpdb->term_taxonomy tt
                on t.term_id = tt.term_id
                join $wpdb->term_relationships tr
                on tt.term_taxonomy_id = tr.term_taxonomy_id
                join $wpdb->posts p
                on p.ID = tr.object_id
                join $wpdb->postmeta visibility
                on p.ID = visibility.post_id
                and visibility.meta_key = '_visibility'
                and visibility.meta_value <> 'hidden'
                ";
    
        //IF WPML Plugin is enabled join and get correct language product.
        if($wmplEnabled)
        {
            $query .=
                "join ".$wpdb->prefix."icl_translations trans on
             trans.element_id = p.ID
             and trans.element_type = 'post_product'
             and trans.language_code = '$languageCode'";
        }
    
        $query .= "
                WHERE
                tt.taxonomy = 'product_tag' and
                t.name LIKE '%$tag%'
                and p.post_status = 'publish'
                and p.post_type = 'product'
                and (p.post_parent = 0 or p.post_parent is null)
                and p.ID not in ($ignoreIdsForMySql)
                group by p.ID
                 ;
    ";
    
        //Search for the sku of a variation and return the parent.
        $matchedProducts = $wpdb->get_results($query) ;
    
        //var_dump($matchedProducts);
        if(is_array($matchedProducts) && !empty($matchedProducts))
        {
            //var_dump($matchedProducts);
            $wp_query->found_posts += sizeof($matchedProducts);
            //var_dump($wp_query->found_posts, sizeof($matchedProducts));
            return $matchedProducts;
    
        }
    
        //return get_post($product_id);
    
        return null;
    }
    #83375

    Artem Temos
    Keymaster

    Hello,

    Thank you so much for purchasing our theme and contacting our support center.

    Your code seems to be correct but we don’t know why it doesn’t work. Unfortunately, we don’t have an instruction for this since our theme doesn’t have this option. And additional code customizations like this are out of our theme support scope.

    Kind Regards
    XTemos Studio

    #83411

    Is my functions.php code missing a hook or some other code that connects to or changes the default Basel: Search Form (searchform.php)???

    #83420

    Please, any help would be greatly appreciated. Thanks in advance!

    #83451

    Artem Temos
    Keymaster

    Sorry, but we don’t have an instruction for such additional code customization.

Viewing 5 posts - 1 through 5 (of 5 total)