Adding JS to Magento product page

Adding JS to Magento product page

August 22, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Adding JS to Magento product page

Modifying Magento Product Page Fixed Scrolling Effect

As a Magento developer, I often encounter requests from clients looking to customize their product pages. Recently, I received an inquiry about adding a fixed scrolling effect to the product summary section on the shop page. Let’s dive into how we can achieve this using jQuery and modify the appropriate XML and PHP files in your Magento installation.

Understanding the Problem

The user provided a JSFiddle link showcasing the desired behavior: http://jsfiddle.net/PXadg/

In this example, the product summary section (“Summary”) is fixed while scrolling down until it reaches a certain point before the footer. The code provided targets an element with the ID “summary” and applies the scrollToFixed plugin.

Step-by-Step Solution

  1. Include jQuery Library

Before adding our custom scripts, ensure that jQuery is properly included in your Magento theme. Open your app/design/frontend/{your_package}/{your_theme}/template/page/html/head.phtml file and add the following line within the <head> tags:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  1. Include ScrollToFixed Plugin

Download the ScrollToFixed plugin from GitHub (https://github.com/danielsternfeld/scrolltofixed) and save it to your theme’s JavaScript folder, typically located at app/design/frontend/{your_package}/{your_theme}/js/.

  1. Add Custom Scripts

Create a new file named custom.js in the same js/ folder. Open this file and paste the following code:

require(['jquery'], function($) {
    $(document).ready(function() {
        $('#summary').scrollToFixed({
            marginTop: $('.header').outerHeight() + 10,
            limit: $('.footer').offset().top - $('#summary').outerHeight() - 10
        });
    });
});
  1. Include Custom JavaScript File

Back in your head.phtml file, add the following line to include the custom.js file:

<script src="{{ base_url }}js/custom.js"></script>
  1. Ensure Proper Output in Product Page Template

Finally, verify that the “Summary” element is properly output in your product page template. Open app/design/frontend/{your_package}/{your_theme}/template/catalog/product/view.phtml and locate where the summary block is rendered. Ensure the element has an ID of “summary”.

<div id="summary">
    <!-- Summary content goes here -->
</div>

Conclusion

By following these steps, you should now have a fully functional fixed scrolling effect for the product summary section on your Magento shop page. Remember to clear the cache after making these changes.

Always ensure that you’re working with a backup of your website and theme before making any modifications. This approach allows for easy rollbacks if needed while providing a more stable development environment.

For further reading on Magento theme customization, I recommend checking out this comprehensive guide from my colleagues at LaravelCompany. It covers best practices and advanced techniques for working with Magento themes.

Happy coding! Let me know if you have any questions or need further assistance in the comments below.

Catalog Database API Extensions

Our network