WC_AJAX::add_new_attribute()public staticWC 1.0

Add a new attribute via ajax function.

Method of the class: WC_AJAX{}

No Hooks.

Return

null. Nothing (null).

Usage

$result = WC_AJAX::add_new_attribute();

WC_AJAX::add_new_attribute() code WC 8.6.1

public static function add_new_attribute() {
	check_ajax_referer( 'add-attribute', 'security' );

	if ( current_user_can( 'manage_product_terms' ) && isset( $_POST['taxonomy'], $_POST['term'] ) ) {
		$taxonomy = esc_attr( wp_unslash( $_POST['taxonomy'] ) ); // phpcs:ignore
		$term     = wc_clean( wp_unslash( $_POST['term'] ) );

		if ( taxonomy_exists( $taxonomy ) ) {

			$result = wp_insert_term( $term, $taxonomy );

			if ( is_wp_error( $result ) ) {
				wp_send_json(
					array(
						'error' => $result->get_error_message(),
					)
				);
			} else {
				$term = get_term_by( 'id', $result['term_id'], $taxonomy );
				wp_send_json(
					array(
						'term_id' => $term->term_id,
						'name'    => $term->name,
						'slug'    => $term->slug,
					)
				);
			}
		}
	}
	wp_die( -1 );
}