Allow save meta data for products in v2

This commit is contained in:
Claudio Sanches 2017-02-16 01:39:30 -02:00
parent d2818be2ba
commit 036a2f6c92
2 changed files with 24 additions and 3 deletions

View File

@ -227,6 +227,28 @@ class WC_REST_Products_Controller extends WC_REST_Products_V1_Controller {
return $variations;
}
/**
* Set product meta.
*
* @throws WC_REST_Exception REST API exceptions.
* @param WC_Product $product Product instance.
* @param WP_REST_Request $request Request data.
* @return WC_Product
*/
protected function set_product_meta( $product, $request ) {
$product = parent::set_product_meta( $product, $request );
// Allow set meta_data.
if ( is_array( $request['meta_data'] ) ) {
foreach ( $request['meta_data'] as $meta ) {
$meta_id = isset( $meta['id'] ) ? $meta['id'] : '';
$product->update_meta_data( $meta['key'], $meta['value'], $meta_id );
}
}
return $product;
}
/**
* Get the Product's schema, conforming to JSON Schema.
*

View File

@ -1063,7 +1063,8 @@ class WC_REST_Products_V1_Controller extends WC_REST_Posts_Controller {
* @throws WC_REST_Exception
*/
protected function save_product_meta( $product, $request ) {
$this->set_product_meta( $product, $request );
$product = $this->set_product_meta( $product, $request );
$product->save();
return true;
}
@ -1353,8 +1354,6 @@ class WC_REST_Products_V1_Controller extends WC_REST_Posts_Controller {
$product = $this->save_default_attributes( $product, $request );
}
$product->save();
return $product;
}