WooCommerce Code Reference

GetProduct
in package

Query to retrieve a single product by ID.

Demonstrates: authorize(), $_query_info, AuthorizationException.

Authorization logic: admins (manage_woocommerce) can read any product, non-admin users can only read their own products.

Table of Contents

authorize()  : bool
Authorize access to a specific product.
execute()  : object|null
Retrieve a product by ID.

Methods

authorize()

Authorize access to a specific product.

public authorize(int $id, bool $_preauthorized) : bool

Admins can read any product. Non-admin users can only read products they authored themselves.

Every inaccessible case throws AuthorizationException('Product not found.') — whether the ID doesn't exist, points at a non-product post type, or points at a product the caller doesn't own. This prevents callers from enumerating product IDs vs non-product post IDs via the response they get back (which would otherwise be "not found" vs "no permission").

Parameters
$id : int

The product ID.

$_preauthorized : bool

Whether the declared capability check passed.

Tags
throws
AuthorizationException

When the product is not accessible.

Return values
boolWhether the current user can read this product.

execute()

Retrieve a product by ID.

public execute(int $id[, array<string|int, mixed>|null $_query_info = null ]) : object|null
Parameters
$id : int

The product ID.

$_query_info : array<string|int, mixed>|null = null

Unified query info tree from the GraphQL request.

Return values
object|null