[API] Refactored the products endpoint to use the WC_API_Exception class #6138
This commit is contained in:
parent
d71e135c27
commit
4f3326fe86
|
@ -155,18 +155,21 @@ class WC_API_Products extends WC_API_Resource {
|
|||
* @return array
|
||||
*/
|
||||
public function get_products_count( $type = null, $filter = array() ) {
|
||||
try {
|
||||
if ( ! current_user_can( 'read_private_products' ) ) {
|
||||
throw new WC_API_Exception( 'woocommerce_api_user_cannot_read_products_count', __( 'You do not have permission to read the products count', 'woocommerce' ), 401 );
|
||||
}
|
||||
|
||||
if ( ! empty( $type ) ) {
|
||||
$filter['type'] = $type;
|
||||
if ( ! empty( $type ) ) {
|
||||
$filter['type'] = $type;
|
||||
}
|
||||
|
||||
$query = $this->query_products( $filter );
|
||||
|
||||
return array( 'count' => (int) $query->found_posts );
|
||||
} catch ( WC_API_Exception $e ) {
|
||||
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
|
||||
}
|
||||
|
||||
if ( ! current_user_can( 'read_private_products' ) ) {
|
||||
return new WP_Error( 'woocommerce_api_user_cannot_read_products_count', __( 'You do not have permission to read the products count', 'woocommerce' ), array( 'status' => 401 ) );
|
||||
}
|
||||
|
||||
$query = $this->query_products( $filter );
|
||||
|
||||
return array( 'count' => (int) $query->found_posts );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -177,85 +180,77 @@ class WC_API_Products extends WC_API_Resource {
|
|||
* @return array
|
||||
*/
|
||||
public function create_product( $data ) {
|
||||
$id = 0;
|
||||
|
||||
$data = isset( $data['product'] ) ? $data['product'] : array();
|
||||
try {
|
||||
$data = isset( $data['product'] ) ? $data['product'] : array();
|
||||
|
||||
// Check permissions
|
||||
if ( ! current_user_can( 'publish_products' ) ) {
|
||||
return new WP_Error( 'woocommerce_api_user_cannot_create_product', __( 'You do not have permission to create products', 'woocommerce' ), array( 'status' => 401 ) );
|
||||
}
|
||||
|
||||
$data = apply_filters( 'woocommerce_api_create_product_data', $data, $this );
|
||||
|
||||
// Check if product title is specified
|
||||
if ( ! isset( $data['title'] ) ) {
|
||||
return new WP_Error( 'woocommerce_api_missing_product_title', sprintf( __( 'Missing parameter %s', 'woocommerce' ), 'title' ), array( 'status' => 400 ) );
|
||||
}
|
||||
|
||||
// Check product type
|
||||
if ( ! isset( $data['type'] ) ) {
|
||||
$data['type'] = 'simple';
|
||||
}
|
||||
|
||||
// Validate the product type
|
||||
if ( ! in_array( wc_clean( $data['type'] ), array_keys( wc_get_product_types() ) ) ) {
|
||||
return new WP_Error( 'woocommerce_api_invalid_product_type', sprintf( __( 'Invalid product type - the product type must be any of these: %s', 'woocommerce' ), implode( ', ', array_keys( wc_get_product_types() ) ) ), array( 'status' => 400 ) );
|
||||
}
|
||||
|
||||
$new_product = array(
|
||||
'post_title' => wc_clean( $data['title'] ),
|
||||
'post_status' => ( isset( $data['status'] ) ? wc_clean( $data['status'] ) : 'publish' ),
|
||||
'post_type' => 'product',
|
||||
'post_excerpt' => ( isset( $data['short_description'] ) ? wc_clean( $data['short_description'] ) : '' ),
|
||||
'post_content' => ( isset( $data['description'] ) ? wc_clean( $data['description'] ) : '' ),
|
||||
'post_author' => get_current_user_id(),
|
||||
);
|
||||
|
||||
// Attempts to create the new product
|
||||
$id = wp_insert_post( $new_product, true );
|
||||
|
||||
// Checks for an error in the product creation
|
||||
if ( is_wp_error( $id ) ) {
|
||||
return new WP_Error( 'woocommerce_api_cannot_create_product', $id->get_error_message(), array( 'status' => 400 ) );
|
||||
}
|
||||
|
||||
// Check for featured/gallery images, upload it and set it
|
||||
if ( isset( $data['images'] ) ) {
|
||||
$images = $this->save_product_images( $id, $data['images'] );
|
||||
|
||||
if ( is_wp_error( $images ) ) {
|
||||
wp_delete_post( $id, true );
|
||||
|
||||
return $images;
|
||||
// Check permissions
|
||||
if ( ! current_user_can( 'publish_products' ) ) {
|
||||
throw new WC_API_Exception( 'woocommerce_api_user_cannot_create_product', __( 'You do not have permission to create products', 'woocommerce' ), 401 );
|
||||
}
|
||||
}
|
||||
|
||||
// Save product meta fields
|
||||
$meta = $this->save_product_meta( $id, $data );
|
||||
if ( is_wp_error( $meta ) ) {
|
||||
// Remove the product
|
||||
$data = apply_filters( 'woocommerce_api_create_product_data', $data, $this );
|
||||
|
||||
// Check if product title is specified
|
||||
if ( ! isset( $data['title'] ) ) {
|
||||
throw new WC_API_Exception( 'woocommerce_api_missing_product_title', sprintf( __( 'Missing parameter %s', 'woocommerce' ), 'title' ), 400 );
|
||||
}
|
||||
|
||||
// Check product type
|
||||
if ( ! isset( $data['type'] ) ) {
|
||||
$data['type'] = 'simple';
|
||||
}
|
||||
|
||||
// Validate the product type
|
||||
if ( ! in_array( wc_clean( $data['type'] ), array_keys( wc_get_product_types() ) ) ) {
|
||||
throw new WC_API_Exception( 'woocommerce_api_invalid_product_type', sprintf( __( 'Invalid product type - the product type must be any of these: %s', 'woocommerce' ), implode( ', ', array_keys( wc_get_product_types() ) ) ), 400 );
|
||||
}
|
||||
|
||||
$new_product = array(
|
||||
'post_title' => wc_clean( $data['title'] ),
|
||||
'post_status' => ( isset( $data['status'] ) ? wc_clean( $data['status'] ) : 'publish' ),
|
||||
'post_type' => 'product',
|
||||
'post_excerpt' => ( isset( $data['short_description'] ) ? wc_clean( $data['short_description'] ) : '' ),
|
||||
'post_content' => ( isset( $data['description'] ) ? wc_clean( $data['description'] ) : '' ),
|
||||
'post_author' => get_current_user_id(),
|
||||
);
|
||||
|
||||
// Attempts to create the new product
|
||||
$id = wp_insert_post( $new_product, true );
|
||||
|
||||
// Checks for an error in the product creation
|
||||
if ( is_wp_error( $id ) ) {
|
||||
throw new WC_API_Exception( 'woocommerce_api_cannot_create_product', $id->get_error_message(), 400 );
|
||||
}
|
||||
|
||||
// Check for featured/gallery images, upload it and set it
|
||||
if ( isset( $data['images'] ) ) {
|
||||
$this->save_product_images( $id, $data['images'] );
|
||||
}
|
||||
|
||||
// Save product meta fields
|
||||
$this->save_product_meta( $id, $data );
|
||||
|
||||
// Save variations
|
||||
if ( isset( $data['type'] ) && 'variable' == $data['type'] && isset( $data['variations'] ) && is_array( $data['variations'] ) ) {
|
||||
$this->save_variations( $id, $data );
|
||||
}
|
||||
|
||||
do_action( 'woocommerce_api_create_product', $id, $data );
|
||||
|
||||
// Clear cache/transients
|
||||
wc_delete_product_transients( $id );
|
||||
|
||||
$this->server->send_status( 201 );
|
||||
|
||||
return $this->get_product( $id );
|
||||
} catch ( WC_API_Exception $e ) {
|
||||
// Remove the product when fails
|
||||
wp_delete_post( $id, true );
|
||||
|
||||
return $meta;
|
||||
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
|
||||
}
|
||||
|
||||
// Save variations
|
||||
if ( isset( $data['type'] ) && 'variable' == $data['type'] && isset( $data['variations'] ) && is_array( $data['variations'] ) ) {
|
||||
$variations = $this->save_variations( $id, $data );
|
||||
|
||||
if ( is_wp_error( $variations ) ) {
|
||||
return $variations;
|
||||
}
|
||||
}
|
||||
|
||||
do_action( 'woocommerce_api_create_product', $id, $data );
|
||||
|
||||
// Clear cache/transients
|
||||
wc_delete_product_transients( $id );
|
||||
|
||||
$this->server->send_status( 201 );
|
||||
|
||||
return $this->get_product( $id );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -267,77 +262,69 @@ class WC_API_Products extends WC_API_Resource {
|
|||
* @return array
|
||||
*/
|
||||
public function edit_product( $id, $data ) {
|
||||
try {
|
||||
$data = isset( $data['product'] ) ? $data['product'] : array();
|
||||
|
||||
$data = isset( $data['product'] ) ? $data['product'] : array();
|
||||
$id = $this->validate_request( $id, 'product', 'edit' );
|
||||
|
||||
$id = $this->validate_request( $id, 'product', 'edit' );
|
||||
|
||||
if ( is_wp_error( $id ) ) {
|
||||
return $id;
|
||||
}
|
||||
|
||||
$data = apply_filters( 'woocommerce_api_edit_product_data', $data, $this );
|
||||
|
||||
// Product title.
|
||||
if ( isset( $data['title'] ) ) {
|
||||
wp_update_post( array( 'ID' => $id, 'post_title' => wc_clean( $data['title'] ) ) );
|
||||
}
|
||||
|
||||
// Product name (slug).
|
||||
if ( isset( $data['name'] ) ) {
|
||||
wp_update_post( array( 'ID' => $id, 'post_name' => sanitize_title( $data['name'] ) ) );
|
||||
}
|
||||
|
||||
// Product status.
|
||||
if ( isset( $data['status'] ) ) {
|
||||
wp_update_post( array( 'ID' => $id, 'post_status' => wc_clean( $data['status'] ) ) );
|
||||
}
|
||||
|
||||
// Product short description.
|
||||
if ( isset( $data['short_description'] ) ) {
|
||||
wp_update_post( array( 'ID' => $id, 'post_excerpt' => wc_clean( $data['short_description'] ) ) );
|
||||
}
|
||||
|
||||
// Product description.
|
||||
if ( isset( $data['description'] ) ) {
|
||||
wp_update_post( array( 'ID' => $id, 'post_content' => wc_clean( $data['description'] ) ) );
|
||||
}
|
||||
|
||||
// Validate the product type
|
||||
if ( isset( $data['type'] ) && ! in_array( wc_clean( $data['type'] ), array_keys( wc_get_product_types() ) ) ) {
|
||||
return new WP_Error( 'woocommerce_api_invalid_product_type', sprintf( __( 'Invalid product type - the product type must be any of these: %s', 'woocommerce' ), implode( ', ', array_keys( wc_get_product_types() ) ) ), array( 'status' => 400 ) );
|
||||
}
|
||||
|
||||
// Check for featured/gallery images, upload it and set it
|
||||
if ( isset( $data['images'] ) ) {
|
||||
$images = $this->save_product_images( $id, $data['images'] );
|
||||
|
||||
if ( is_wp_error( $images ) ) {
|
||||
return $images;
|
||||
if ( is_wp_error( $id ) ) {
|
||||
return $id;
|
||||
}
|
||||
}
|
||||
|
||||
// Save product meta fields
|
||||
$meta = $this->save_product_meta( $id, $data );
|
||||
if ( is_wp_error( $meta ) ) {
|
||||
return $meta;
|
||||
}
|
||||
$data = apply_filters( 'woocommerce_api_edit_product_data', $data, $this );
|
||||
|
||||
// Save variations
|
||||
if ( isset( $data['type'] ) && 'variable' == $data['type'] && isset( $data['variations'] ) && is_array( $data['variations'] ) ) {
|
||||
$variations = $this->save_variations( $id, $data );
|
||||
|
||||
if ( is_wp_error( $variations ) ) {
|
||||
return $variations;
|
||||
// Product title.
|
||||
if ( isset( $data['title'] ) ) {
|
||||
wp_update_post( array( 'ID' => $id, 'post_title' => wc_clean( $data['title'] ) ) );
|
||||
}
|
||||
|
||||
// Product name (slug).
|
||||
if ( isset( $data['name'] ) ) {
|
||||
wp_update_post( array( 'ID' => $id, 'post_name' => sanitize_title( $data['name'] ) ) );
|
||||
}
|
||||
|
||||
// Product status.
|
||||
if ( isset( $data['status'] ) ) {
|
||||
wp_update_post( array( 'ID' => $id, 'post_status' => wc_clean( $data['status'] ) ) );
|
||||
}
|
||||
|
||||
// Product short description.
|
||||
if ( isset( $data['short_description'] ) ) {
|
||||
wp_update_post( array( 'ID' => $id, 'post_excerpt' => wc_clean( $data['short_description'] ) ) );
|
||||
}
|
||||
|
||||
// Product description.
|
||||
if ( isset( $data['description'] ) ) {
|
||||
wp_update_post( array( 'ID' => $id, 'post_content' => wc_clean( $data['description'] ) ) );
|
||||
}
|
||||
|
||||
// Validate the product type
|
||||
if ( isset( $data['type'] ) && ! in_array( wc_clean( $data['type'] ), array_keys( wc_get_product_types() ) ) ) {
|
||||
throw new WC_API_Exception( 'woocommerce_api_invalid_product_type', sprintf( __( 'Invalid product type - the product type must be any of these: %s', 'woocommerce' ), implode( ', ', array_keys( wc_get_product_types() ) ) ), 400 );
|
||||
}
|
||||
|
||||
// Check for featured/gallery images, upload it and set it
|
||||
if ( isset( $data['images'] ) ) {
|
||||
$this->save_product_images( $id, $data['images'] );
|
||||
}
|
||||
|
||||
// Save product meta fields
|
||||
$this->save_product_meta( $id, $data );
|
||||
|
||||
// Save variations
|
||||
if ( isset( $data['type'] ) && 'variable' == $data['type'] && isset( $data['variations'] ) && is_array( $data['variations'] ) ) {
|
||||
$this->save_variations( $id, $data );
|
||||
}
|
||||
|
||||
do_action( 'woocommerce_api_edit_product', $id, $data );
|
||||
|
||||
// Clear cache/transients
|
||||
wc_delete_product_transients( $id );
|
||||
|
||||
return $this->get_product( $id );
|
||||
} catch ( WC_API_Exception $e ) {
|
||||
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
|
||||
}
|
||||
|
||||
do_action( 'woocommerce_api_edit_product', $id, $data );
|
||||
|
||||
// Clear cache/transients
|
||||
wc_delete_product_transients( $id );
|
||||
|
||||
return $this->get_product( $id );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -410,22 +397,24 @@ class WC_API_Products extends WC_API_Resource {
|
|||
* @return array
|
||||
*/
|
||||
public function get_product_categories( $fields = null ) {
|
||||
try {
|
||||
// Permissions check
|
||||
if ( ! current_user_can( 'manage_product_terms' ) ) {
|
||||
throw new WC_API_Exception( 'woocommerce_api_user_cannot_read_product_categories', __( 'You do not have permission to read product categories', 'woocommerce' ), 401 );
|
||||
}
|
||||
|
||||
// permissions check
|
||||
if ( ! current_user_can( 'manage_product_terms' ) ) {
|
||||
return new WP_Error( "woocommerce_api_user_cannot_read_product_categories", __( 'You do not have permission to read product categories', 'woocommerce' ), array( 'status' => 401 ) );
|
||||
$product_categories = array();
|
||||
|
||||
$terms = get_terms( 'product_cat', array( 'hide_empty' => false, 'fields' => 'ids' ) );
|
||||
|
||||
foreach ( $terms as $term_id ) {
|
||||
$product_categories[] = current( $this->get_product_category( $term_id, $fields ) );
|
||||
}
|
||||
|
||||
return array( 'product_categories' => apply_filters( 'woocommerce_api_product_categories_response', $product_categories, $terms, $fields, $this ) );
|
||||
} catch ( WC_API_Exception $e ) {
|
||||
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
|
||||
}
|
||||
|
||||
$product_categories = array();
|
||||
|
||||
$terms = get_terms( 'product_cat', array( 'hide_empty' => false, 'fields' => 'ids' ) );
|
||||
|
||||
foreach ( $terms as $term_id ) {
|
||||
|
||||
$product_categories[] = current( $this->get_product_category( $term_id, $fields ) );
|
||||
}
|
||||
|
||||
return array( 'product_categories' => apply_filters( 'woocommerce_api_product_categories_response', $product_categories, $terms, $fields, $this ) );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -437,35 +426,38 @@ class WC_API_Products extends WC_API_Resource {
|
|||
* @return array
|
||||
*/
|
||||
public function get_product_category( $id, $fields = null ) {
|
||||
try {
|
||||
$id = absint( $id );
|
||||
|
||||
$id = absint( $id );
|
||||
// Validate ID
|
||||
if ( empty( $id ) ) {
|
||||
throw new WC_API_Exception( 'woocommerce_api_invalid_product_category_id', __( 'Invalid product category ID', 'woocommerce' ), 400 );
|
||||
}
|
||||
|
||||
// validate ID
|
||||
if ( empty( $id ) ) {
|
||||
return new WP_Error( 'woocommerce_api_invalid_product_category_id', __( 'Invalid product category ID', 'woocommerce' ), array( 'status' => 400 ) );
|
||||
// Permissions check
|
||||
if ( ! current_user_can( 'manage_product_terms' ) ) {
|
||||
throw new WC_API_Exception( 'woocommerce_api_user_cannot_read_product_categories', __( 'You do not have permission to read product categories', 'woocommerce' ), 401 );
|
||||
}
|
||||
|
||||
$term = get_term( $id, 'product_cat' );
|
||||
|
||||
if ( is_wp_error( $term ) || is_null( $term ) ) {
|
||||
throw new WC_API_Exception( 'woocommerce_api_invalid_product_category_id', __( 'A product category with the provided ID could not be found', 'woocommerce' ), 404 );
|
||||
}
|
||||
|
||||
$product_category = array(
|
||||
'id' => intval( $term->term_id ),
|
||||
'name' => $term->name,
|
||||
'slug' => $term->slug,
|
||||
'parent' => $term->parent,
|
||||
'description' => $term->description,
|
||||
'count' => intval( $term->count ),
|
||||
);
|
||||
|
||||
return array( 'product_category' => apply_filters( 'woocommerce_api_product_category_response', $product_category, $id, $fields, $term, $this ) );
|
||||
} catch ( WC_API_Exception $e ) {
|
||||
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
|
||||
}
|
||||
|
||||
// permissions check
|
||||
if ( ! current_user_can( 'manage_product_terms' ) ) {
|
||||
return new WP_Error( 'woocommerce_api_user_cannot_read_product_categories', __( 'You do not have permission to read product categories', 'woocommerce' ), array( 'status' => 401 ) );
|
||||
}
|
||||
|
||||
$term = get_term( $id, 'product_cat' );
|
||||
|
||||
if ( is_wp_error( $term ) || is_null( $term ) ) {
|
||||
return new WP_Error( 'woocommerce_api_invalid_product_category_id', __( 'A product category with the provided ID could not be found', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
$product_category = array(
|
||||
'id' => intval( $term->term_id ),
|
||||
'name' => $term->name,
|
||||
'slug' => $term->slug,
|
||||
'parent' => $term->parent,
|
||||
'description' => $term->description,
|
||||
'count' => intval( $term->count ),
|
||||
);
|
||||
|
||||
return array( 'product_category' => apply_filters( 'woocommerce_api_product_category_response', $product_category, $id, $fields, $term, $this ) );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -477,7 +469,7 @@ class WC_API_Products extends WC_API_Resource {
|
|||
*/
|
||||
private function query_products( $args ) {
|
||||
|
||||
// set base query arguments
|
||||
// Set base query arguments
|
||||
$query_args = array(
|
||||
'fields' => 'ids',
|
||||
'post_type' => 'product',
|
||||
|
@ -500,7 +492,7 @@ class WC_API_Products extends WC_API_Resource {
|
|||
unset( $args['type'] );
|
||||
}
|
||||
|
||||
// filter products by category
|
||||
// Filter products by category
|
||||
if ( ! empty( $args['category'] ) ) {
|
||||
$query_args['product_cat'] = $args['category'];
|
||||
}
|
||||
|
@ -710,7 +702,7 @@ class WC_API_Products extends WC_API_Resource {
|
|||
if ( ! empty( $new_sku ) ) {
|
||||
$unique_sku = wc_product_has_unique_sku( $id, $new_sku );
|
||||
if ( ! $unique_sku ) {
|
||||
return new WP_Error( 'woocommerce_api_product_sku_already_exists', __( 'The SKU already exists on another product', 'woocommerce' ), array( 'status' => 400 ) );
|
||||
throw new WC_API_Exception( 'woocommerce_api_product_sku_already_exists', __( 'The SKU already exists on another product', 'woocommerce' ), 400 );
|
||||
} else {
|
||||
update_post_meta( $id, '_sku', $new_sku );
|
||||
}
|
||||
|
@ -1142,7 +1134,7 @@ class WC_API_Products extends WC_API_Resource {
|
|||
|
||||
// Stop with we don't have a variation ID
|
||||
if ( is_wp_error( $variation_id ) ) {
|
||||
return $variation_id;
|
||||
throw new WC_API_Exception( 'woocommerce_api_cannot_save_product_variation', $variation_id->get_error_message(), 400 );
|
||||
}
|
||||
|
||||
// SKU
|
||||
|
@ -1156,7 +1148,7 @@ class WC_API_Products extends WC_API_Resource {
|
|||
if ( ! empty( $new_sku ) ) {
|
||||
$unique_sku = wc_product_has_unique_sku( $variation_id, $new_sku );
|
||||
if ( ! $unique_sku ) {
|
||||
return new WP_Error( 'woocommerce_api_product_sku_already_exists', __( 'The SKU already exists on another product', 'woocommerce' ), array( 'status' => 400 ) );
|
||||
throw new WC_API_Exception( 'woocommerce_api_product_sku_already_exists', __( 'The SKU already exists on another product', 'woocommerce' ), 400 );
|
||||
} else {
|
||||
update_post_meta( $variation_id, '_sku', $new_sku );
|
||||
}
|
||||
|
@ -1173,7 +1165,7 @@ class WC_API_Products extends WC_API_Resource {
|
|||
if ( isset( $image['position'] ) && isset( $image['src'] ) && $image['position'] == 0 ) {
|
||||
$upload = $this->upload_product_image( wc_clean( $image['src'] ) );
|
||||
if ( is_wp_error( $upload ) ) {
|
||||
return new WP_Error( 'woocommerce_api_cannot_upload_product_image', $upload->get_error_message(), array( 'status' => 400 ) );
|
||||
throw new WC_API_Exception( 'woocommerce_api_cannot_upload_product_image', $upload->get_error_message(), 400 );
|
||||
}
|
||||
$attachment_id = $this->set_product_image_as_attachment( $upload, $id );
|
||||
update_post_meta( $variation_id, '_thumbnail_id', $attachment_id );
|
||||
|
@ -1453,7 +1445,7 @@ class WC_API_Products extends WC_API_Resource {
|
|||
private function save_downloadable_files( $product_id, $downloads, $variation_id = 0 ) {
|
||||
$files = array();
|
||||
|
||||
// file paths will be stored in an array keyed off md5(file path)
|
||||
// File paths will be stored in an array keyed off md5(file path)
|
||||
foreach ( $downloads as $key => $file ) {
|
||||
if ( ! isset( $file['url'] ) ) {
|
||||
continue;
|
||||
|
@ -1468,7 +1460,7 @@ class WC_API_Products extends WC_API_Resource {
|
|||
);
|
||||
}
|
||||
|
||||
// grant permission to any newly added files on any existing orders for this product prior to saving
|
||||
// Grant permission to any newly added files on any existing orders for this product prior to saving
|
||||
do_action( 'woocommerce_process_product_file_download_paths', $product_id, $variation_id, $files );
|
||||
|
||||
update_post_meta( $product_id, '_downloadable_files', $files );
|
||||
|
@ -1511,27 +1503,27 @@ class WC_API_Products extends WC_API_Resource {
|
|||
|
||||
if ( has_post_thumbnail( $product->get_variation_id() ) ) {
|
||||
|
||||
// add variation image if set
|
||||
// Add variation image if set
|
||||
$attachment_ids[] = get_post_thumbnail_id( $product->get_variation_id() );
|
||||
|
||||
} elseif ( has_post_thumbnail( $product->id ) ) {
|
||||
|
||||
// otherwise use the parent product featured image if set
|
||||
// Otherwise use the parent product featured image if set
|
||||
$attachment_ids[] = get_post_thumbnail_id( $product->id );
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
// add featured image
|
||||
// Add featured image
|
||||
if ( has_post_thumbnail( $product->id ) ) {
|
||||
$attachment_ids[] = get_post_thumbnail_id( $product->id );
|
||||
}
|
||||
|
||||
// add gallery images
|
||||
// Add gallery images
|
||||
$attachment_ids = array_merge( $attachment_ids, $product->get_gallery_attachment_ids() );
|
||||
}
|
||||
|
||||
// build image data
|
||||
// Build image data
|
||||
foreach ( $attachment_ids as $position => $attachment_id ) {
|
||||
|
||||
$attachment_post = get_post( $attachment_id );
|
||||
|
@ -1557,12 +1549,12 @@ class WC_API_Products extends WC_API_Resource {
|
|||
);
|
||||
}
|
||||
|
||||
// set a placeholder image if the product has no images set
|
||||
// Set a placeholder image if the product has no images set
|
||||
if ( empty( $images ) ) {
|
||||
|
||||
$images[] = array(
|
||||
'id' => 0,
|
||||
'created_at' => $this->server->format_datetime( time() ), // default to now
|
||||
'created_at' => $this->server->format_datetime( time() ), // Default to now
|
||||
'updated_at' => $this->server->format_datetime( time() ),
|
||||
'src' => wc_placeholder_img_src(),
|
||||
'title' => __( 'Placeholder', 'woocommerce' ),
|
||||
|
@ -1580,7 +1572,6 @@ class WC_API_Products extends WC_API_Resource {
|
|||
* @since 2.2
|
||||
* @param array $images
|
||||
* @param int $id
|
||||
* @return void|WP_Error
|
||||
*/
|
||||
protected function save_product_images( $id, $images ) {
|
||||
if ( is_array( $images ) ) {
|
||||
|
@ -1594,7 +1585,7 @@ class WC_API_Products extends WC_API_Resource {
|
|||
$upload = $this->upload_product_image( wc_clean( $image['src'] ) );
|
||||
|
||||
if ( is_wp_error( $upload ) ) {
|
||||
return new WP_Error( 'woocommerce_api_cannot_upload_product_image', $upload->get_error_message(), array( 'status' => 400 ) );
|
||||
throw new WC_API_Exception( 'woocommerce_api_cannot_upload_product_image', $upload->get_error_message(), 400 );
|
||||
}
|
||||
|
||||
$attachment_id = $this->set_product_image_as_attachment( $upload, $id );
|
||||
|
@ -1608,7 +1599,7 @@ class WC_API_Products extends WC_API_Resource {
|
|||
$upload = $this->upload_product_image( wc_clean( $image['src'] ) );
|
||||
|
||||
if ( is_wp_error( $upload ) ) {
|
||||
return new WP_Error( 'woocommerce_api_cannot_upload_product_image', $upload->get_error_message(), array( 'status' => 400 ) );
|
||||
throw new WC_API_Exception( 'woocommerce_api_cannot_upload_product_image', $upload->get_error_message(), 400 );
|
||||
}
|
||||
|
||||
$gallery[] = $this->set_product_image_as_attachment( $upload, $id );
|
||||
|
@ -1632,7 +1623,7 @@ class WC_API_Products extends WC_API_Resource {
|
|||
*
|
||||
* @since 2.2
|
||||
* @param string $image_url
|
||||
* @return integer attachment id
|
||||
* @return int attachment id
|
||||
*/
|
||||
public function upload_product_image( $image_url ) {
|
||||
$file_name = basename( current( explode( '?', $image_url ) ) );
|
||||
|
@ -1641,7 +1632,7 @@ class WC_API_Products extends WC_API_Resource {
|
|||
|
||||
// Check parsed URL
|
||||
if ( ! $parsed_url || ! is_array( $parsed_url ) ) {
|
||||
return new WP_Error( 'woocommerce_api_invalid_product_image', sprintf( __( 'Invalid URL %s', 'woocommerce' ), $image_url ), array( 'status' => 400 ) );
|
||||
throw new WC_API_Exception( 'woocommerce_api_invalid_product_image', sprintf( __( 'Invalid URL %s', 'woocommerce' ), $image_url ), 400 );
|
||||
}
|
||||
|
||||
// Ensure url is valid
|
||||
|
@ -1653,7 +1644,7 @@ class WC_API_Products extends WC_API_Resource {
|
|||
) );
|
||||
|
||||
if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
|
||||
return new WP_Error( 'woocommerce_api_invalid_remote_product_image', sprintf( __( 'Error getting remote image %s', 'woocommerce' ), $image_url ), array( 'status' => 400 ) );
|
||||
throw new WC_API_Exception( 'woocommerce_api_invalid_remote_product_image', sprintf( __( 'Error getting remote image %s', 'woocommerce' ), $image_url ), 400 );
|
||||
}
|
||||
|
||||
// Ensure we have a file name and type
|
||||
|
@ -1673,7 +1664,7 @@ class WC_API_Products extends WC_API_Resource {
|
|||
$upload = wp_upload_bits( $file_name, '', wp_remote_retrieve_body( $response ) );
|
||||
|
||||
if ( $upload['error'] ) {
|
||||
return new WP_Error( 'woocommerce_api_product_image_upload_error', $upload['error'], array( 'status' => 400 ) );
|
||||
throw new WC_API_Exception( 'woocommerce_api_product_image_upload_error', $upload['error'], 400 );
|
||||
}
|
||||
|
||||
// Get filesize
|
||||
|
@ -1682,7 +1673,7 @@ class WC_API_Products extends WC_API_Resource {
|
|||
if ( 0 == $filesize ) {
|
||||
@unlink( $upload['file'] );
|
||||
unset( $upload );
|
||||
return new WP_Error( 'woocommerce_api_product_image_upload_file_error', __( 'Zero size file downloaded', 'woocommerce' ), array( 'status' => 400 ) );
|
||||
throw new WC_API_Exception( 'woocommerce_api_product_image_upload_file_error', __( 'Zero size file downloaded', 'woocommerce' ), 400 );
|
||||
}
|
||||
|
||||
unset( $response );
|
||||
|
@ -1830,13 +1821,17 @@ class WC_API_Products extends WC_API_Resource {
|
|||
* @return array
|
||||
*/
|
||||
public function get_product_by_sku( $sku, $fields = null ) {
|
||||
$id = wc_get_product_id_by_sku( $sku );
|
||||
try {
|
||||
$id = wc_get_product_id_by_sku( $sku );
|
||||
|
||||
if ( empty( $id ) ) {
|
||||
return new WP_Error( 'woocommerce_api_invalid_product_sku', __( 'Invalid product SKU', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
if ( empty( $id ) ) {
|
||||
throw new WC_API_Exception( 'woocommerce_api_invalid_product_sku', __( 'Invalid product SKU', 'woocommerce' ), 404 );
|
||||
}
|
||||
|
||||
return $this->get_product( $id, $fields );
|
||||
} catch ( WC_API_Exception $e ) {
|
||||
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
|
||||
}
|
||||
|
||||
return $this->get_product( $id, $fields );
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue