简体   繁体   中英

Showing the main product image in the cart thumbnail, instead of the variation image in WooCommerce

I have a WooCommerce store that has products with multiple variations.

In the cart, it shows a thumbnail of the product variation image. This is not good, because the variation is actually just a text image, so you can't see the actual product the text is going onto.

How can I force Woo Commerce to show only the main product image, not the variation on the cart page?

Thanks!!

Add following code in active theme's functions.php .

function getCartItemThumbnail( $img, $cart_item ) {

    if ( isset( $cart_item['product_id'] ) ) {
        $product = wc_get_product($cart_item['product_id']);

        if ( $product && $product->is_type( 'variable' ) ) {
            // Return variable product thumbnail instead variation.
            return $product->get_image();
        }
    }

    return $img;
}

add_filter( 'woocommerce_cart_item_thumbnail', 'getCartItemThumbnail', 111, 2 );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM