Skip to content
This repository was archived by the owner on Oct 4, 2019. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions api/class-wc-rest-dev-product-variations-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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 ) ),
Expand All @@ -332,7 +336,7 @@ protected function get_images( $variation ) {
);
}

return array( $image );
return;
}

/**
Expand Down