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'] ) ) {
$product->set_parent_id( $request['parent_id'] );
}
@ -1354,13 +1354,18 @@ 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 );
$child->set_parent_id( 0 );
$child->save();
if ( ! empty( $child ) ) {
$child->set_parent_id( 0 );
$child->save();
}
}
}

View File

@ -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();
}
}
}

View File

@ -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();
}
}
}

View File

@ -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();
}
}
}