Automattic\WooCommerce\Api\Mutations\Products
DeleteProduct::execute
Execute the mutation.
Method of the class: DeleteProduct{}
No Hooks.
Returns
true|false. Whether the product was deleted.
Usage
$DeleteProduct = new DeleteProduct(); $DeleteProduct->execute( #[Description( foo )] int $id, #[Description( fooo )] bool $force, ): bool;
[Description( foo )] int $id (required)
: .
[Description( fooo )] bool $force
: .
Default: false
- (required)
- .
DeleteProduct::execute() DeleteProduct::execute code WC 10.9.1
public function execute(
#[Description( 'The ID of the product to delete.' )]
int $id,
#[Description( 'Whether to permanently delete the product (bypass trash).' )]
bool $force = false,
): bool {
$wc_product = wc_get_product( $id );
if ( ! $wc_product instanceof \WC_Product ) {
throw new ApiException( 'Product not found.', 'NOT_FOUND', status_code: 404 );
}
// Capture the raw return value. A `(bool)` cast would coerce
// filter-originated `WP_Error` objects to `true`, reporting failure
// as success; we need to detect that case explicitly and surface
// the underlying error instead.
$deleted = $wc_product->delete( $force );
// phpcs:disable WordPress.Security.EscapeOutput.ExceptionNotEscaped -- Not HTML; serialized as JSON.
if ( $deleted instanceof \WP_Error ) {
throw new ApiException(
$deleted->get_error_message(),
'INTERNAL_ERROR',
status_code: 500,
);
}
// phpcs:enable WordPress.Security.EscapeOutput.ExceptionNotEscaped
return true === $deleted;
}