HomePage Forums Themeforest Petpuzzy – Pet Shop WooCommerce Theme Fix AJAX search results showing results in multiple languages w/ WPML installed

Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #10947
    Avataranto80
    Support Expired

    Hello,

    My website is running WPML with 2 languages. The issue I have is related to the AJAX search results that appear once you type a few letters in the search field, which are showing results for both languages.

    For example, if you type “fridge” into the search field, it will begin showing results once “fri” is typed, and those results will show products in 2 languages because the letters “fri” appear in the product titles of both languages.

    Can you modify the JavaScript code responsible for making the AJAX requests so that it includes the current language information and passes it to the server when requesting search results?

    Hope this is an easy fix, thanks in advance for your support!
    Anthony

    #10955

    Please add this custom code to the file functions.php:

    function petpuzzy_ajax_search_products() {
            global $woocommerce;
            $search_keyword = $_REQUEST['query'];
    
            $ordering_args = $woocommerce->query->get_catalog_ordering_args('date', 'desc');
            $suggestions = array();
    
            $args = array(
                's'                   => apply_filters('petpuzzy_ajax_search_products_search_query', $search_keyword),
                'post_type'           => 'product',
                'post_status'         => 'publish',
                'ignore_sticky_posts' => 1,
                'orderby'             => $ordering_args['orderby'],
                'order'               => $ordering_args['order'],
                'posts_per_page'      => apply_filters('petpuzzy_ajax_search_products_posts_per_page', 8),
                'supress_filters' => true
    
            );
    
            $args['tax_query']['relation'] = 'AND';
    
            if (!empty($_REQUEST['product_cat'])) {
                $args['tax_query'][] = array(
                    'taxonomy' => 'product_cat',
                    'field'    => 'slug',
                    'terms'    => strip_tags($_REQUEST['product_cat']),
                    'operator' => 'IN'
                );
            }
    
            $products = get_posts($args);
    
            if (!empty($products)) {
                foreach ($products as $post) {
                    $product = wc_get_product($post);
                    $product_image = wp_get_attachment_image_src(get_post_thumbnail_id($product->get_id()));
    
                    $suggestions[] = apply_filters('petpuzzy_suggestion', array(
                        'id'    => $product->get_id(),
                        'value' => strip_tags($product->get_title()),
                        'url'   => $product->get_permalink(),
                        'img'   => esc_url($product_image[0]),
                        'price' => sprintf('<span class="price">%s</span>', $product->get_price_html()),
                    ), $product);
                }
            } else {
                $suggestions[] = array(
                    'id'    => -1,
                    'value' => esc_html__('No results', 'petpuzzy'),
                    'url'   => '',
                );
            }
            wp_reset_postdata();
    
            echo json_encode($suggestions);
            die();
        }
    #10957
    Avataranto80
    Support Expired

    Thank you for the quick reply, but unfortunately that code did not work.

    The code is in my functions.php file, but when I type “fri” in the search field on the English website, the suggestions also include French results.

    Is there another snippet of code I can try?

    #10959

    Please send the WordPress admin web account for me, I will check and try to help you.

    #10964
    Avataranto80
    Support Expired
    This reply has been marked as private.
    #10978

    I can’t login to the admin dashboard, please recheck your account: https://prnt.sc/ArXB4cOGB7jx

    #10984
    Avataranto80
    Support Expired
    This reply has been marked as private.
    #10990

    I fixed this issue, please recheck your site.

    #10992
    Avataranto80
    Support Expired

    Great news!
    Can you explain what you did so that I understand the fix?

    #10993
    Avataranto80
    Support Expired
    This reply has been marked as private.
    #10999

    1. The previous code had an error so I fixed it: https://prnt.sc/-1Rzz3RcGNrD

    2. It is possible but you will have to pay a fee for this request

    #11000
    Avataranto80
    Support Expired
    This reply has been marked as private.
    #11007

    Ok, let me know if you need to help.

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

You must be logged in to reply to this topic.