-
Notifications
You must be signed in to change notification settings - Fork 48
API - Term Exists Check #11
Description
Is your feature request related to a problem? Please describe.
It is very frustrating, that product categories get created even when they exists. This is due to the wp_insert_term function does not check children categories. For example in the live environment there are theses categories:
Media
- CDs
- Vinyl
When we do an API import via create product the categories "CDs" will be created as a new category.
Describe the solution you'd like
The category should be checked if it exists, before creating it. It is very simple, we added the following to the "includes/abstracts/abstract-wc-rest-terms-controller.php" starting before the wp_insert_term in line 397:
$termExists = get_term_by( 'name', $request['name'], 'product_cat' );
if ( $termExists ) {
$error_data = array( 'status' => 400 );
// If we're going to inform the client that the term exists,
// give them the identifier they can actually use.
$term_id = $termExists->term_id;
if ( $term_id ) {
$error_data['resource_id'] = $term_id;
}
return new WP_Error( 'term_exists', 'term_exists', $error_data );
}
Describe alternatives you've considered
None
Additional context
Add any other context or screenshots about the feature request here.
