0

I am using below code to show back button on product details page after search results but the back button doesn't show if I go the details page after search results. I am now checking using $_product->getCategory() but i need some condition to show back button after search result as well.

<?php if ($_category = $_product->getCategory()): ?>

<div class="back-button">
    <button onclick="history.back();" class="button" title="<?php echo $this->__('Back') ?>" type="button">
        <span>
            <span><?php echo $this->__('Back') ?></span>
        </span>
    </button>
</div>
<?php endif;?>

I want to show back button on product details page when someone click product details page from search results.

1 Answer 1

1

Since you are using history.back() there is no need to check if a product has a category. When coming from the search results the product has no category attached to it.
So you can remove the if statement. This way the button will behave just like pressing back in the browser. It will take you to the previous page no mater what. Even if you clicked on a related product or if you come from the cart page.
EDIT
Here is a way to get the page referrer

Mage::app()->getRequest()->getServer('HTTP_REFERER');

You can check if the referred contains the string /catalogsearch/ and add it to your if statement.

6
  • if i removed all the condition then if anyone press directly the product details page link then also it shows back button. Commented Aug 28, 2013 at 8:15
  • I know... that's the down side. But in my opinion is a small risk
    – Marius
    Commented Aug 28, 2013 at 8:24
  • so can you give me some suggestion. My requirement is i want to show back button on product details page when someone click product details page from search results. Commented Aug 28, 2013 at 8:26
  • There is no way to determine from JS whether the user comes from the shop or not. But somewhere in magento (I can't find it) are the pages saved, the user visited in the past. You can just check there whether the user cames from a "local" link and if yes, show the back button. Then you only show it, if the user would not leave your page Commented Aug 28, 2013 at 8:28
  • @monojitbanik. I've edited my answer with a possible solution.
    – Marius
    Commented Aug 28, 2013 at 8:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.