Search with Enter press
-
I need help with with my search bar in header.
Firts:
If i search fo “Iphone 13” , the search correctly suggest in the dropdown, but it suggests all other models also.
Second:
I want, when i enter “Iphone 13” and click Enter button, that i see the page same is i would , when iclick on the “Iphone 13” in the dropdown. Because now if i click enter i dont get any results…
Thank you
The page I need help with: [log in to see the link]
Viewing 1 replies (of 1 total)
-
Hi,
- By default plugin use OR search logic. This means that inside search results you will see all products that contain at least one of the search words. Of course products that contain all searching words will have higher priority and will be visible at the top of search results.If you want to show in search results only products that contain all search words then you need to switch to AND search logic. But this feature is available only with the pro plugin version.
- As I see the problem can be in that you have only product categories as search results and no product results. Looks like your current theme doesn’t support showing categories inside the search results page. You can fix this by using the 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)
- You must be logged in to reply to this topic.