WC_API_Resource::delete()protectedWC 2.1

Delete a given resource

Method of the class: WC_API_Resource{}

No Hooks.

Return

Array|WP_Error.

Usage

// protected - for code of main (parent) or child class
$result = $this->delete( $id, $type, $force );
$id(int) (required)
the resource ID
$type(string) (required)
the resource post type, or customer
$force(true|false)
true to permanently delete resource, false to move to trash (not supported for customer)
Default: false

Changelog

Since 2.1 Introduced.

WC_API_Resource::delete() code WC 8.6.1

protected function delete( $id, $type, $force = false ) {

	if ( 'shop_order' === $type || 'shop_coupon' === $type ) {
		$resource_name = str_replace( 'shop_', '', $type );
	} else {
		$resource_name = $type;
	}

	if ( 'customer' === $type ) {

		$result = wp_delete_user( $id );

		if ( $result ) {
			return array( 'message' => __( 'Permanently deleted customer', 'woocommerce' ) );
		} else {
			return new WP_Error( 'woocommerce_api_cannot_delete_customer', __( 'The customer cannot be deleted', 'woocommerce' ), array( 'status' => 500 ) );
		}
	} else {

		// delete order/coupon/product/webhook
		$result = ( $force ) ? wp_delete_post( $id, true ) : wp_trash_post( $id );

		if ( ! $result ) {
			return new WP_Error( "woocommerce_api_cannot_delete_{$resource_name}", sprintf( __( 'This %s cannot be deleted', 'woocommerce' ), $resource_name ), array( 'status' => 500 ) );
		}

		if ( $force ) {
			return array( 'message' => sprintf( __( 'Permanently deleted %s', 'woocommerce' ), $resource_name ) );

		} else {

			$this->server->send_status( '202' );

			return array( 'message' => sprintf( __( 'Deleted %s', 'woocommerce' ), $resource_name ) );
		}
	}
}