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

Commit 41ee2fc

Browse files
author
Justin Shreve
committed
If no position is provided and no featured image is set, set the first image as a product's featured image. This also removes the placeholder image from the API response.
1 parent 3e799c1 commit 41ee2fc

File tree

2 files changed

+151
-2
lines changed

2 files changed

+151
-2
lines changed

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

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* Handles requests to the /products/<product_id>/variations endpoints.
66
*
7-
* @author WooThemes
7+
* @author Automattic
88
* @category API
99
* @package WooCommerce/API
1010
* @since 3.0.0
@@ -28,4 +28,44 @@ class WC_REST_Dev_Product_Variations_Controller extends WC_REST_Product_Variatio
2828
*/
2929
protected $namespace = 'wc/v3';
3030

31+
/**
32+
* Get the image for a product variation.
33+
*
34+
* @param WC_Product_Variation $variation Variation
35+
* @return array
36+
*/
37+
protected function get_images( $variation ) {
38+
if ( has_post_thumbnail( $variation->get_id() ) ) {
39+
$attachment_id = $variation->get_image_id();
40+
} else {
41+
$attachment_id = current( $variation->get_gallery_image_ids() );
42+
}
43+
44+
$attachment_post = get_post( $attachment_id );
45+
if ( is_null( $attachment_post ) ) {
46+
$image = array();
47+
}
48+
49+
$attachment = wp_get_attachment_image_src( $attachment_id, 'full' );
50+
if ( ! is_array( $attachment ) ) {
51+
$image = array();
52+
}
53+
54+
if ( ! isset ( $image ) ) {
55+
$image = array(
56+
'id' => (int) $attachment_id,
57+
'date_created' => wc_rest_prepare_date_response( $attachment_post->post_date, false ),
58+
'date_created_gmt' => wc_rest_prepare_date_response( strtotime( $attachment_post->post_date_gmt ) ),
59+
'date_modified' => wc_rest_prepare_date_response( $attachment_post->post_modified, false ),
60+
'date_modified_gmt' => wc_rest_prepare_date_response( strtotime( $attachment_post->post_modified_gmt ) ),
61+
'src' => current( $attachment ),
62+
'name' => get_the_title( $attachment_id ),
63+
'alt' => get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ),
64+
'position' => (int) $position,
65+
);
66+
}
67+
68+
return array( $image );
69+
}
70+
3171
}

api/class-wc-rest-dev-products-controller.php

Lines changed: 110 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* Handles requests to the /products endpoint.
66
*
7-
* @author WooThemes
7+
* @author Automattic
88
* @category API
99
* @package WooCommerce/API
1010
* @since 2.6.0
@@ -28,4 +28,113 @@ class WC_REST_Dev_Products_Controller extends WC_REST_Products_Controller {
2828
*/
2929
protected $namespace = 'wc/v3';
3030

31+
/**
32+
* Get the images for a product or product variation.
33+
*
34+
* @param WC_Product|WC_Product_Variation $product Product instance.
35+
* @return array
36+
*/
37+
protected function get_images( $product ) {
38+
$images = array();
39+
$attachment_ids = array();
40+
41+
// Add featured image.
42+
if ( has_post_thumbnail( $product->get_id() ) ) {
43+
$attachment_ids[] = $product->get_image_id();
44+
}
45+
46+
// Add gallery images.
47+
$attachment_ids = array_merge( $attachment_ids, $product->get_gallery_image_ids() );
48+
49+
// Build image data.
50+
foreach ( $attachment_ids as $position => $attachment_id ) {
51+
$attachment_post = get_post( $attachment_id );
52+
if ( is_null( $attachment_post ) ) {
53+
continue;
54+
}
55+
56+
$attachment = wp_get_attachment_image_src( $attachment_id, 'full' );
57+
if ( ! is_array( $attachment ) ) {
58+
continue;
59+
}
60+
61+
$images[] = array(
62+
'id' => (int) $attachment_id,
63+
'date_created' => wc_rest_prepare_date_response( $attachment_post->post_date, false ),
64+
'date_created_gmt' => wc_rest_prepare_date_response( strtotime( $attachment_post->post_date_gmt ) ),
65+
'date_modified' => wc_rest_prepare_date_response( $attachment_post->post_modified, false ),
66+
'date_modified_gmt' => wc_rest_prepare_date_response( strtotime( $attachment_post->post_modified_gmt ) ),
67+
'src' => current( $attachment ),
68+
'name' => get_the_title( $attachment_id ),
69+
'alt' => get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ),
70+
'position' => (int) $position,
71+
);
72+
}
73+
74+
return $images;
75+
}
76+
77+
/**
78+
* Set product images.
79+
*
80+
* @throws WC_REST_Exception REST API exceptions.
81+
* @param WC_Product $product Product instance.
82+
* @param array $images Images data.
83+
* @return WC_Product
84+
*/
85+
protected function set_product_images( $product, $images ) {
86+
if ( is_array( $images ) ) {
87+
$gallery = array();
88+
89+
foreach ( $images as $image ) {
90+
$attachment_id = isset( $image['id'] ) ? absint( $image['id'] ) : 0;
91+
92+
if ( 0 === $attachment_id && isset( $image['src'] ) ) {
93+
$upload = wc_rest_upload_image_from_url( esc_url_raw( $image['src'] ) );
94+
95+
if ( is_wp_error( $upload ) ) {
96+
if ( ! apply_filters( 'woocommerce_rest_suppress_image_upload_error', false, $upload, $product->get_id(), $images ) ) {
97+
throw new WC_REST_Exception( 'woocommerce_product_image_upload_error', $upload->get_error_message(), 400 );
98+
} else {
99+
continue;
100+
}
101+
}
102+
103+
$attachment_id = wc_rest_set_uploaded_image_as_attachment( $upload, $product->get_id() );
104+
}
105+
106+
if ( ! wp_attachment_is_image( $attachment_id ) ) {
107+
throw new WC_REST_Exception( 'woocommerce_product_invalid_image_id', sprintf( __( '#%s is an invalid image ID.', 'woocommerce' ), $attachment_id ), 400 );
108+
}
109+
110+
$featured_image = $product->get_image_id();
111+
112+
if ( isset( $image['position'] ) && 0 === absint( $image['position'] ) ) {
113+
$product->set_image_id( $attachment_id );
114+
} elseif( empty( $image['position'] ) && empty( $featured_image ) ) {
115+
$product->set_image_id( $attachment_id );
116+
} else {
117+
$gallery[] = $attachment_id;
118+
}
119+
120+
// Set the image alt if present.
121+
if ( ! empty( $image['alt'] ) ) {
122+
update_post_meta( $attachment_id, '_wp_attachment_image_alt', wc_clean( $image['alt'] ) );
123+
}
124+
125+
// Set the image name if present.
126+
if ( ! empty( $image['name'] ) ) {
127+
wp_update_post( array( 'ID' => $attachment_id, 'post_title' => $image['name'] ) );
128+
}
129+
}
130+
131+
$product->set_gallery_image_ids( $gallery );
132+
} else {
133+
$product->set_image_id( '' );
134+
$product->set_gallery_image_ids( array() );
135+
}
136+
137+
return $product;
138+
}
139+
31140
}

0 commit comments

Comments
 (0)