WC_API_Products::get_product_shipping_class()publicWC 2.5.0

Get the product shipping class for the given ID.

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

Возвращает

Массив|WP_Error. Product shipping class if succeed, otherwise WP_Error will be returned

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

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

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

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

Код WC_API_Products::get_product_shipping_class() WC 8.7.0

public function get_product_shipping_class( $id, $fields = null ) {
	try {
		$id = absint( $id );
		if ( ! $id ) {
			throw new WC_API_Exception( 'woocommerce_api_invalid_product_shipping_class_id', __( 'Invalid product shipping class ID', 'woocommerce' ), 400 );
		}

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

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

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

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

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

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