Merge pull request #12738 from woocommerce/api-12727

Return current variations after API update/create
This commit is contained in:
Claudio Sanches 2017-01-03 15:00:38 -02:00 committed by GitHub
commit 8b85a0080d
1 changed files with 14 additions and 26 deletions

View File

@ -823,7 +823,7 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
* @param array $images Images data. * @param array $images Images data.
* @return WC_Product * @return WC_Product
*/ */
protected function save_product_images( $product, $images ) { protected function set_product_images( $product, $images ) {
if ( is_array( $images ) ) { if ( is_array( $images ) ) {
$gallery = array(); $gallery = array();
@ -1042,7 +1042,7 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
* @param WP_REST_Request $request Request data. * @param WP_REST_Request $request Request data.
* @return WC_Product * @return WC_Product
*/ */
protected function save_product_meta( $product, $request ) { protected function set_product_meta( $product, $request ) {
global $wpdb; global $wpdb;
// Virtual. // Virtual.
@ -1321,6 +1321,8 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
$product = $this->save_default_attributes( $product, $request ); $product = $this->save_default_attributes( $product, $request );
} }
$product->save();
return $product; return $product;
} }
@ -1377,7 +1379,7 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
$image['position'] = 0; $image['position'] = 0;
} }
$variation = $this->save_product_images( $variation, array( $image ) ); $variation = $this->set_product_images( $variation, array( $image ) );
} }
// Virtual variation. // Virtual variation.
@ -1526,23 +1528,7 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
* @return bool|WP_Error * @return bool|WP_Error
*/ */
protected function add_post_meta_fields( $post, $request ) { protected function add_post_meta_fields( $post, $request ) {
$product = wc_get_product( $post ); return $this->update_post_meta_fields( $post, $request );
// Check for featured/gallery images, upload it and set it.
if ( isset( $request['images'] ) ) {
$product = $this->save_product_images( $product, $request['images'] );
}
// Save product meta fields.
$product = $this->save_product_meta( $product, $request );
$product->save();
// Save variations.
if ( isset( $request['type'] ) && 'variable' === $request['type'] && isset( $request['variations'] ) && is_array( $request['variations'] ) ) {
$this->save_variations_data( $product, $request );
}
return true;
} }
/** /**
@ -1557,23 +1543,25 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
// Check for featured/gallery images, upload it and set it. // Check for featured/gallery images, upload it and set it.
if ( isset( $request['images'] ) ) { if ( isset( $request['images'] ) ) {
$product = $this->save_product_images( $product, $request['images'] ); $product = $this->set_product_images( $product, $request['images'] );
} }
// Save product meta fields. // Save product meta fields.
$product = $this->save_product_meta( $product, $request ); $product = $this->set_product_meta( $product, $request );
// Save the product data.
$product->save();
// Save variations. // Save variations.
if ( $product->is_type( 'variable' ) ) { if ( $product->is_type( 'variable' ) ) {
if ( isset( $request['variations'] ) && is_array( $request['variations'] ) ) { if ( isset( $request['variations'] ) && is_array( $request['variations'] ) ) {
$this->save_variations_data( $product, $request ); $this->save_variations_data( $product, $request );
} else {
// Just sync variations.
$product = WC_Product_Variable::sync( $product, false );
} }
} }
$product->save(); // Clear caches here so in sync with any new variations/children.
wc_delete_product_transients( $product->get_id() );
wp_cache_delete( 'product-' . $product->get_id(), 'products' );
return true; return true;
} }