• Resolved semplicewebsite

    (@semplicewebsite)


    I tried this code to print the photo contained in the variation image as the main product image, but it doesn’t work. Anyone can help me? Thank in advance!

    // Aggiungi script JavaScript personalizzato
    function custom_wc_product_image_script() {
        // Verifica se siamo sulla pagina del singolo prodotto WooCommerce
        if (is_product()) { 
            // Output del codice JavaScript
            ?>
            <script type="text/javascript">
                document.addEventListener("DOMContentLoaded", function() {
                    var selectedSwatch = document.querySelector('.cfvsw-selected-swatch');
                    var imageUrl = selectedSwatch.querySelector('.cfvsw-swatch-inner').style.backgroundImage;
                    imageUrl = imageUrl.replace('url(','').replace(')','').replace(/\"/gi, "");
                    var mainProductImage = document.querySelector('.woocommerce div.product div.images .woocommerce-product-gallery__image img');
                    mainProductImage.setAttribute('src', imageUrl);
                });
            </script>
            <?php
        }
    }
    add_action('wp_footer', 'custom_wc_product_image_script');
    
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)

The topic ‘Main Product Image code’ is closed to new replies.