WC_AJAX::add_new_attributepublic staticWC 1.0

Add a new attribute via ajax function.

Method of the class: WC_AJAX{}

No Hooks.

Returns

null. Nothing (null).

Usage

$result = WC_AJAX::add_new_attribute();

WC_AJAX::add_new_attribute() code WC 11.0.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 {
				VisualAttributeTermMeta::save_term_visual_from_request( (int) $result['term_id'], $taxonomy, $_POST ); // phpcs:ignore WordPress.Security.NonceVerification.Missing

				$term = get_term_by( 'id', $result['term_id'], $taxonomy );

				if ( ! $term ) {
					wp_send_json(
						array(
							'error' => __( 'Term not found', 'woocommerce' ),
						)
					);
				}

				$response = array(
					'term_id' => $term->term_id,
					'name'    => $term->name,
					'slug'    => $term->slug,
				);

				if ( VisualAttributeTermMeta::is_visual_attribute_taxonomy( $taxonomy ) ) {
					$response['visual'] = VisualAttributeTermMeta::get_term_visual( (int) $term->term_id );
				}

				wp_send_json( $response );
			}//end if
		}//end if
	}//end if
	wp_die( -1 );
}