• I’m struggling with a solution for changing images based on selected colors. I have 3 types of attributes: Color, Size, and Branches. My swatch image works when all these 3 types will be matched, for instance: the image will be changed when you’ll select: color + size + branch. But I want to force it somehow to change an image based on selected color and ignore the other 2 attributes.
    Is it possible to come up with this solution? any suggestions?

    thanks in advance for any tips.

    Cheers.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Support con

    (@conschneider)

    Engineer

    Hi there,

    Is it possible to come up with this solution? any suggestions?

    Yes, this is WooCommerce default behaviour. A variation is sum of all parts; it won’t match a variable product with images until all attributes are chosen. If you want an image to appear immediately, set the ‘defaults’ for size (beneath the variations).

    Anything else will need custom coding. Or a plugin that suits this, but I could not find one. Anybody else maybe?

    Kind regards,

    Thread Starter zimmerx

    (@zimmerx)

    Thanks for the reply.. You’re absolutely right, it’s woocommerce default behavior.

    I also tried to find any solution, but without success. I start trying to find how it works and come up with this code.

    if( defined('DOING_AJAX') && DOING_AJAX && ! isset($etheme_global['quick_view']) ) {
        if( ! empty( $_REQUEST['product_id'] ) ) {
            $post_id = (int) $_REQUEST['product_id'];
            setup_postdata( $post_id );
            $main_attachment_id = get_post_thumbnail_id( $post_id );
            $product = wc_get_product($post_id);
            if( ! empty($_REQUEST['option']) ) {
                $option = esc_attr($_REQUEST['option']);
                $attributes = $product->get_attributes();
                $variations = $product->get_available_variations();
                $images = '';
                $thumb = '';
                $attachment_ids = array();
    
                foreach($variations as $variation) {
                    if( isset( $variation['attributes']['attribute_' . $swatch] ) && $variation['attributes']['attribute_' . $swatch] == $option && has_post_thumbnail( $variation['variation_id'] ) ) {
                        $main_attachment_id = get_post_thumbnail_id( $variation['variation_id'] );
                    }
                }
    
            }
        }
    
    } else {
        $attachment_ids = $product->get_gallery_image_ids();
        $main_attachment_id = get_post_thumbnail_id( $post_id );
    }

    What if $variations = $product->get_available_variations(); change to $variations = $product->get_available_variations(‘pa_color’);.. does this make a trick ?

    I’m not good at PHP at all :S

    Plugin Support John Coy a11n

    (@johndcoy)

    Automattic Happiness Engineer

    Hi @zimmerx

    I am not sure what you’d like to accomplish. By default, the image will change based on the image added to the variation. If you’d like to have additional variation images, you can likely use a plugin like:

    WooCommerce Additional Variation Images

    or

    Variation Swatches and Photos which has a color swatch option.

    Thread Starter zimmerx

    (@zimmerx)

    @johndcoy
    Thank you for your reply.
    But I still can’t accomplish it.
    Let me describe my situation in more detail.

    I have a product which has sizes + color, for instance I have Shoe :
    Black shoes – sizes : 40-41-42-43-44-45
    White shoes – sizes : 40-41-42

    So, instead of uploading every white shoe to the size variations 40-41-42 I want to upload it once, just for the color, and don’t do the same for the black one.

    The solution for choosing White with any attribute is not an option for me as sizes for the different colors might be different.

    It’s very annoying to upload 50 variations with just only for 2 colors if that makes a sense.

    There must be some workaround for this task, I’m quite sure that I’m not the only one.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Variation Images based on color attribute’ is closed to new replies.