Skip to content
This repository was archived by the owner on Oct 4, 2019. It is now read-only.

Commit a0cb7e5

Browse files
author
Justin Shreve
committed
Don't return parent image when no variation image is set.
1 parent 1cff6c6 commit a0cb7e5

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

api/class-wc-rest-dev-product-variations-controller.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function prepare_object_for_response( $object, $request ) {
7676
),
7777
'shipping_class' => $object->get_shipping_class(),
7878
'shipping_class_id' => $object->get_shipping_class_id(),
79-
'image' => current( $this->get_images( $object ) ),
79+
'image' => $this->get_image( $object ),
8080
'attributes' => $this->get_attributes( $object ),
8181
'menu_order' => $object->get_menu_order(),
8282
'meta_data' => $object->get_meta_data(),
@@ -311,20 +311,24 @@ protected function prepare_object_for_database( $request, $creating = false ) {
311311
* @param WC_Product_Variation $variation Variation
312312
* @return array
313313
*/
314-
protected function get_images( $variation ) {
314+
protected function get_image( $variation ) {
315+
if ( ! has_post_thumbnail( $variation->get_id() ) ) {
316+
return;
317+
}
318+
315319
$attachment_id = $variation->get_image_id();
316320
$attachment_post = get_post( $attachment_id );
317321
if ( is_null( $attachment_post ) ) {
318-
$image = array();
322+
return;
319323
}
320324

321325
$attachment = wp_get_attachment_image_src( $attachment_id, 'full' );
322326
if ( ! is_array( $attachment ) ) {
323-
$image = array();
327+
return;
324328
}
325329

326330
if ( ! isset ( $image ) ) {
327-
$image = array(
331+
return array(
328332
'id' => (int) $attachment_id,
329333
'date_created' => wc_rest_prepare_date_response( $attachment_post->post_date, false ),
330334
'date_created_gmt' => wc_rest_prepare_date_response( strtotime( $attachment_post->post_date_gmt ) ),
@@ -336,7 +340,7 @@ protected function get_images( $variation ) {
336340
);
337341
}
338342

339-
return array( $image );
343+
return;
340344
}
341345

342346
/**

0 commit comments

Comments
 (0)