• Resolved Ankur Prem

    (@ankur-prem)


    Hi. When searching for a product (woocommerce search widget) with terms that point to both portuguese (default lang) and english (second lang) the search results show both products, as if they were separate things, instead of only showing the result in the active language. This leads the costumer to a very confusing place.

    For example: http://www.ankurprem.com.br/catprod/cattestept/
    CatTestePt is the category in portuguese
    CatTesteEn is the category in english
    The product Testeprod01 have both portuguese and english versions (same name).

    If I search for “Test” (a part of the products title present in both languages – could be the whole name also) it only shows in the result the product in the active language – awesome!

    But…

    If I use tags (installed the plugin “Search By Product tag – for Woocommerce” so the search uses the tags too), the same not happens. For example, in the portuguese product version I have the tags [aranha, lycosa] and in the english version, their traduction [spider, lycosa], since lycosa is a name.

    Lets suppose I have the portuguese language active. If I search for “aranha”, great, the portuguese version appears. If I search for “spider”, now it can be problematic, because the search returns the english product, but for a portuguese costumer (in this case they shouldn’t be using an english tag, of course, but many terms exist in both languages, and the user getting redirected for an english version of the site without no warning is not so friendly). And the worst picture: if the costumer search for “lycosa”, now both versions are found… “what is it? different products? same product? wow, why suddenly the site changed the language?”.

    Got the point?

    I don’t know, but I think its not so complicated to resolve this issue – since the products title are showing the results properly, in the correct (active) language only.

    Anyway, despite of that, I think you’ve done a great job and this is the better and most intelligent plugin for Woo+Pol available! I’m already giving 5 stars and a endorsement.

    https://wordpress.org/plugins/woo-poly-integration/

Viewing 15 replies - 1 through 15 (of 40 total)
  • Thread Starter Ankur Prem

    (@ankur-prem)

    By the way, the product image does not have relation with the tags – don’t mind if you see a ladybug instead of a spider! 😀

    Thread Starter Ankur Prem

    (@ankur-prem)

    Oops, looks there are more issues regarding the search functionality:
    – Portuguese site active. Search “testeprod01” (product’s name in both languages). Returns the portuguese product (ok!) / English site active. Search “testeprod01”. Returns the PORTUGUESE product 🙁
    – Portuguese site active. Search any term present in the portuguese product description (“breve”, for example). Returns the portuguese product (ok!) / English site active. Search any term present in the english product description (“short”, for example). Don’t find anything, showing a PORTUGUESE page to say that.

    Plugin Author Hyyan Abo Fakher

    (@hyyan)

    @ankur Prem I think I’ve got your point here , so let’s break it up :

    1 – woocommerce search widget

    The widget uses the wordpress functions to display products and that’s what allowed polylang to display the correct products according to active langauge , and that’s what we expect , so as you said It is OK

    2 – Search By Product tag – for Woocommerce plugin

    I’ve just installed the plugin and made some tests , as it seems the plugin is using its own query to achive its goal , here is a snippet :

    $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'
                ";

    thats make it impossible for polylang or WooPoly to to correct the results plus it does not provider any filter for its query to add support ,

    But guess what I still have the solution but you have add a two or three lines to the plugin , here is the modified version :

    <?php
    
    /*
      Plugin Name: Search By Product tag - for Woocommerce
      Plugin URI: http://www.mattyl.co.uk/2012/12/14/woocommerce-wordpress-plugin-to-search-for-products-by-tag/
      Description: The search functionality in woocommerce doesn't search by product tags by default. This simple plugin adds this functionality to both the admin site and regular search
      Author: Matthew Lawson
      Version: 0.3.1
      Author URI: http://www.mattyl.co.uk/
     */
    
    add_filter('the_posts', 'search_by_product_tag');
    
    function search_by_product_tag($posts, $query = false) {
        if (is_search()) {
            //get_search_query does sanitization
    
            $tags = explode(',', get_search_query());
            //var_dump($tags);
            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;
        }
    
        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);
    	global $polylang;
    	$join = $polylang->model->join_clause('post');
        $where = $polylang->model->where_clause($lang, 'post');
        $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'
    			{$join}
                ";
        //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
    			{$where}
                 ;
    ";
    
        //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;
    }
    
    ?>

    please note that I am not adding support for my plugin here but a support for polylang in general , maybe you can ask the plugin author to add support for polylang as it is really easy .

    3 – user getting redirected for an english version

    That is the default behavior for woocommerce when there is only one result for your search terms .

    Plugin Author Hyyan Abo Fakher

    (@hyyan)

    @ankur Prem I belive the Search By Product tag – for Woocommerce plugin is your problem here , cause it modifies the search query , please try the solution first , to see if it worked for you

    Thread Starter Ankur Prem

    (@ankur-prem)

    Didn’t solved the problem, Hyyan. Just instead of showing both products, it show only the english version – even when I’m browsing in portuguese. I think the perfect solution should give:
    – Portuguese results only, when browsing in portuguese
    – English result only, when browsing in english

    But, I don’t know, maybe its just too many plugins needing to work together with a more close global integration for this to work properly.

    Thread Starter Ankur Prem

    (@ankur-prem)

    Couldn’t you write a plugin with this feature? (portuguese results only, when browsing in portuguese; english result only, when browsing in english).

    This would solve 100% all the problems above and would be the only one in the market.

    If you’re not interested in releasing such plugin, could you write this code for me as a freelance job?

    I don’t know, but it doesn’t look so much complicated – at least for someone skilled in coding (like you) 🙂

    Plugin Author Hyyan Abo Fakher

    (@hyyan)

    @ankur Prem I am afraid you are write , it is not a complete solution and some extra works need to be done here , the problem with search query in general I will try to solve this tomorrow.

    Thread Starter Ankur Prem

    (@ankur-prem)

    See, I removed the “Search By Product tag – for Woocommerce” plugin, lets leave the tag subject aside for now.

    I also changed the english product name for Testeprod01en (added “en” in the end) just to discriminate better.

    The main issue happening is:
    – When I search for the partial product name (“teste”) it only find the portuguese version, even when browsing in english.
    – When I search for the whole english product name (“Testeprod01en”) it doesn’t find anything (and get back to the portuguese browsing, looks like this “no products found” page are not being translated.
    – All other terms in other fields (descriptions, etc) only show results in portuguese (main language), even when browsing in english.

    Looks like it is an integration problem really, since the search widget only show results for the portuguese (main) products even when browsing in english (secondary). And also the page “no products found” not being translated.

    Plugin Author Hyyan Abo Fakher

    (@hyyan)

    @ankur Perm the problem does not happens on my local server , it may be my server only , but I’ve could you please do the following and tell me if that helped :

    from polylang settings page disable the following options :

    Detect browser language
    Hide URL language information for default language

    Thread Starter Ankur Prem

    (@ankur-prem)

    The problems persists – but let me do a fresh install of the store tomorrow and see what happens.

    Plugin Author Hyyan Abo Fakher

    (@hyyan)

    @ankur Prem OK +1

    Thread Starter Ankur Prem

    (@ankur-prem)

    Hyyan, looks like disabling the “hide url for default” did the trick – perfect results and only for the active language! but I really would want to hide it for the default language, is that possible?

    Thread Starter Ankur Prem

    (@ankur-prem)

    And just for you to know, the Polylang option to set the language by content (the first option in the URL modifications) leads to the exact same problem (plus the double result for product name). It have to be set to the directory option (second, which insert the language code (en/pt) as a directory in the url). Maybe is good to state that in the install notes.

    Thread Starter Ankur Prem

    (@ankur-prem)

    And, by the way, the “detect browser language” doesn’t affect anything, can be checked or not – both works fine.

    Plugin Author Hyyan Abo Fakher

    (@hyyan)

    @ankur Prem thank you for your feedback , you are correct about the Polylang option to set the language by content , it is not supported yet , and I think I’ve to mention that in the install note.

    I’ve digged more in the polylang code and I found it has a special support for wordpress search form only when it is defined in the searchform.php template file. nothing more than that.

    I’ve added and experiment feature to support the woo searchform in the same way. I think it will solve the issue even when the polylang option hide url for default is enabled.

    Could you please past this code in your theme’s functions.php file and provide me with the results .

    add_filter('get_product_search_form', function($form) {
    
        global $polylang;
    
        if ($form) {
            if ($polylang->links_model->using_permalinks) {
                // take care to modify only the url in the <form> tag
                preg_match('#<form.+>#', $form, $matches);
                $old = reset($matches);
                $new = preg_replace('#' . $polylang->links_model->home . '\/?#', $polylang->curlang->search_url, $old);
                $form = str_replace($old, $new, $form);
            } else
                $form = str_replace('</form>', '<input type="hidden" name="lang" value="' . esc_attr($polylang->curlang->slug) . '" /></form>', $form);
        }
        return $form;
    });

    Thank you again for your support

Viewing 15 replies - 1 through 15 (of 40 total)
  • The topic ‘Duplicated search result’ is closed to new replies.