Magento – Back button on product details page after search results

magento-1.7product

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.

Best Answer

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.

Related Topic