Created new method to save product taxonomy terms

This commit is contained in:
Claudio Sanches 2016-03-29 22:06:05 -03:00
parent dc6b4e95c8
commit 124a4291b0
1 changed files with 22 additions and 7 deletions

View File

@ -746,6 +746,23 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
update_post_meta( $id, '_downloadable_files', $files ); update_post_meta( $id, '_downloadable_files', $files );
} }
/**
* Save taxonomy terms.
*
* @param WC_Product $product
* @param array $terms
* @param string $taxonomy
* @return array
*/
protected function save_taxonomy_terms( $product, $terms, $taxonomy = 'cat' ) {
$term_ids = wp_list_pluck( $terms, 'id' );
$term_ids = array_unique( array_map( 'intval', $term_ids ) );
wp_set_object_terms( $product->id, $term_ids, 'product_' . $taxonomy );
return $terms;
}
/** /**
* Save product meta. * Save product meta.
* *
@ -1117,14 +1134,12 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
// Product categories. // Product categories.
if ( isset( $request['categories'] ) && is_array( $request['categories'] ) ) { if ( isset( $request['categories'] ) && is_array( $request['categories'] ) ) {
$term_ids = array_unique( array_map( 'intval', $request['categories'] ) ); $this->save_taxonomy_terms( $product, $request['categories'] );
wp_set_object_terms( $product->id, $term_ids, 'product_cat' );
} }
// Product tags. // Product tags.
if ( isset( $request['tags'] ) && is_array( $request['tags'] ) ) { if ( isset( $request['tags'] ) && is_array( $request['tags'] ) ) {
$term_ids = array_unique( array_map( 'intval', $request['tags'] ) ); $this->save_taxonomy_terms( $product, $request['tags'], 'tag' );
wp_set_object_terms( $product->id, $term_ids, 'product_tag' );
} }
// Downloadable. // Downloadable.
@ -1181,7 +1196,7 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
* @return bool * @return bool
* @throws WC_REST_Exception * @throws WC_REST_Exception
*/ */
protected function save_variations( $product, $request ) { protected function save_variations_data( $product, $request ) {
global $wpdb; global $wpdb;
$variations = $request['variations']; $variations = $request['variations'];
@ -1526,7 +1541,7 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
// Save variations. // Save variations.
if ( isset( $request['type'] ) && 'variable' == $request['type'] && isset( $request['variations'] ) && is_array( $request['variations'] ) ) { if ( isset( $request['type'] ) && 'variable' == $request['type'] && isset( $request['variations'] ) && is_array( $request['variations'] ) ) {
$this->save_variations( $product, $request ); $this->save_variations_data( $product, $request );
} }
return true; return true;
@ -1557,7 +1572,7 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
// 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( $product, $request ); $this->save_variations_data( $product, $request );
} else { } else {
// Just sync variations. // Just sync variations.
WC_Product_Variable::sync( $product->id ); WC_Product_Variable::sync( $product->id );