From c94a5df7a42f4cf45d7b02550f45d06a9f3237ec Mon Sep 17 00:00:00 2001 From: Eleanor Martin Date: Fri, 29 Jun 2018 15:01:10 +0100 Subject: [PATCH 1/2] Allowing API deletion of grouped products. #20666 --- includes/api/class-wc-rest-products-controller.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/includes/api/class-wc-rest-products-controller.php b/includes/api/class-wc-rest-products-controller.php index 05b9c11ea5b..bebe513af19 100644 --- a/includes/api/class-wc-rest-products-controller.php +++ b/includes/api/class-wc-rest-products-controller.php @@ -1359,8 +1359,10 @@ class WC_REST_Products_Controller extends WC_REST_Legacy_Products_Controller { } elseif ( $object->is_type( 'grouped' ) ) { foreach ( $object->get_children() as $child_id ) { $child = wc_get_product( $child_id ); - $child->set_parent_id( 0 ); - $child->save(); + if ( ! empty( $child ) ) { + $child->set_parent_id( 0 ); + $child->save(); + } } } From a480cff0d2ffe3f5893b29270def880f695034ee Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Fri, 29 Jun 2018 15:24:17 +0100 Subject: [PATCH 2/2] Logic is not grouped specific; only for product types with children --- includes/api/class-wc-rest-products-controller.php | 9 ++++++--- includes/api/legacy/v2/class-wc-api-products.php | 13 +++++++++---- includes/api/legacy/v3/class-wc-api-products.php | 13 +++++++++---- .../api/v1/class-wc-rest-products-controller.php | 13 +++++++++---- 4 files changed, 33 insertions(+), 15 deletions(-) diff --git a/includes/api/class-wc-rest-products-controller.php b/includes/api/class-wc-rest-products-controller.php index bebe513af19..6096837dbcb 100644 --- a/includes/api/class-wc-rest-products-controller.php +++ b/includes/api/class-wc-rest-products-controller.php @@ -877,7 +877,7 @@ class WC_REST_Products_Controller extends WC_REST_Legacy_Products_Controller { } } - // Product parent ID for groups. + // Product parent ID. if ( isset( $request['parent_id'] ) ) { $product->set_parent_id( $request['parent_id'] ); } @@ -1354,9 +1354,12 @@ class WC_REST_Products_Controller extends WC_REST_Legacy_Products_Controller { if ( $object->is_type( 'variable' ) ) { foreach ( $object->get_children() as $child_id ) { $child = wc_get_product( $child_id ); - $child->delete( true ); + if ( ! empty( $child ) ) { + $child->delete( true ); + } } - } elseif ( $object->is_type( 'grouped' ) ) { + } else { + // For other product types, if the product has children, remove the relationship. foreach ( $object->get_children() as $child_id ) { $child = wc_get_product( $child_id ); if ( ! empty( $child ) ) { diff --git a/includes/api/legacy/v2/class-wc-api-products.php b/includes/api/legacy/v2/class-wc-api-products.php index 59f07d4da2f..0da071ccb15 100644 --- a/includes/api/legacy/v2/class-wc-api-products.php +++ b/includes/api/legacy/v2/class-wc-api-products.php @@ -429,13 +429,18 @@ class WC_API_Products extends WC_API_Resource { if ( $product->is_type( 'variable' ) ) { foreach ( $product->get_children() as $child_id ) { $child = wc_get_product( $child_id ); - $child->delete( true ); + if ( ! empty( $child ) ) { + $child->delete( true ); + } } - } elseif ( $product->is_type( 'grouped' ) ) { + } else { + // For other product types, if the product has children, remove the relationship. foreach ( $product->get_children() as $child_id ) { $child = wc_get_product( $child_id ); - $child->set_parent_id( 0 ); - $child->save(); + if ( ! empty( $child ) ) { + $child->set_parent_id( 0 ); + $child->save(); + } } } diff --git a/includes/api/legacy/v3/class-wc-api-products.php b/includes/api/legacy/v3/class-wc-api-products.php index fb663565df4..71062b2820d 100644 --- a/includes/api/legacy/v3/class-wc-api-products.php +++ b/includes/api/legacy/v3/class-wc-api-products.php @@ -488,13 +488,18 @@ class WC_API_Products extends WC_API_Resource { if ( $product->is_type( 'variable' ) ) { foreach ( $product->get_children() as $child_id ) { $child = wc_get_product( $child_id ); - $child->delete( true ); + if ( ! empty( $child ) ) { + $child->delete( true ); + } } - } elseif ( $product->is_type( 'grouped' ) ) { + } else { + // For other product types, if the product has children, remove the relationship. foreach ( $product->get_children() as $child_id ) { $child = wc_get_product( $child_id ); - $child->set_parent_id( 0 ); - $child->save(); + if ( ! empty( $child ) ) { + $child->set_parent_id( 0 ); + $child->save(); + } } } diff --git a/includes/api/v1/class-wc-rest-products-controller.php b/includes/api/v1/class-wc-rest-products-controller.php index 137ebca40d9..9d893f039c8 100644 --- a/includes/api/v1/class-wc-rest-products-controller.php +++ b/includes/api/v1/class-wc-rest-products-controller.php @@ -1664,13 +1664,18 @@ class WC_REST_Products_V1_Controller extends WC_REST_Posts_Controller { if ( $product->is_type( 'variable' ) ) { foreach ( $product->get_children() as $child_id ) { $child = wc_get_product( $child_id ); - $child->delete( true ); + if ( ! empty( $child ) ) { + $child->delete( true ); + } } - } elseif ( $product->is_type( 'grouped' ) ) { + } else { + // For other product types, if the product has children, remove the relationship. foreach ( $product->get_children() as $child_id ) { $child = wc_get_product( $child_id ); - $child->set_parent_id( 0 ); - $child->save(); + if ( ! empty( $child ) ) { + $child->set_parent_id( 0 ); + $child->save(); + } } }