Friday, August 5, 2022
HomeWordPress Developmentplugins - woocommerce search by sku and title ajax

plugins – woocommerce search by sku and title ajax


This snippet works solely in Title and i would like so as to add additionally in Metakey SKU.

<?php

// Ajax Search.

perform thb_ajax_search() {

check_ajax_referer( 'thb_autocomplete_ajax', 'safety' );
$search_keyword              = filter_input( INPUT_GET, 'question', 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' );
$ideas                 = 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',
            'discipline'    => 'term_taxonomy_id',
            'phrases'    => $product_visibility_term_ids['exclude-from-search'],
            'operator' => 'NOT IN',
        ),
    ),
);
$merchandise = get_posts( $args );

if ( ! empty( $merchandise ) ) {
    foreach ( $merchandise as $put up ) {
        $product = wc_get_product( $put up );

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


}

 else {
    $ideas = false;
}

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

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments