cancel
Showing results for 
Search instead for 
Did you mean: 

Pass Search Query to Product Page

SOLVED

Pass Search Query to Product Page

I'd like to pass the search query to the product page that is clicked on.

The reasoning is because if someone searches for a product, it may be on a grouped product page, so if they click on that result it just takes them to the top of that product page. I can add the anchor tags in the grouped product table, I just need the query passed so the page scrolls to that product.

For example, if someone searches for "Red Cup" and all of my cups are on a grouped product with 50 cups on, when they click the "Red Cup" on the results page it will take them to the top of the cup page. They've then got to scroll down to find "Red Cup" manually rather than the page scrolling to that product

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Pass Search Query to Product Page

Found it thank you.

The link now looks like:

<a class="product-item-link test1169"
href="<?php /* @escapeNotVerified */ echo $_product->getProductUrl() ?><?php if (isset($_GET['q'])) { echo "?q=" . $_GET['q'] . "#" . $_GET['q']; }; ?>"
title="<?php echo $this->stripTags($_product->getName(), null, true) ?>"> <?php /* @escapeNotVerified */ echo $_product->getSku() ?><br> <?php /* @escapeNotVerified */ echo $_helper->productAttribute($_product, $_product->getName(), 'name'); ?> </a>

 

View solution in original post

7 REPLIES 7

Re: Pass Search Query to Product Page

Hello @resurgem12 

 

you need to implement using cookie.

 

when somebody go on the search page then save that value into cookie, then on product page load one js and get value of that cookie and scroll it.

 

Don't try product page code using php file as all thing come from cache so it will not work so you must need to do using js

 

 

If the help you then mark as a solution.

 


Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer

Re: Pass Search Query to Product Page

Okay,

Is there no way to pass it as a url parameter. For example www.example.com/cups#red%20cup.

When the search query way red cup

Re: Pass Search Query to Product Page

yes possible but it will create an issue with layer navigation and all.

 

If you want to go using that way then into list.phtml and find q params from URL and append it into url so it will work

 

Hope it will help you


Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer

Re: Pass Search Query to Product Page

I can't seem to find the correct list.phtml.

I've tried: app/design/frontend/Alothemes/default/Magento_Catalog/templates/product/list.phtml

Could you point me in the correct direction?

Re: Pass Search Query to Product Page

Hello @resurgem12 

 

you can enable template path hint and you can find it.

 

 


Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer

Re: Pass Search Query to Product Page

Found it thank you.

The link now looks like:

<a class="product-item-link test1169"
href="<?php /* @escapeNotVerified */ echo $_product->getProductUrl() ?><?php if (isset($_GET['q'])) { echo "?q=" . $_GET['q'] . "#" . $_GET['q']; }; ?>"
title="<?php echo $this->stripTags($_product->getName(), null, true) ?>"> <?php /* @escapeNotVerified */ echo $_product->getSku() ?><br> <?php /* @escapeNotVerified */ echo $_helper->productAttribute($_product, $_product->getName(), 'name'); ?> </a>

 

Re: Pass Search Query to Product Page

Hello @resurgem12 

 

If works then mark as solution 


Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer