[API] Fixed errors and improved create product category endpoint

This commit is contained in:
Claudio Sanches 2015-12-16 16:35:46 -02:00
parent e097ff7b56
commit ee990de153
1 changed files with 5 additions and 11 deletions

View File

@ -664,9 +664,6 @@ class WC_API_Products extends WC_API_Resource {
$data = wp_parse_args( $data['product_category'], $defaults );
$data = apply_filters( 'woocommerce_api_create_product_category_data', $data, $this );
$name = $data['name'];
unset( $data['name'] );
// Check parent.
$data['parent'] = absint( $data['parent'] );
if ( $data['parent'] ) {
@ -676,28 +673,25 @@ class WC_API_Products extends WC_API_Resource {
}
}
$display_type = $data['display'];
unset( $data['display'] );
// If value of image is numeric, assume value as image_id.
$image = $data['image'];
$image = $data['image'];
$image_id = 0;
if ( is_numeric( $image ) ) {
$image_id = absint( $image );
} else if ( ! empty( $image ) ) {
$upload = $this->upload_product_image( esc_url_raw( $image ) );
$image_id = $this->set_product_category_image_as_attachment( $upload );
}
unset( $data['image'] );
$insert = wp_insert_term( $name, 'product_cat', $data );
$insert = wp_insert_term( $data['name'], 'product_cat', $data );
if ( is_wp_error( $insert ) ) {
throw new WC_API_Exception( 'woocommerce_api_cannot_create_product_category', $insert->get_error_message(), 400 );
}
update_woocommerce_term_meta( $insert['term_taxonomy_id'], 'display_type', esc_attr( $display_type ) );
update_woocommerce_term_meta( $insert['term_taxonomy_id'], 'display_type', esc_attr( $data['display'] ) );
// Check if image_id is a valid image attachment before updating the term meta.
if ( wp_attachment_is_image( $image_id ) ) {
if ( $image_id && wp_attachment_is_image( $image_id ) ) {
update_woocommerce_term_meta( $insert['term_taxonomy_id'], 'thumbnail_id', $image_id );
}