Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

API - Term Exists Check #11

@danielbarenkamp

Description

@danielbarenkamp

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.

Bildschirmfoto 2019-07-19 um 08 52 15

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementImprovements that slightly enhance existing functionality and are fast to implement.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions