Display Dynamic SKU on configurable product view Magento

Display Dynamic SKU on configurable product view Magento

July 14, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Display Dynamic SKU on configurable product view Magento

Displaying Dynamic SKU on Configurable Product View in Magento

Hello developers! I understand your frustration when dealing with dynamic product attributes like SKUs on configurable product views in Magento. Let’s dive into the issue you’re facing and find a solution together.

The Current Situation

You’ve shared some JavaScript code that aims to load a list of SKUs and update a div element whenever an option is selected from the attribute dropdown on a configurable product page. While the script seems to be loading the correct SKU, it’s not triggering any updates when you change the selected option.

The Problem

The issue lies in how you’re managing the event listener for the onchange event of your select element. In your current implementation, the JavaScript function changeSku(this) is called correctly, but it’s not updating the SKU display as expected.

Let me point out a few potential problems with your existing code:

  1. You’ve used Prototype.js syntax like document.observe("dom:loaded", ...) which might not be compatible with jQuery or plain JavaScript environments in modern Magento installations.

  2. The $ symbol is being used for both Prototype.js and jQuery, leading to potential conflicts if jQuery is enabled on the page.

  3. The changeSku(this) function is correctly structured but lacks some necessary checks before updating the SKU display.

A Step-by-Step Solution

To resolve this issue, we need to refactor your JavaScript code while ensuring compatibility with Magento’s default environment. Here’s a step-by-step approach:

  1. Ensure jQuery is available: In modern Magento installations, jQuery is enabled by default. To avoid conflicts, let’s switch to using jQuery syntax consistently.

  2. Refactor the SKU update logic: We’ll revise the changeSku function to handle edge cases and ensure the SKU display updates correctly regardless of whether an option is selected or not.

  3. Use Magento’s default event listener mechanism: Instead of relying on onchange, we can use Magento’s built-in event listeners that are already set up for configurable products.

Refactoring Your JavaScript Code

Here’s a refactored version of your JavaScript code with the suggested improvements:

// Ensure jQuery is available
if (typeof jQuery !== 'undefined') {
    $(document).ready(function() {
        // Initialize the SKU display container
        $('#sku-container').html('<strong>Product Id: </strong> Select an option to display Product Id');

        $('.super-attribute-select').change(function() {
            var selectedOption = this.value;

            if (selectedOption === '') {
                // If no option is selected, display the default message
                $('#sku-container').html('<strong>Product Id: </strong> Select an option to display Product Id');
            } else {
                // Find the corresponding SKU from the itemId array
                var skus = <?php echo json_encode($itemId); ?>;
                var selectedSku = '';

                for (var i = 0; i < skus.length; i++) {
                    if (skus[i].hasOwnProperty(selectedOption)) {
                        selectedSku = skus[i][selectedOption];
                        break;
                    }
                }

                // Update the SKU display with the selected product's SKU
                $('#sku-container').html('<strong>Product Id: </strong>' + selectedSku);
            }
        });

        // Trigger the change event to update the initial SKU display
        $('.super-attribute-select').trigger('change');
    });
}

What We’ve Done

  1. Wrapped the entire script inside a jQuery document ready function to ensure it runs after the DOM is fully loaded.

  2. Used consistent syntax for selecting elements with jQuery’s $() function.

  3. Simplified the changeSku logic by using an array of SKUs and checking if the selected option exists as a property in that array.

  4. Added a check for when no option is selected, displaying the default message again.

  5. Triggered the change event manually after the script runs to update the initial SKU display correctly.

Conclusion

In conclusion, resolving this issue requires careful refactoring of your JavaScript code while ensuring compatibility with Magento’s environment and best practices.

By following the suggested changes and using jQuery syntax consistently, you should now be able to display dynamic SKUs on configurable product views without any conflicts or missing updates.

Remember that proper debugging and testing are key when working with JavaScript in Magento. Don’t hesitate to reach out if you encounter any further issues or have other questions about Magento development.

Happy coding! 💻🎉

Learn more about Magento development best practices on our blog at LaravelCompany.com

Catalog Database Configuration Pricing

Our network