Need Help Displaying Variation Price and Shipping Icon in Quick View (Swatches)

Solved6 replies2 participantsUpdated by Juan Alberto Montoya

  • Avatar: Juan Alberto Montoya
    Juan Alberto Montoya
    Participant

    Hello support team,

    I’ve created a custom solution that displays the variation price and a “⚡” icon for each variation that uses a shipping class containing “48h”. It works correctly on the product page by parsing a JSON map of prices, stock, and shipping class, then injecting the information visually inside the swatch buttons (.st-custom-attribute). Here is a simplified example of the script I use:

    
    <!-- Maps injected as JSON -->
    <script id="price-map" type="application/json">{...}</script>
    <script id="shipping-class-map" type="application/json">{...}</script>
    <script id="stock-map" type="application/json">{...}</script>
    
    <!-- Swatch Enhancement Script -->
    <script>
      // Functions to inject price, shipping icon and stock limit into swatches
      ...
    </script>
    
    

    The system works fine on the regular product page (single-product), but it doesn’t work in the Quick View popup, even though the swatches render visually. I assume this is because the JSON <script> tags or the DOMContentLoaded events don’t trigger properly inside the dynamic Quick View content.

    Could you please guide me on how to properly hook or initialize my script so it works inside the Quick View popup context?
    I’d like to keep the logic I built, just need help adapting it to your Quick View system. Alternatively, is there a native hook in your theme/plugin where I can inject the price and shipping icon data directly from PHP into the swatch rendering process (st-label-swatch)?

    Thank you in advance for your support!

    Best regards

    Files is visible for topic creator and
    support staff only.

    6 Answers

    Avatar: Justin
    Luca Rossi
    Support staff

    Hello @Juan Alberto Montoya,

    Thank you for reaching out to us and for providing a detailed explanation of your custom implementation.

    We understand that your script works correctly on the single product page but does not function as expected within the Quick View popup. This behavior is likely due to the fact that the Quick View content is loaded dynamically via AJAX, and therefore, the DOMContentLoaded event does not fire again when the popup is opened. As a result, your script does not reinitialize in that context.

    To resolve this, we recommend the following to reinitialize Your Script on Quick View Load
    Our theme triggers a custom event when the Quick View content is loaded. You can listen for this event and re-run your script accordingly. Please use the following JavaScript snippet as a starting point:

    
    jQuery(document).on('etheme_quick_view_content_loaded', function() {
        // Your custom script logic here
    });
    

    This event is triggered after the Quick View content is fully loaded and inserted into the DOM. You can use this hook to parse your JSON maps and inject the price and shipping icon into the swatches within the Quick View.

    We hope this helps you adapt your custom solution to work seamlessly with the Quick View feature. Please don’t hesitate to contact us again if you have any additional questions or need further clarification.

    Best Regards,
    8Theme’s Team

    Avatar: Juan Alberto Montoya
    Juan Alberto Montoya
    Participant

    Thanks for your solution. Now my code works on quick view too, the problem is that, the values inserted (fast shipping and price of size) are the same values of the current product in the product page, not the product opened in the quick view. how can i solve this?

    
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css" />
    
    <script>
    document.addEventListener("DOMContentLoaded", function () {
        initSwatchEnhancements();
    });
    
    // Reactivar en el Quick View
    jQuery(document).on('etheme_quick_view_content_loaded', function () {
        setTimeout(initSwatchEnhancements, 200); // esperar un poco para que el DOM esté completo
    });
    
    function initSwatchEnhancements() {
        addFastShippingIcon();
        styleFastShippingSwatches();
        addPriceToSwatches();
        updateQuantityMaxBySelectedSwatch();
        styleFastShippingDescription();
    }
    
    function addFastShippingIcon() {
        const shippingMapEl = document.getElementById('shipping-class-map');
        if (!shippingMapEl) return;
    
        const shippingMap = JSON.parse(shippingMapEl.textContent);
        document.querySelectorAll('.st-custom-attribute').forEach(swatch => {
            const talla = swatch.getAttribute('data-value');
            if (!talla || swatch.querySelector('.envio-info')) return;
    
            if (shippingMap[talla] && shippingMap[talla].toLowerCase().includes('48')) {
                const icon = document.createElement('div');
                icon.className = 'envio-info';
                icon.style.position = 'absolute';
                icon.style.top = '5px';
                icon.style.left = '5px';
                icon.style.textAlign = 'center';
                icon.innerHTML = '<i class="fa-solid fa-bolt-lightning" style="color:#C8FF0B; font-size:10px;"></i>';
                swatch.style.position = 'relative';
                swatch.appendChild(icon);
            }
        });
    }
    
    function styleFastShippingSwatches() {
        document.querySelectorAll('ul.st-swatch-size-large li.type-label').forEach(li => {
            const span = li.querySelector('span');
            if (span && span.querySelector('.envio-info')) {
                li.style.backgroundColor = '#F9FDED';
            }
        });
    }
    
    function styleFastShippingDescription() {
        document.querySelectorAll('.woocommerce-variation-description p').forEach(p => {
            if (p.textContent.includes('48')) {
                p.style.color = '#000000';
                p.style.backgroundColor = '#F9FDED';
                p.style.padding = '10px';
                p.style.border = '2px solid #C8FF0B';
                p.style.borderRadius = '5px';
            }
        });
    }
    
    function addPriceToSwatches() {
        const priceMapEl = document.getElementById('price-map');
        if (!priceMapEl) return;
    
        const priceMap = JSON.parse(priceMapEl.textContent);
        document.querySelectorAll('.st-custom-attribute').forEach(swatch => {
            const talla = swatch.getAttribute('data-value');
            if (!talla || swatch.querySelector('.precio-info')) return;
    
            if (priceMap[talla]) {
                const priceDiv = document.createElement('div');
                priceDiv.className = 'precio-info';
                priceDiv.style.fontSize = '11px';
                priceDiv.style.color = '#000';
                priceDiv.style.textAlign = 'center';
                priceDiv.style.marginTop = '3px';
                priceDiv.style.fontWeight = 'normal';
                priceDiv.innerHTML = priceMap[talla];
                swatch.appendChild(priceDiv);
            }
        });
    }
    
    function updateQuantityMaxBySelectedSwatch() {
        const stockMapEl = document.getElementById('stock-map');
        if (!stockMapEl) return;
    
        const stockMap = JSON.parse(stockMapEl.textContent);
        const selectedSwatch = document.querySelector('li.type-label.subtype-square.selected .st-custom-attribute');
        const quantityInput = document.querySelector('input[name="quantity"]');
        if (!selectedSwatch || !quantityInput) return;
    
        const talla = selectedSwatch.getAttribute('data-value');
        const stock = stockMap[talla];
    
        if (stock && stock > 0) {
            quantityInput.max = stock;
            if (parseInt(quantityInput.value) > stock) {
                quantityInput.value = stock;
            }
            quantityInput.disabled = false;
        } else {
            quantityInput.max = 0;
            quantityInput.value = 0;
            quantityInput.disabled = true;
        }
    }
    </script>
    
    
    Avatar: Juan Alberto Montoya
    Juan Alberto Montoya
    Participant

    This is a screenshot of the problem

    Files is visible for topic creator and
    support staff only.
    Avatar: Juan Alberto Montoya
    Juan Alberto Montoya
    Participant

    There is a way to make this directly in php? i tried to edit “class-st-woo-product-attributes.php” and also “st-woo-swatches.php” but literally it didn’t do nothing

    Avatar: Justin
    Luca Rossi
    Support staff

    Hi @Juan Alberto Montoya,

    In this case, you have to create the JSON code for each product.

    Something likes this:

    <!-- Maps injected as JSON -->
    <script id="price-map-product-123" type="application/json">{...}</script>
    <script id="shipping-class-map-product-123" type="application/json">{...}</script>
    <script id="stock-map-product-123" type="application/json">{...}</script>
    
    <script id="price-map-product-456" type="application/json">{...}</script>
    <script id="shipping-class-map-product-456" type="application/json">{...}</script>
    <script id="stock-map-product-456" type="application/json">{...}</script>

    Hope it helps!

    Avatar: Juan Alberto Montoya
    Juan Alberto Montoya
    Participant

    Thanks for the support! My topic “Need Help Displaying Variation Price and Shipping Icon in Quick View (Swatches)” has been successfully resolved.

The issue related to '‘Need Help Displaying Variation Price and Shipping Icon in Quick View (Swatches)’' has been successfully resolved, and the topic is now closed for further responses