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

Commit 0e70f3b

Browse files
author
Justin Shreve
authored
Fix variation image setting via ID. (#21)
1 parent 737a221 commit 0e70f3b

File tree

2 files changed

+80
-2
lines changed

2 files changed

+80
-2
lines changed

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

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,7 @@ protected function prepare_object_for_database( $request, $creating = false ) {
130130
// Thumbnail.
131131
if ( isset( $request['image'] ) ) {
132132
if ( is_array( $request['image'] ) ) {
133-
$image = $request['image'];
134-
$variation = $this->set_product_images( $variation, array( $image ) );
133+
$variation = $this->set_variation_image( $variation, $request['image'] );
135134
} else {
136135
$variation->set_image_id( '' );
137136
}
@@ -339,6 +338,48 @@ protected function get_image( $variation ) {
339338
return;
340339
}
341340

341+
/**
342+
* Set variation image.
343+
*
344+
* @throws WC_REST_Exception REST API exceptions.
345+
* @param WC_Product_Variation $variation Variation instance.
346+
* @param array $image Image data.
347+
* @return WC_Product_Variation
348+
*/
349+
protected function set_variation_image( $variation, $image ) {
350+
$attachment_id = isset( $image['id'] ) ? absint( $image['id'] ) : 0;
351+
352+
if ( 0 === $attachment_id && isset( $image['src'] ) ) {
353+
$upload = wc_rest_upload_image_from_url( esc_url_raw( $image['src'] ) );
354+
355+
if ( is_wp_error( $upload ) ) {
356+
if ( ! apply_filters( 'woocommerce_rest_suppress_image_upload_error', false, $upload, $variation->get_id(), array( $image ) ) ) {
357+
throw new WC_REST_Exception( 'woocommerce_variation_image_upload_error', $upload->get_error_message(), 400 );
358+
}
359+
}
360+
361+
$attachment_id = wc_rest_set_uploaded_image_as_attachment( $upload, $variation->get_id() );
362+
}
363+
364+
if ( ! wp_attachment_is_image( $attachment_id ) ) {
365+
throw new WC_REST_Exception( 'woocommerce_variation_invalid_image_id', sprintf( __( '#%s is an invalid image ID.', 'woocommerce' ), $attachment_id ), 400 );
366+
}
367+
368+
$variation->set_image_id( $attachment_id );
369+
370+
// Set the image alt if present.
371+
if ( ! empty( $image['alt'] ) ) {
372+
update_post_meta( $attachment_id, '_wp_attachment_image_alt', wc_clean( $image['alt'] ) );
373+
}
374+
375+
// Set the image name if present.
376+
if ( ! empty( $image['name'] ) ) {
377+
wp_update_post( array( 'ID' => $attachment_id, 'post_title' => $image['name'] ) );
378+
}
379+
380+
return $variation;
381+
}
382+
342383
/**
343384
* Get the Variation's schema, conforming to JSON Schema.
344385
*

tests/unit-tests/product-variations.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,43 @@ public function test_update_variation() {
195195
$product->delete( true );
196196
}
197197

198+
/**
199+
* Test setting variation image from ID. (src is tested in `test_update_variation`)
200+
*/
201+
public function test_set_variation_image_from_id() {
202+
wp_set_current_user( $this->user );
203+
204+
$orig_file = dirname( __FILE__ ) . '/../../assets/icon-128x128.png';
205+
$this->test_file = '/tmp/icon.png';
206+
copy( $orig_file, $this->test_file );
207+
208+
$request = new WP_REST_Request( 'POST', '/wp/v2/media' );
209+
$request->set_header( 'Content-Type', 'image/png' );
210+
$request->set_header( 'Content-Disposition', 'attachment; filename=icon.png' );
211+
$request->set_body( file_get_contents( $this->test_file ) );
212+
$response = $this->server->dispatch( $request );
213+
$image = $response->get_data();
214+
215+
$product = WC_Helper_Product::create_variation_product();
216+
$children = $product->get_children();
217+
$variation_id = $children[0];
218+
219+
$request = new WP_REST_Request( 'PUT', '/wc/v3/products/' . $product->get_id() . '/variations/' . $variation_id );
220+
$request->set_body_params( array(
221+
'image' => array( 'id' => $image['id'] ),
222+
) );
223+
$response = $this->server->dispatch( $request );
224+
$variation = $response->get_data();
225+
226+
$this->assertNotEmpty( $variation['image'] );
227+
$this->assertEquals( $image['id'], $variation['image']['id'] );
228+
229+
$product->delete( true );
230+
if ( file_exists( $this->test_file ) ) {
231+
unlink( $this->test_file );
232+
}
233+
}
234+
198235
/**
199236
* Test updating a single variation without permission.
200237
*

0 commit comments

Comments
 (0)