Support » Plugin: Ajax Search for WooCommerce » Custom taxonomy

  • Resolved antonimac

    (@antonimac)


    Hi, how can I add the default wordpress taxonomy=category to the ajax search form results, just like taxonomy=product_cat?

    add_filter('dgwt/wcas/search_results/output', function($output){
        // Extra query for category..
        return $output;
    });
Viewing 1 replies (of 1 total)
  • Plugin Author Damian Gora

    (@damian-gora)

    The search doesn’t support the post category. I prepared for your solution, but I think it isn’t possible to support post categories in 100% right way without changing the plugin core code. Try it:

    
    add_filter('dgwt/wcas/search_results/output', function ($output) {
    
        $s = get_search_query();
    
        $terms = get_terms(array(
            'search'     => $s,
            'taxonomy'   => 'category',
            'hide_empty' => true
        ));
    
        $limit = 5;
        if ( ! empty($terms) && is_array($terms)) {
            foreach ($terms as $term) {
                if ($limit === 0) {
                    break;
                }
    
                array_unshift ($output['suggestions'], array(
                    'term_id'     => $term->term_id,
                    'taxonomy'    => 'product_cat', // You can't change it for category. Frontend doesn't handle other taxonomies
                    'value'       => $term->name,
                    'url'         => get_term_link($term->term_id, 'category'),
                    'breadcrumbs' => ''
                ));
    
                $limit--;
            }
        }
    
        return $output;
    });
    

    Best
    Damian Góra

Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.