<?php
function bricks_append_product_gallery_images() {
    global $post;

    if (!$post || get_post_type($post->ID) !== 'product') {
        return '';
    }

    // Get product gallery images
    $gallery_images = get_post_meta($post->ID, '_product_image_gallery', true);

    // Return early if no gallery images found
    if (empty($gallery_images)) {
        return '';
    }

    $image_ids = explode(',', $gallery_images);
    $output = '';

    // Function to get image dimensions
    function get_image_dimensions($image_id) {
        $image = wp_get_attachment_metadata($image_id);
        return $image ? ['width' => $image['width'], 'height' => $image['height']] : ['width' => 800, 'height' => 800]; // Fallback size
    }

    // Loop through gallery images
    foreach ($image_ids as $image_id) {
        $image_url = wp_get_attachment_image_url($image_id, 'full');
        $image_size = get_image_dimensions($image_id);

        if ($image_url) {
            $output .= '<a href="' . esc_url($image_url) . '" class="brxe-image tag bricks-lightbox"
                data-pswp-src="' . esc_url($image_url) . '" 
                data-pswp-width="' . esc_attr($image_size['width']) . '" 
                data-pswp-height="' . esc_attr($image_size['height']) . '">
                <img src="' . esc_url($image_url) . '" class="css-filter size-large" />
            </a>';
        }
    }

    // Wrap the output inside the product-images div
    $output = '<script>
        document.addEventListener("DOMContentLoaded", function() {
            let productImagesDiv = document.querySelector(".product-images");
            if (productImagesDiv) {
                productImagesDiv.innerHTML += `' . addslashes($output) . '`;
            }
        });
    </script>';

    return $output;
}

// Register shortcode
add_shortcode('append_product_gallery_images', 'bricks_append_product_gallery_images');

?>
[append_product_gallery_images]
<?php
function custom_product_images_with_variations() {
    global $post, $product;
    if ( ! $post || get_post_type( $post->ID ) !== 'product' ) {
        return '';
    }

    // 1. Collect image IDs: featured image first, then gallery images (avoiding duplicates)
    $images = array();
    $featured_image_id = get_post_thumbnail_id( $post->ID );
    if ( $featured_image_id ) {
        $images[] = $featured_image_id;
    }
    $gallery_images = get_post_meta( $post->ID, '_product_image_gallery', true );
    if ( ! empty( $gallery_images ) ) {
        $gallery_ids = explode( ',', $gallery_images );
        foreach ( $gallery_ids as $id ) {
            if ( ! in_array( $id, $images ) ) {
                $images[] = $id;
            }
        }
    }
    
    // 2. Build a mapping of variation value (for Colors) to image ID.
    // This example assumes a variable product using the "Colors" attribute (attribute_pa_colors).
    $variation_mapping = array();
    if ( $product && $product->is_type( 'variable' ) ) {
        $available_variations = $product->get_available_variations();
        foreach ( $available_variations as $variation ) {
            if ( isset( $variation['attributes']['attribute_pa_colors'] ) && ! empty( $variation['attributes']['attribute_pa_colors'] ) ) {
                $color = strtolower( $variation['attributes']['attribute_pa_colors'] );
                $variation_image_id = $variation['image_id'];
                if ( $variation_image_id && ! isset( $variation_mapping[ $color ] ) ) {
                    $variation_mapping[ $color ] = $variation_image_id;
                }
            }
        }
    }
    
    // 3. Build the HTML for the images.
    // Each image is wrapped in an anchor that has the lightbox classes and data attributes.
    $html = '';
    foreach ( $images as $image_id ) {
        $image_url  = wp_get_attachment_image_url( $image_id, 'full' );
        $image_meta = wp_get_attachment_metadata( $image_id );
        $width  = isset( $image_meta['width'] ) ? $image_meta['width'] : 800;
        $height = isset( $image_meta['height'] ) ? $image_meta['height'] : 800;
        
        // Get alt text; fallback to the filename (without extension) if empty.
        $image_alt = get_post_meta( $image_id, '_wp_attachment_image_alt', true );
        if ( empty( $image_alt ) ) {
            $attachment = get_post( $image_id );
            $image_alt  = $attachment ? pathinfo( $attachment->guid, PATHINFO_FILENAME ) : '';
        }
        
        // If this image is used as a variation image, add a data attribute.
        $data_variation = '';
        $found_variation = array_search( $image_id, $variation_mapping );
        if ( $found_variation !== false ) {
            $data_variation = ' data-variation="' . esc_attr( $found_variation ) . '"';
        }
        
        if ( $image_url ) {
            // IMPORTANT: Use the classes your lightbox expects.
            $html .= '<a href="' . esc_url( $image_url ) . '" class="brxe-image tag bricks-lightbox"'. $data_variation .' data-pswp-src="' . esc_url( $image_url ) . '" data-pswp-width="' . esc_attr( $width ) . '" data-pswp-height="' . esc_attr( $height ) . '">';
            $html .= '<img src="' . esc_url( $image_url ) . '" alt="' . esc_attr( $image_alt ) . '" class="custom-image" />';
            $html .= '</a>';
        }
    }
    
    // 4. Wrap all images in a container.
    $output = '<div class="custom-product-images">'.$html.'</div>';
    
    return $output;
}
add_shortcode('custom_product_images', 'custom_product_images_with_variations');
?>
document.addEventListener("DOMContentLoaded", function(){
    var container = document.querySelector(".custom-product-images");
    if (!container) return;
    // Save the original order of the image anchors.
    var originalNodes = Array.from(container.children);

    function reorderImages(selectedValue) {
        // If no variation is selected, remove any highlight and restore original order.
        if (!selectedValue) {
            container.querySelectorAll("a.highlight").forEach(function(a) {
                a.classList.remove("highlight");
            });
            while (container.firstChild) {
                container.removeChild(container.firstChild);
            }
            originalNodes.forEach(function(node) {
                container.appendChild(node);
            });
            return;
        }
        // If a variation is selected, find the corresponding image link.
        var target = container.querySelector('a[data-variation="' + selectedValue.toLowerCase() + '"]');
        if (target) {
            container.querySelectorAll("a.highlight").forEach(function(a) {
                a.classList.remove("highlight");
            });
            target.classList.add("highlight");
            container.insertBefore(target, container.firstChild);
        }
    }

    // Listen for changes on the hidden select element.
    var variationSelect = document.querySelector("select[name='attribute_pa_colors']");
    if (variationSelect) {
        variationSelect.addEventListener("change", function(){
            var selected = variationSelect.value;
            reorderImages(selected);
        });
    }

    // Listen for clicks on radio-style variation selectors.
    var variationRadios = document.querySelectorAll("[data-attribute_name='attribute_pa_colors']");
    variationRadios.forEach(function(radio){
        radio.addEventListener("click", function(){
            // After the click, check if any radio still has the "selected" class.
            var selectedRadio = document.querySelector("li.selected[data-attribute_name='attribute_pa_colors']");
            if (selectedRadio) {
                var val = selectedRadio.getAttribute("data-value");
                reorderImages(val);
            } else {
                // If none are selected, clear the highlight and restore original order.
                reorderImages("");
            }
        });
    });

    // Listen for clicks on the clear variations link.
    var clearBtn = document.querySelector(".reset_variations");
    if (clearBtn) {
        clearBtn.addEventListener("click", function(e){
            e.preventDefault(); // Prevent default behavior if needed.
            // Remove "selected" class from all radio items.
            variationRadios.forEach(function(radio){
                radio.classList.remove("selected");
            });
            // Reset the select value.
            if (variationSelect) {
                variationSelect.value = "";
            }
            // Revert images to original order.
            reorderImages("");
        });
    }
});
7P2A55007P2A54977P2A55427P2A5494

Blouse-Giardino-Segreto

Le prix initial était : $ 126.34.Le prix actuel est : $ 63.17.

Blouse Giardino Segreto

Une expression de féminité raffinée, confectionnée en viscose fluide qui épouse la silhouette avec élégance. Son imprimé floral multicolore évoque la beauté d’un jardin secret, tandis que le col classique et la fermeture boutonnée apportent une touche intemporelle.

Le détail contrasté au bas sublime la pièce avec caractère et sophistication.


COMPOSITION
100% viscose

ORIGINE
Fabriqué en France

TAILLES
S/M – M/L – L/XL


LIVRAISON & RETOURS
Livraison standard sous 2 à 5 jours ouvrables
Retours sous 30 jours.

 

Similar to this