WC_API_Products::get_product_tag()publicWC 2.5.0

Get the product tag for the given ID.

Метод класса: WC_API_Products{}

Хуки из метода

Возвращает

Массив|WP_Error.

Использование

$WC_API_Products = new WC_API_Products();
$WC_API_Products->get_product_tag( $id, $fields );
$id(строка) (обязательный)
Product tag term ID
$fields(строка|null)
Fields to limit response to
По умолчанию: null

Список изменений

С версии 2.5.0 Введена.

Код WC_API_Products::get_product_tag() WC 8.7.0

public function get_product_tag( $id, $fields = null ) {
	try {
		$id = absint( $id );

		// Validate ID
		if ( empty( $id ) ) {
			throw new WC_API_Exception( 'woocommerce_api_invalid_product_tag_id', __( 'Invalid product tag ID', 'woocommerce' ), 400 );
		}

		// Permissions check
		if ( ! current_user_can( 'manage_product_terms' ) ) {
			throw new WC_API_Exception( 'woocommerce_api_user_cannot_read_product_tags', __( 'You do not have permission to read product tags', 'woocommerce' ), 401 );
		}

		$term = get_term( $id, 'product_tag' );

		if ( is_wp_error( $term ) || is_null( $term ) ) {
			throw new WC_API_Exception( 'woocommerce_api_invalid_product_tag_id', __( 'A product tag with the provided ID could not be found', 'woocommerce' ), 404 );
		}

		$term_id = intval( $term->term_id );

		$tag = array(
			'id'          => $term_id,
			'name'        => $term->name,
			'slug'        => $term->slug,
			'description' => $term->description,
			'count'       => intval( $term->count ),
		);

		return array( 'product_tag' => apply_filters( 'woocommerce_api_product_tag_response', $tag, $id, $fields, $term, $this ) );
	} catch ( WC_API_Exception $e ) {
		return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
	}
}