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
Solved! Go to Solution.
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; ?>
@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.
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
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; ?>