mirror of
https://gh.wpcy.net/https://github.com/mainwp/Code-Snippets-Functions.git
synced 2026-04-29 11:32:21 +08:00
https://stackoverflow.com/questions/64843768/display-first-gallery-image-as-product-thumbnail-in-woocommerce-cart/64845123
19 lines
619 B
Text
19 lines
619 B
Text
function filter_woocommerce_cart_item_thumbnail( $thumbnail, $cart_item, $cart_item_key ) {
|
|
// Get product
|
|
$product = $cart_item['data'];
|
|
|
|
// Get gallery image ids
|
|
$attachment_ids = $product->get_gallery_image_ids();
|
|
|
|
// NOT empty
|
|
if ( ! empty ( $attachment_ids ) ) {
|
|
// First
|
|
$attachment_id = $attachment_ids[0];
|
|
|
|
// New thumbnail
|
|
$thumbnail = wp_get_attachment_image( $attachment_id, 'woocommerce_thumbnail' );
|
|
}
|
|
|
|
return $thumbnail;
|
|
}
|
|
add_filter( 'woocommerce_cart_item_thumbnail', 'filter_woocommerce_cart_item_thumbnail', 10, 3 );
|