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

Commit cfd6ae6

Browse files
author
Justin Shreve
authored
Merge pull request #5 from woocommerce/fix/variation-image-response
Don't return parent image for variations.
2 parents d000aa4 + c16f02c commit cfd6ae6

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(),
@@ -307,20 +307,24 @@ protected function prepare_object_for_database( $request, $creating = false ) {
307307
* @param WC_Product_Variation $variation Variation
308308
* @return array
309309
*/
310-
protected function get_images( $variation ) {
310+
protected function get_image( $variation ) {
311+
if ( ! has_post_thumbnail( $variation->get_id() ) ) {
312+
return;
313+
}
314+
311315
$attachment_id = $variation->get_image_id();
312316
$attachment_post = get_post( $attachment_id );
313317
if ( is_null( $attachment_post ) ) {
314-
$image = array();
318+
return;
315319
}
316320

317321
$attachment = wp_get_attachment_image_src( $attachment_id, 'full' );
318322
if ( ! is_array( $attachment ) ) {
319-
$image = array();
323+
return;
320324
}
321325

322326
if ( ! isset ( $image ) ) {
323-
$image = array(
327+
return array(
324328
'id' => (int) $attachment_id,
325329
'date_created' => wc_rest_prepare_date_response( $attachment_post->post_date, false ),
326330
'date_created_gmt' => wc_rest_prepare_date_response( strtotime( $attachment_post->post_date_gmt ) ),
@@ -332,7 +336,7 @@ protected function get_images( $variation ) {
332336
);
333337
}
334338

335-
return array( $image );
339+
return;
336340
}
337341

338342
/**

0 commit comments

Comments
 (0)