ProductCategoriesById.php
<?php
namespace Automattic\WooCommerce\StoreApi\Routes\V1;
use Automattic\WooCommerce\StoreApi\Exceptions\RouteException;
class ProductCategoriesById extends AbstractRoute {
const IDENTIFIER = 'product-categories-by-id';
const SCHEMA_TYPE = 'product-category';
public function get_path() {
return self::get_path_regex();
}
public static function get_path_regex() {
return '/products/categories/(?P<id>[\d]+)';
}
public function get_args() {
return [
'args' => array(
'id' => array(
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
'type' => 'integer',
),
),
[
'methods' => \WP_REST_Server::READABLE,
'callback' => [ $this, 'get_response' ],
'permission_callback' => '__return_true',
'args' => array(
'context' => $this->get_context_param(
array(
'default' => 'view',
)
),
),
],
'schema' => [ $this->schema, 'get_public_item_schema' ],
];
}
protected function get_route_response( \WP_REST_Request $request ) {
$object = get_term( (int) $request['id'], 'product_cat' );
if ( ! $object || 0 === $object->id ) {
throw new RouteException( 'woocommerce_rest_category_invalid_id', __( 'Invalid category ID.', 'woocommerce' ), 404 );
}
$data = $this->prepare_item_for_response( $object, $request );
return rest_ensure_response( $data );
}
}