Saturday, 1 September 2018

Hide odd thumbnails in product gallery on attribute selection

I am trying to hide the odd thumbnails in my product pages on attribute selection.

When the attribute with the value Electronics Protection is selected, it should hide the odd thumbnails in the Yith Magnifier Gallery which has the CSS class yith_magnifier_gallery.

I found the code from the answer in the following thread: Show Message in Woocommerce Cart only for specific product variation Attribute

And I edited it a bit. But I feel that there are mistakes in my code because it's giving the following error when I put the code in functions.php.

This page isn’t working

powermatic7.com is currently unable to handle this request.

HTTP ERROR 500

Below is my code:

add_action( 'woocommerce_after_cart', 'hide_thumbs_based_on_variation' );
function hide_thumbs_based_on_variation() {
    $found = false;
    // Loop through cart items
    foreach ( WC()->cart->get_cart() as $cart_item ){
        $product = $cart_item['data'];
        if( ! $product->is_type('variation')){
            continue; // Jump to next cart item
        }
        $variation_attributes = $product->get_variation_attributes();
        foreach ( $variation_attributes as $variation_attribute => $term_value ){
            $attribute_slug = str_replace('attribute_pa_', '', $variation_attribute);

            if( $term_value == 'Electronics Protection' ){
                $found = true;
                break;
            }
        }
    }
    if($found){
        $(".yith_magnifier_gallery img:nth-child(odd)").hide();
    }
}

And the HTML code of the Yith Magnifier Gallery:

<div class="yith_magnifier_gallery">
<img src="#" />
<img src="#" />
<img src="#" />
<img src="#" />
<img src="#" />
<img src="#" />
</div>

Any help is greatly appreciated.



from Hide odd thumbnails in product gallery on attribute selection

No comments:

Post a Comment