Merge pull request #17383 from woocommerce/fix/17332

[REST API] Fix category "image" returned type when is empty
This commit is contained in:
Mike Jolley 2017-10-26 13:01:50 +01:00 committed by GitHub
commit 8590cd297c
2 changed files with 18 additions and 13 deletions

View File

@ -32,9 +32,9 @@ class WC_REST_Product_Categories_Controller extends WC_REST_Product_Categories_V
/**
* Prepare a single product category output for response.
*
* @param WP_Term $item Term object.
* @param WP_REST_Request $request
* @return WP_REST_Response $response
* @param WP_Term $item Term object.
* @param WP_REST_Request $request Request instance.
* @return WP_REST_Response
*/
public function prepare_item_for_response( $item, $request ) {
// Get category display type.
@ -50,13 +50,14 @@ class WC_REST_Product_Categories_Controller extends WC_REST_Product_Categories_V
'parent' => (int) $item->parent,
'description' => $item->description,
'display' => $display_type ? $display_type : 'default',
'image' => array(),
'image' => null,
'menu_order' => (int) $menu_order,
'count' => (int) $item->count,
);
// Get category image.
if ( $image_id = get_woocommerce_term_meta( $item->term_id, 'thumbnail_id' ) ) {
$image_id = get_woocommerce_term_meta( $item->term_id, 'thumbnail_id' );
if ( $image_id ) {
$attachment = get_post( $image_id );
$data['image'] = array(

View File

@ -46,9 +46,9 @@ class WC_REST_Product_Categories_V1_Controller extends WC_REST_Terms_Controller
/**
* Prepare a single product category output for response.
*
* @param WP_Term $item Term object.
* @param WP_REST_Request $request
* @return WP_REST_Response $response
* @param WP_Term $item Term object.
* @param WP_REST_Request $request Request instance.
* @return WP_REST_Response
*/
public function prepare_item_for_response( $item, $request ) {
// Get category display type.
@ -64,13 +64,14 @@ class WC_REST_Product_Categories_V1_Controller extends WC_REST_Terms_Controller
'parent' => (int) $item->parent,
'description' => $item->description,
'display' => $display_type ? $display_type : 'default',
'image' => array(),
'image' => null,
'menu_order' => (int) $menu_order,
'count' => (int) $item->count,
);
// Get category image.
if ( $image_id = get_woocommerce_term_meta( $item->term_id, 'thumbnail_id' ) ) {
$image_id = get_woocommerce_term_meta( $item->term_id, 'thumbnail_id' );
if ( $image_id ) {
$attachment = get_post( $image_id );
$data['image'] = array(
@ -106,8 +107,8 @@ class WC_REST_Product_Categories_V1_Controller extends WC_REST_Terms_Controller
/**
* Update term meta fields.
*
* @param WP_Term $term
* @param WP_REST_Request $request
* @param WP_Term $term Term object.
* @param WP_REST_Request $request Request instance.
* @return bool|WP_Error
*/
protected function update_term_meta_fields( $term, $request ) {
@ -145,7 +146,10 @@ class WC_REST_Product_Categories_V1_Controller extends WC_REST_Terms_Controller
// Set the image title.
if ( ! empty( $request['image']['title'] ) ) {
wp_update_post( array( 'ID' => $image_id, 'post_title' => wc_clean( $request['image']['title'] ) ) );
wp_update_post( array(
'ID' => $image_id,
'post_title' => wc_clean( $request['image']['title'] ),
) );
}
} else {
delete_woocommerce_term_meta( $id, 'thumbnail_id' );