Vishal Tanna
Vishal Tanna

Reputation: 1305

woocommerce search on product attribute added by ACF using wordpress default search

In Wordpress default search functionality i want to search woocommerce search on product attribute. Please guide me on this. Thanks in advance.

Upvotes: 1

Views: 864

Answers (1)

Malhar
Malhar

Reputation: 76

You can do that by using product post meta. put the below code in no-products-found.php.

global $wpdb;
$item_code = get_search_query();

$sql = 'SELECT DISTINCT post_id FROM wp_postmeta WHERE meta_value LIKE "'.$item_code.'"';
$results = $wpdb->get_results($sql);
$pro = array();

foreach( $results as $result ){
     $pro[] = $result->post_id;
}

if($pro){ 
    $args = array( 'post_type' => 'product', 'post__in' => $pro );
    $loop = new WP_Query($args); 
    woocommerce_product_loop_start(); 
         woocommerce_product_subcategories(); 

         while ( $loop->have_posts() ) : $loop->the_post(); 

             wc_get_template_part( 'content', 'product' ); 

         endwhile; wp_reset_query();

       woocommerce_product_loop_end(); 
 } else { ?>

    <p class="woocommerce-info"><?php _e( 'No products were found matching your selection.', 'woocommerce' ); ?></p>

<?php } ?>

Upvotes: 3

Related Questions