WC_API_Products::get_product_attribute_term()publicWC 2.5.0

Get the product attribute term for the given ID.

Method of the class: WC_API_Products{}

Return

Array|WP_Error.

Usage

$WC_API_Products = new WC_API_Products();
$WC_API_Products->get_product_attribute_term( $attribute_id, $id, $fields );
$attribute_id(int) (required)
Attribute ID.
$id(string) (required)
Product attribute term ID.
$fields(string|null)
Fields to limit response to.
Default: null

Changelog

Since 2.5.0 Introduced.

WC_API_Products::get_product_attribute_term() code WC 8.7.0

public function get_product_attribute_term( $attribute_id, $id, $fields = null ) {
	global $wpdb;

	try {
		$id = absint( $id );
		$attribute_id = absint( $attribute_id );

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

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

		$taxonomy = wc_attribute_taxonomy_name_by_id( $attribute_id );

		if ( ! $taxonomy ) {
			throw new WC_API_Exception( 'woocommerce_api_invalid_product_attribute_id', __( 'A product attribute with the provided ID could not be found', 'woocommerce' ), 404 );
		}

		$term = get_term( $id, $taxonomy );

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

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

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