• Resolved snipsnip

    (@snipsnip)


    Hello, along with the product results, can I show the categories above them (that are relevant for the search)?
    For example, if I search for “Jeans”, 1st to show the category “Men Jeans”, after that the category “Woman Jeans” and right after that to start showing the products? And of course, before the categories to show a header “Categories” and after the categories and before the product search results to show a header “Products”:

    Categories
    Men Jeans
    Women Jeans

    Products
    Super slim blue men jeans Benetton
    Relaxed blue men jeans Sisley
    ….
    ….

Viewing 1 replies (of 1 total)
  • Plugin Author ILLID

    (@mihail-barinov)

    Hello,

    Well, such feature is not supported by default. But you can try to use following code snippet:

    class AWS_Add_Tax {
        public $loop_start_triggered = array();
        public $current_tax = false;
        public $tax_id = 0;
        public $tax = array();
        public $tax_count = 0;
        public function __construct() {
            add_action( 'woocommerce_shop_loop', array( $this, 'woocommerce_shop_loop' ) );
            add_action( 'woocommerce_before_shop_loop_item', array( $this, 'woocommerce_shop_loop' ) );
            add_filter( 'aws_search_page_results', array( $this, 'aws_search_page_results' ), 10, 3 );
            add_filter( 'woocommerce_loop_product_link', array( $this, 'woocommerce_loop_product_link' ), 9999999 );
            add_filter( 'the_permalink', array( $this, 'woocommerce_loop_product_link' ), 9999999 );
            add_filter( 'post_link', array( $this, 'woocommerce_loop_product_link' ), 9999999 );
            add_filter( 'post_type_link', array( $this, 'woocommerce_loop_product_link' ), 9999999 );
            add_filter( 'woocommerce_product_get_image', array( $this, 'woocommerce_product_get_image' ), 10, 4 );
            add_filter( 'woocommerce_placeholder_img_src', array( $this, 'woocommerce_placeholder_img_src' )  );
            add_filter( 'wp_get_attachment_image_src', array( $this, 'wp_get_attachment_image_src' ), 99999 );
            add_filter( 'woocommerce_loop_add_to_cart_link', array( $this, 'woocommerce_loop_add_to_cart_link' ), 10, 2 );
            add_filter( 'woocommerce_sale_flash', array( $this, 'woocommerce_sale_flash' ), 10, 3 );
            add_filter( 'woocommerce_get_price_html', array( $this, 'woocommerce_get_price_html' ), 10, 2 );
            add_filter( 'posts_request', array( $this, 'filter_posts_request' ), 99999, 2 );
        }
        function woocommerce_shop_loop() {
            global $product, $post;
            if ( $product ) {
                $id = $product->get_id();
                if ( $this->current_tax && isset( $this->loop_start_triggered[$id] ) ) {
                    return;
                }
                $this->current_tax = false;
                if ( isset( $_REQUEST['type_aws'] ) && isset( $this->tax[$id] ) ) {
                    foreach( $this->tax[$id] as $key => $tax_term ) {
                        $this->current_tax = $tax_term;
                        unset( $this->tax[$id][$key] );
                        $this->loop_start_triggered[$id] = true;
                        break;
                    }
                }
            }
        }
        public function aws_search_page_results( $posts, $query, $data ) {
            $new_posts = array();
            $product = get_posts( array(
                'posts_per_page' => 1,
                'numberposts' => 1,
                'fields' => 'ids',
                'post_type' => 'product',
                'post_status' => 'publish',
                'ignore_sticky_posts' => true,
                'suppress_filters' => true,
                'no_found_rows' => 1,
                'orderby' => 'ID',
                'order' => 'ASC',
                'lang' => ''
            ) );
            foreach ( $data['search_res'] as $search_res_fields ) {
                if ( isset( $search_res_fields['tax'] ) ) {
                    foreach ( $search_res_fields['tax'] as $tax_name => $tax_terms ) {
                        $this->tax_id = $product[0];
                        foreach ( $tax_terms as $tax_term ) {
                            $post = new stdClass();
                            $post->ID = $product[0];
                            $post->site_id = get_current_blog_id();
                            $post->post_name = $tax_term['name'];
                            $post->post_title = $tax_term['name'];
                            $post->permalink = $tax_term['link'];
                            $post->post_type = "product";
                            $post->post_author = "1";
                            $post->post_status = "publish";
                            $post->post_parent = 0;
                            $post->post_content = "";
                            $post->menu_order = 0;
                            $post->post_date = "2000-01-01 12:00:00";
                            $post->post_date_gmt = "2000-01-01 12:00:00";
                            $post->post_modified = "2000-01-01 12:00:00";
                            $post->post_modified_gmt = "2000-01-01 12:00:00";
                            $post->awssearch = true;
                            $post->aws_tax = true;
                            $post->aws_img = $tax_term['image'];
                            if ( $post ) {
                                $new_posts[] = $post;
                            }
                            $this->tax[$post->ID][] = $tax_term;
                        }
                    }
                }
            }
            if ( ! empty( $new_posts ) ) { 
                $posts = array_merge($new_posts, $posts);
            }
            $this->tax_count = count( $posts );
            return $posts;
        }
        public function woocommerce_loop_product_link( $link ) {
            global $post;
            if ( $post && property_exists( $post, 'aws_tax' ) && $post->aws_tax ) {
                return $post->permalink;
            }
            if ( $this->current_tax && isset( $this->current_tax['link'] ) ) {
                return $this->current_tax['link'];
            }
            return $link;
        }
        public function woocommerce_product_get_image( $image, $product, $size, $attr ) {
            global $post;
            if ( $post->aws_tax ) {
                $placeholder = wc_placeholder_img_src();
                return '<img src="' . $placeholder . '"/>';
            }
            if ( $this->current_tax ) {
                if ( isset( $this->current_tax['image'] ) ) {
                    return '<img src="' . esc_url( $this->current_tax['image'] ) . '"/>';;
                }
                return wc_placeholder_img( $size, $attr );
            }
            return $image;
        }
        public function woocommerce_placeholder_img_src( $src ) {
            if ( $post && property_exists( $post, 'aws_tax' ) && $post->aws_tax && $post->aws_img ) {
                return $post->aws_img;
            }
            return $src;
        }
        public function wp_get_attachment_image_src( $image ) {
            global $post;
            if ( $this->current_tax && $post && property_exists( $post, 'aws_tax' ) && $post->aws_tax && $post->aws_img ) {
                return array( $post->aws_img, '', '' );
            }
            if ( $this->current_tax && isset( $this->current_tax['id'] ) ) {
                $post_thumbnail_id = get_post_thumbnail_id( $this->current_tax['id'] );
                remove_filter( 'wp_get_attachment_image_src', array( $this, 'wp_get_attachment_image_src' ), 99999);
                $attachment_image = wp_get_attachment_image_src( $post_thumbnail_id );
                add_filter( 'wp_get_attachment_image_src', array( $this, 'wp_get_attachment_image_src' ), 99999);
                if ( $attachment_image ) {
                    $image = $attachment_image;
                }
            }
            return $image;
        }
        public function woocommerce_loop_add_to_cart_link( $output, $product ) {
            $id = $product->get_id();
            if ( isset( $_REQUEST['type_aws'] ) && isset( $this->tax[$id] ) ) {
                return '';
            }
            return $output;
        }
        public function woocommerce_sale_flash( $output, $post, $product ) {
            $id = $product->get_id();
            if ( isset( $_REQUEST['type_aws'] ) && isset( $this->tax[$id] ) ) {
                return '';
            }
            return $output;
        }
        public function woocommerce_get_price_html( $output, $product ) {
            $id = $product->get_id();
            if ( isset( $_REQUEST['type_aws'] ) && isset( $this->tax[$id] ) ) {
                return '';
            }
            return $output;
        }
        public function filter_posts_request( $request, $query ) {
            $query->found_posts = $this->tax_count;
            return $request;
        }
    }
    new AWS_Add_Tax();

    You need to add it somewhere outside the plugins folder. For example, inside functions.php file of your theme or use some plugin for adding code snippets.

    Regards

Viewing 1 replies (of 1 total)
  • The topic ‘Can I show product categories in the search results before the product results?’ is closed to new replies.