1

This snippet works only in Title and i need to add also in Metakey SKU.

<?php

// Ajax Search.

function thb_ajax_search() {

check_ajax_referer( 'thb_autocomplete_ajax', 'security' );
$search_keyword              = filter_input( INPUT_GET, 'query', FILTER_SANITIZE_STRING );
$time_start                  = microtime( true );
$product_visibility_term_ids = wc_get_product_visibility_term_ids();
$ordering_args               = WC()->query->get_catalog_ordering_args('title', 'asc' );
$suggestions                 = array();
$args = array(
    's'                   => $search_keyword,
    'post_type'           => 'product',
    'post_status'         => 'publish',
    'ignore_sticky_posts' => 1,
    'posts_per_page'      => 3,
    'orderby'             => $ordering_args['orderby'],
    'order'               => $ordering_args['order'],
    'suppress_filters'    => false,
    'meta_key'          => '_sku',

    'tax_query'           => array(
        array(
            'taxonomy' => 'product_visibility',
            'field'    => 'term_taxonomy_id',
            'terms'    => $product_visibility_term_ids['exclude-from-search'],
            'operator' => 'NOT IN',
        ),
    ),
);
$products = get_posts( $args );

if ( ! empty( $products ) ) {
    foreach ( $products as $post ) {
        $product = wc_get_product( $post );

        $suggestions[] = array(
            'id'        => $product->get_id(),
            'value'     => wp_strip_all_tags( $product->get_title() ),
            'url'       => $product->get_permalink(),
            'thumbnail' => $product->get_image(),
            'price'     => $product->get_price_html(),
            'sku'       => $product->get_sku(),
        );
    }


}

 else {
    $suggestions = false;
}

$time_end    = microtime( true );
$time        = $time_end - $time_start;
$suggestions = array(
    'suggestions' => $suggestions,
    'time'        => $time,
);
echo wp_json_encode( $suggestions );
wp_die();}add_action( 'wp_ajax_nopriv_thb_ajax_search', 'thb_ajax_search' );add_action( 'wp_ajax_thb_ajax_search', 'thb_ajax_search' );

1 Answer 1

1

Pls Try this code

if(is_numeric($search_keyword)){
$args = array(
            'posts_per_page'  => 3,
            'post_type'       => 'product',
            'meta_query' => array(
                array(
                    'key' => '_sku',
                    'value' => $search_keyword,
                    'compare' => 'LIKE'
                )
            )
        );
}

else if(is_string($search_keyword)){
  $args = array(
    's'                   => $search_keyword,
    'post_type'           => 'product',
    'post_status'         => 'publish',
    'ignore_sticky_posts' => 1,
    'posts_per_page'      => 3,
    'orderby'             => $ordering_args['orderby'],
    'order'               => $ordering_args['order'],
    'suppress_filters'    => false,
    'tax_query'           => array(
        array(
            'taxonomy' => 'product_visibility',
            'field'    => 'term_taxonomy_id',
            'terms'    => $product_visibility_term_ids['exclude-from-search'],
            'operator' => 'NOT IN',
        ),
    ),
);}
1
  • Grt thanks for help.
    – DK Thakur
    Aug 5 at 9:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.