Merge pull request #20670 from woocommerce/pr/20669

Deleting a product via API should handle children products regardless of type.
This commit is contained in:
Claudio Sanches 2018-07-10 15:16:13 -03:00 committed by GitHub
commit 1c7b2f3100
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 17 deletions

View File

@ -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'] ) ) { if ( isset( $request['parent_id'] ) ) {
$product->set_parent_id( $request['parent_id'] ); $product->set_parent_id( $request['parent_id'] );
} }
@ -1354,15 +1354,20 @@ class WC_REST_Products_Controller extends WC_REST_Legacy_Products_Controller {
if ( $object->is_type( 'variable' ) ) { if ( $object->is_type( 'variable' ) ) {
foreach ( $object->get_children() as $child_id ) { foreach ( $object->get_children() as $child_id ) {
$child = wc_get_product( $child_id ); $child = wc_get_product( $child_id );
if ( ! empty( $child ) ) {
$child->delete( true ); $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 ) { foreach ( $object->get_children() as $child_id ) {
$child = wc_get_product( $child_id ); $child = wc_get_product( $child_id );
if ( ! empty( $child ) ) {
$child->set_parent_id( 0 ); $child->set_parent_id( 0 );
$child->save(); $child->save();
} }
} }
}
$object->delete( true ); $object->delete( true );
$result = 0 === $object->get_id(); $result = 0 === $object->get_id();

View File

@ -429,15 +429,20 @@ class WC_API_Products extends WC_API_Resource {
if ( $product->is_type( 'variable' ) ) { if ( $product->is_type( 'variable' ) ) {
foreach ( $product->get_children() as $child_id ) { foreach ( $product->get_children() as $child_id ) {
$child = wc_get_product( $child_id ); $child = wc_get_product( $child_id );
if ( ! empty( $child ) ) {
$child->delete( true ); $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 ) { foreach ( $product->get_children() as $child_id ) {
$child = wc_get_product( $child_id ); $child = wc_get_product( $child_id );
if ( ! empty( $child ) ) {
$child->set_parent_id( 0 ); $child->set_parent_id( 0 );
$child->save(); $child->save();
} }
} }
}
$product->delete( true ); $product->delete( true );
$result = ! ( $product->get_id() > 0 ); $result = ! ( $product->get_id() > 0 );

View File

@ -488,15 +488,20 @@ class WC_API_Products extends WC_API_Resource {
if ( $product->is_type( 'variable' ) ) { if ( $product->is_type( 'variable' ) ) {
foreach ( $product->get_children() as $child_id ) { foreach ( $product->get_children() as $child_id ) {
$child = wc_get_product( $child_id ); $child = wc_get_product( $child_id );
if ( ! empty( $child ) ) {
$child->delete( true ); $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 ) { foreach ( $product->get_children() as $child_id ) {
$child = wc_get_product( $child_id ); $child = wc_get_product( $child_id );
if ( ! empty( $child ) ) {
$child->set_parent_id( 0 ); $child->set_parent_id( 0 );
$child->save(); $child->save();
} }
} }
}
$product->delete( true ); $product->delete( true );
$result = ! ( $product->get_id() > 0 ); $result = ! ( $product->get_id() > 0 );

View File

@ -1664,15 +1664,20 @@ class WC_REST_Products_V1_Controller extends WC_REST_Posts_Controller {
if ( $product->is_type( 'variable' ) ) { if ( $product->is_type( 'variable' ) ) {
foreach ( $product->get_children() as $child_id ) { foreach ( $product->get_children() as $child_id ) {
$child = wc_get_product( $child_id ); $child = wc_get_product( $child_id );
if ( ! empty( $child ) ) {
$child->delete( true ); $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 ) { foreach ( $product->get_children() as $child_id ) {
$child = wc_get_product( $child_id ); $child = wc_get_product( $child_id );
if ( ! empty( $child ) ) {
$child->set_parent_id( 0 ); $child->set_parent_id( 0 );
$child->save(); $child->save();
} }
} }
}
$product->delete( true ); $product->delete( true );
$result = ! ( $product->get_id() > 0 ); $result = ! ( $product->get_id() > 0 );