Support » Plugin: WooCommerce » Search Query Returns Simple instead of Category Slug

  • Resolved paulanzr

    (@paulanzr)


    Currently when I search it gives me url like this:

    ....com/collections/simple/#product-support-Acerra

    I need to replace /simple/ with Collections slug
    here is the ending of the url for my collections

    taxonomy=collections&post_type=product

    in functions.php I have:

    function get_product_collection_slug($productID){
        global $wpdb;
         $search_query = 
                    "select slug from wp_terms te
                    inner join wp_term_taxonomy tt on te.term_id = tt.term_id
                    inner join wp_term_relationships tr on tr.term_taxonomy_id = tt.term_taxonomy_id
                    where tr.object_id = $productID;";                   
                
        $products = $wpdb->get_results( $search_query );
        
        wp_reset_postdata();
        
        return $products[0]->slug;
    }

    then in plugin.php I have:

    $products = $wpdb->get_results( $search_query );
                $product_support = get_option('product_support_flag');
               if ( !empty( $products ) ) {
                    foreach ( $products as $product ) {
                        
                            $targetId = strtolower($product->post_title);
    
                            if($product_support === '1') {
                                $targetId = "product-support-{$product->post_title}";
                            }
                            
                            $search_results[] = array(
                            'id'    => $product->ID,
                            'value' => $product->post_title,
                            'url'   => '/collections/'. get_product_collection_slug($product->ID).'/#'.$targetId
                        ); 
                    }
                }
                else {
                    $search_results[] = array(
                        'id'    => 0,
                        'value' => __( 'No results', 'ph-ajax-pro-search' ),
                        'url'   => '#',
                    );
                }
                wp_reset_postdata();
    
                $search_results = array(
                    'search_results' => $search_results
                );
    • This topic was modified 2 years, 12 months ago by paulanzr.
    • This topic was modified 2 years, 12 months ago by paulanzr.

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Search Query Returns Simple instead of Category Slug’ is closed to new replies.