cancel
Showing results for 
Search instead for 
Did you mean: 

Single Search Result Redirect

SOLVED
   Did you know you can see the translated content as per your choice?

Translation is in progress. Please check again after few minutes.

Single Search Result Redirect

If someone searches for a SKU only 1 result will show up.

Is it possible to then redirect the search page to that products page.

But only if there is only 1 result

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Single Search Result Redirect

Fixed!

I didn't add "die();" after the header('Location'...).

 

Final code looks like this:

<?php elseif ($_productCollection->count() == 1): // CHANGE THIS NUMBER BACK TO 1 TO SHOW WHEN ONLY 1 PRODUCT ?> 
	<div class="message info empty"><div>Single Result - Redirecting - <a href="<?php foreach ($_productCollection as $_product): ?><?php /* @escapeNotVerified */ echo $_product->getProductUrl() ?><?php if (isset($_GET['q'])) { echo "?q=" . $_GET['q'] . "#" . $_GET['q']; }; ?><?php endforeach; ?>">Click Here if you're not redirected</a></div></div>
	<?php foreach ($_productCollection as $_product): ?>
		<?php
			$linkRedirect = $_product->getProductUrl();
			if (isset($_GET['q'])) { 
				$linkRedirectParams = $linkRedirect . "?q=" . $_GET['q'] . "#" . $_GET['q'];
			} else {
				$linkRedirectParams = $linkRedirect;
			};
		?>
		<?php echo $linkRedirectParams; ?>
		<?php header('Location: '.$linkRedirectParams);die(); ?>
	<?php endforeach; ?>

View solution in original post

3 REPLIES 3

Re: Single Search Result Redirect

@resurgem12Yes, this is possible. I do not have a code snippet but did implemented the same few year ago. You have to check in your code that if search result is returning 1 result that forward to PDP instead of search result else show result page.

- Tarandeep
Problem solved?Please give 'Kudos' and accept 'Answer as Solution'.

Re: Single Search Result Redirect

I'm half way there.

I'm struggling to get the redirect working properly.

 

This is where im at.

<?php elseif ($_productCollection->count() == 1): ?>
	<div class="message info empty"><div>Single Result - Redirecting - <a href="<?php foreach ($_productCollection as $_product): ?><?php /* @escapeNotVerified */ echo $_product->getProductUrl() ?><?php if (isset($_GET['q'])) { echo "?q=" . $_GET['q'] . "#" . $_GET['q']; }; ?><?php endforeach; ?>">Click Here if you're not redirected</a></div></div>
	<?php foreach ($_productCollection as $_product): ?>
		<?php
			$linkRedirect = $_product->getProductUrl();
			if (isset($_GET['q'])) { 
				$linkRedirectParams = $linkRedirect . "?q=" . $_GET['q'] . "#" . $_GET['q'];
			} else {
				$linkRedirectParams = $linkRedirect;
			};
		?>
		<?php echo $linkRedirectParams; ?>
		<?php header('Location: '.$linkRedirectParams); ?>
		<?php $this->_redirect($linkRedirectParams); ?>
	<?php endforeach; ?>

Both the header('Location: '... and the $this_>_redirect()... aren't working to redirect the page

Re: Single Search Result Redirect

Fixed!

I didn't add "die();" after the header('Location'...).

 

Final code looks like this:

<?php elseif ($_productCollection->count() == 1): // CHANGE THIS NUMBER BACK TO 1 TO SHOW WHEN ONLY 1 PRODUCT ?> 
	<div class="message info empty"><div>Single Result - Redirecting - <a href="<?php foreach ($_productCollection as $_product): ?><?php /* @escapeNotVerified */ echo $_product->getProductUrl() ?><?php if (isset($_GET['q'])) { echo "?q=" . $_GET['q'] . "#" . $_GET['q']; }; ?><?php endforeach; ?>">Click Here if you're not redirected</a></div></div>
	<?php foreach ($_productCollection as $_product): ?>
		<?php
			$linkRedirect = $_product->getProductUrl();
			if (isset($_GET['q'])) { 
				$linkRedirectParams = $linkRedirect . "?q=" . $_GET['q'] . "#" . $_GET['q'];
			} else {
				$linkRedirectParams = $linkRedirect;
			};
		?>
		<?php echo $linkRedirectParams; ?>
		<?php header('Location: '.$linkRedirectParams);die(); ?>
	<?php endforeach; ?>