• Resolved nicetig

    (@nicetig)


    Hi All,

    For some reason, the search term is not displaying on my search result page and i have tried every setting but still couldn’t get it to appear. Not sure where i have missed.

    Thanks in advance.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author ILLID

    (@mihail-barinov)

    Hi,

    Well it depends on your current theme layout for the search results page but in most cases search results terms will not be showing. Only products search results.

    But you can try to use the following custom code snippet to show search terms:

    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();
    Thread Starter nicetig

    (@nicetig)

    Thanks, I will give it a go and let you know

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Display search term’ is closed to new replies.