diff --git a/api/class-wc-rest-dev-product-variations-controller.php b/api/class-wc-rest-dev-product-variations-controller.php index 853aea3..92cc71b 100644 --- a/api/class-wc-rest-dev-product-variations-controller.php +++ b/api/class-wc-rest-dev-product-variations-controller.php @@ -76,7 +76,7 @@ public function prepare_object_for_response( $object, $request ) { ), 'shipping_class' => $object->get_shipping_class(), 'shipping_class_id' => $object->get_shipping_class_id(), - 'image' => current( $this->get_images( $object ) ), + 'image' => $this->get_image( $object ), 'attributes' => $this->get_attributes( $object ), 'menu_order' => $object->get_menu_order(), 'meta_data' => $object->get_meta_data(), @@ -307,20 +307,24 @@ protected function prepare_object_for_database( $request, $creating = false ) { * @param WC_Product_Variation $variation Variation * @return array */ - protected function get_images( $variation ) { + protected function get_image( $variation ) { + if ( ! has_post_thumbnail( $variation->get_id() ) ) { + return; + } + $attachment_id = $variation->get_image_id(); $attachment_post = get_post( $attachment_id ); if ( is_null( $attachment_post ) ) { - $image = array(); + return; } $attachment = wp_get_attachment_image_src( $attachment_id, 'full' ); if ( ! is_array( $attachment ) ) { - $image = array(); + return; } if ( ! isset ( $image ) ) { - $image = array( + return array( 'id' => (int) $attachment_id, 'date_created' => wc_rest_prepare_date_response( $attachment_post->post_date, false ), 'date_created_gmt' => wc_rest_prepare_date_response( strtotime( $attachment_post->post_date_gmt ) ), @@ -332,7 +336,7 @@ protected function get_images( $variation ) { ); } - return array( $image ); + return; } /**