Merge pull request #27289 from woocommerce/fix/27222

use first gallery image on single product when image not assigned
This commit is contained in:
Ron Rennick 2020-10-07 15:06:30 -03:00 committed by GitHub
commit 67a54b3aa3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 10 deletions

View File

@ -1475,7 +1475,15 @@ if ( ! function_exists( 'woocommerce_show_product_images' ) ) {
* Output the product image before the single product summary.
*/
function woocommerce_show_product_images() {
wc_get_template( 'single-product/product-image.php' );
global $product;
$post_thumbnail_id = $product->get_image_id();
if ( ! $post_thumbnail_id ) {
$gallery_image_ids = $product->get_gallery_image_ids();
if ( ! empty( $gallery_image_ids ) ) {
$post_thumbnail_id = array_shift( $gallery_image_ids );
}
}
wc_get_template( 'single-product/product-thumbnails.php', array( 'post_thumbnail_id' => $post_thumbnail_id ) );
}
}
if ( ! function_exists( 'woocommerce_show_product_thumbnails' ) ) {
@ -1484,7 +1492,13 @@ if ( ! function_exists( 'woocommerce_show_product_thumbnails' ) ) {
* Output the product thumbnails.
*/
function woocommerce_show_product_thumbnails() {
wc_get_template( 'single-product/product-thumbnails.php' );
global $product;
$attachment_ids = $product->get_gallery_image_ids();
if ( $attachment_ids && ! $product->get_image_id() ) {
array_shift( $attachment_ids );
}
wc_get_template( 'single-product/product-thumbnails.php', array( 'attachment_ids' => $attachment_ids ) );
}
}

View File

@ -12,7 +12,7 @@
*
* @see https://docs.woocommerce.com/document/template-structure/
* @package WooCommerce\Templates
* @version 3.5.1
* @version 4.6.0
*/
defined( 'ABSPATH' ) || exit;
@ -25,12 +25,11 @@ if ( ! function_exists( 'wc_get_gallery_image_html' ) ) {
global $product;
$columns = apply_filters( 'woocommerce_product_thumbnails_columns', 4 );
$post_thumbnail_id = $product->get_image_id();
$wrapper_classes = apply_filters(
'woocommerce_single_product_image_gallery_classes',
array(
'woocommerce-product-gallery',
'woocommerce-product-gallery--' . ( $product->get_image_id() ? 'with-images' : 'without-images' ),
'woocommerce-product-gallery--' . ( $post_thumbnail_id ? 'with-images' : 'without-images' ),
'woocommerce-product-gallery--columns-' . absint( $columns ),
'images',
)
@ -39,7 +38,7 @@ $wrapper_classes = apply_filters(
<div class="<?php echo esc_attr( implode( ' ', array_map( 'sanitize_html_class', $wrapper_classes ) ) ); ?>" data-columns="<?php echo esc_attr( $columns ); ?>" style="opacity: 0; transition: opacity .25s ease-in-out;">
<figure class="woocommerce-product-gallery__wrapper">
<?php
if ( $product->get_image_id() ) {
if ( $post_thumbnail_id ) {
$html = wc_get_gallery_image_html( $post_thumbnail_id, true );
} else {
$html = '<div class="woocommerce-product-gallery__image--placeholder">';

View File

@ -12,7 +12,7 @@
*
* @see https://docs.woocommerce.com/document/template-structure/
* @package WooCommerce\Templates
* @version 3.5.1
* @version 4.6.0
*/
defined( 'ABSPATH' ) || exit;
@ -24,9 +24,7 @@ if ( ! function_exists( 'wc_get_gallery_image_html' ) ) {
global $product;
$attachment_ids = $product->get_gallery_image_ids();
if ( $attachment_ids && $product->get_image_id() ) {
if ( $attachment_ids ) {
foreach ( $attachment_ids as $attachment_id ) {
echo apply_filters( 'woocommerce_single_product_image_thumbnail_html', wc_get_gallery_image_html( $attachment_id ), $attachment_id ); // phpcs:disable WordPress.XSS.EscapeOutput.OutputNotEscaped
}