Home Forums WoodMart support forum Thumbnail with ajax search

Thumbnail with ajax search

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #231905

    abdullahdarcin
    Participant

    Hi. How can i do Thumbnail with ajax search and i want to search for tags or others words ?

    Can you help me ?

    Thank you sir.

    #231912

    abdullahdarcin
    Participant

    I got it thumbnail but i have one more question. How can i search woocommerce variation, tags and others ?

    I added this code in function.php –>>

    function woodmart_ajax_suggestions() {
    
    		$allowed_types = array( 'post', 'product', 'portfolio' );
    		$post_type = 'product';
    
    		if ( apply_filters( 'woodmart_search_by_sku', woodmart_get_opt( 'search_by_sku' ) ) && woodmart_woocommerce_installed() ) {
    			add_filter( 'posts_search', 'woodmart_product_ajax_search_sku', 10 );
    		}
    		
    		$query_args = array(
    			'posts_per_page' => 5,
    			'post_status'    => 'publish',
    			'post_type'      => $post_type,
    			'no_found_rows'  => 1,
    		);
    
    		if ( ! empty( $_REQUEST['post_type'] ) && in_array( $_REQUEST['post_type'], $allowed_types ) ) {
    			$post_type = strip_tags( $_REQUEST['post_type'] );
    			$query_args['post_type'] = $post_type;
    		}
    
    		if ( $post_type == 'product' && woodmart_woocommerce_installed() ) {
    			
    			$product_visibility_term_ids = wc_get_product_visibility_term_ids();
    			$query_args['tax_query'][] = array(
    				'taxonomy' => 'product_visibility',
    				'field'    => 'term_taxonomy_id',
    				'terms'    => $product_visibility_term_ids['exclude-from-search'],
    				'operator' => 'NOT IN',
    			);
    
    			if ( ! empty( $_REQUEST['product_cat'] ) ) {
    				$query_args['product_cat'] = strip_tags( $_REQUEST['product_cat'] );
    			}
    		}
    
    		if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) ) {
    			$query_args['meta_query'][] = array( 'key' => '_stock_status', 'value' => 'outofstock', 'compare' => 'NOT IN' );
    		}
    
    		if ( ! empty( $_REQUEST['query'] ) ) {
    			$query_args['s'] = sanitize_text_field( $_REQUEST['query'] );
    		}
    
    		if ( ! empty( $_REQUEST['number'] ) ) {
    			$query_args['posts_per_page'] = (int) $_REQUEST['number'];
    		}
    
    		$results = new WP_Query( apply_filters( 'woodmart_ajax_search_args', $query_args ) );
    
    		$suggestions = array();
    
    		if ( $results->have_posts() ) {
    
    			if ( $post_type == 'product' && woodmart_woocommerce_installed() ) {
    				$factory = new WC_Product_Factory();
    			}
    
    			while ( $results->have_posts() ) {
    				$results->the_post();
    
    				if ( $post_type == 'product' && woodmart_woocommerce_installed() ) {
    					$product = $factory->get_product( get_the_ID() );
    
    					$suggestions[] = array(
    						'value' => get_the_title(),
    						'permalink' => get_the_permalink(),
    						'price' => $product->get_price_html(),
    						'thumbnail' => $product->get_image( array( 100, 100 ) ),
    					);
    				} else {
    					$suggestions[] = array(
    						'value' => get_the_title(),
    						'permalink' => get_the_permalink(),
    						'thumbnail' => get_the_post_thumbnail( null, 'medium', '' ),
    					);
    				}
    			}
    
    			wp_reset_postdata();
    		} else {
    			$suggestions[] = array(
    				'value' => ( $post_type == 'product' ) ? esc_html__( 'No products found', 'woodmart' ) : esc_html__( 'No posts found', 'woodmart' ),
    				'no_found' => true,
    				'permalink' => ''
    			);
    		}
    
    		echo json_encode( array(
    			'suggestions' => $suggestions
    		) );
    
    		die();
    	}
    
    	add_action( 'wp_ajax_woodmart_ajax_search', 'woodmart_ajax_suggestions', 10 );
    	add_action( 'wp_ajax_nopriv_woodmart_ajax_search', 'woodmart_ajax_suggestions', 10 );
    
    #231918

    Hello,

    AJAS search can be added to the header builder. By default, it finds the products by a keyword.

    If you need more options for the search you will have to use an additional plugin you can try this one: YITH WooCommerce Ajax Search.

    Best Regards

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