WC_API_Products::create_product_tag( array $data )

Create a new product tag.


Description Description


Parameters Parameters

$data

(Required) Posted data


Top ↑

Return Return

(array|WP_Error) Product tag if succeed, otherwise WP_Error will be returned


Top ↑

Source Source

File: includes/legacy/api/v3/class-wc-api-products.php

	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() ) );
		}
	}

Top ↑

Changelog Changelog

Changelog
Version Description
2.5.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.