WordPress at Your Fingertips
function is not described

WC_API_Products::create_product_tag() public WC 2.5.0

Create a new product tag.

{} It's a method of the class: WC_API_Products{}

Return

Array|WP_Error. Product tag if succeed, otherwise WP_Error will be returned

Usage

$WC_API_Products = new WC_API_Products();
$WC_API_Products->create_product_tag( $data );
$data(array) (required)
Posted data

Changelog

Since 2.5.0 Introduced.

Code of WC_API_Products::create_product_tag() WC 5.6.0

public function create_product_tag( $data ) {
	try {
		if ( ! isset( $data['product_tag'] ) ) {
			throw new WC_API_Exception( 'woocommerce_api_missing_product_tag_data', sprintf( __( 'No %1$s data specified to create %1$s', 'woocommerce' ), 'product_tag' ), 400 );
		}

		// Check permissions
		if ( ! current_user_can( 'manage_product_terms' ) ) {
			throw new WC_API_Exception( 'woocommerce_api_user_cannot_create_product_tag', __( 'You do not have permission to create product tags', 'woocommerce' ), 401 );
		}

		$defaults = array(
			'name'        => '',
			'slug'        => '',
			'description' => '',
		);

		$data = wp_parse_args( $data['product_tag'], $defaults );
		$data = apply_filters( 'woocommerce_api_create_product_tag_data', $data, $this );

		$insert = wp_insert_term( $data['name'], 'product_tag', $data );
		if ( is_wp_error( $insert ) ) {
			throw new WC_API_Exception( 'woocommerce_api_cannot_create_product_tag', $insert->get_error_message(), 400 );
		}
		$id = $insert['term_id'];

		do_action( 'woocommerce_api_create_product_tag', $id, $data );

		$this->server->send_status( 201 );

		return $this->get_product_tag( $id );
	} catch ( WC_API_Exception $e ) {
		return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
	}
}