WC_API_Products::delete_product_attribute_term()publicWC 2.5.0

Delete a product attribute term.

Method of the class: WC_API_Products{}

Return

Array|WP_Error.

Usage

$WC_API_Products = new WC_API_Products();
$WC_API_Products->delete_product_attribute_term( $attribute_id, $id );
$attribute_id(int) (required)
Attribute ID.
$id(int) (required)
the product attribute ID.

Changelog

Since 2.5.0 Introduced.

WC_API_Products::delete_product_attribute_term() code WC 8.7.0

public function delete_product_attribute_term( $attribute_id, $id ) {
	global $wpdb;

	try {
		// Check permissions.
		if ( ! current_user_can( 'manage_product_terms' ) ) {
			throw new WC_API_Exception( 'woocommerce_api_user_cannot_delete_product_attribute_term', __( 'You do not have permission to delete product attribute terms', 'woocommerce' ), 401 );
		}

		$taxonomy = wc_attribute_taxonomy_name_by_id( $attribute_id );

		if ( ! $taxonomy ) {
			throw new WC_API_Exception( 'woocommerce_api_invalid_product_attribute_id', __( 'A product attribute with the provided ID could not be found', 'woocommerce' ), 404 );
		}

		$id   = absint( $id );
		$term = wp_delete_term( $id, $taxonomy );

		if ( ! $term ) {
			throw new WC_API_Exception( 'woocommerce_api_cannot_delete_product_attribute_term', sprintf( __( 'This %s cannot be deleted', 'woocommerce' ), 'product_attribute_term' ), 500 );
		} elseif ( is_wp_error( $term ) ) {
			throw new WC_API_Exception( 'woocommerce_api_cannot_delete_product_attribute_term', $term->get_error_message(), 400 );
		}

		do_action( 'woocommerce_api_delete_product_attribute_term', $id, $this );

		return array( 'message' => sprintf( __( 'Deleted %s', 'woocommerce' ), 'product_attribute' ) );
	} catch ( WC_API_Exception $e ) {
		return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
	}
}