Subscription Cancellation System

Overview

The cancellation system in The Golfers Journal allows subscribers to cancel their subscriptions through a user-friendly interface. The system includes both a Gutenberg block for displaying cancellation links and backend functionality for processing cancellations.

Components

A Gutenberg block that displays a cancellation link when appropriate. The block can be added to any page or post.

Features

  • Configurable link text
  • Option to display as a button
  • Text alignment options (left, center, right)
  • Automatic styling for both link and button variants

Usage

<!-- Example block usage -->
<!-- wp:journal-upgrades-downgrades/cancel-link {
    "linkText": "Cancel Subscription",
    "isButton": true,
    "align": "center"
} /-->

2. Cancellation Logic

The system implements several checks before allowing a cancellation:

  1. Subscription Validation
    • Verifies the subscription exists
    • Checks if the subscription is in a cancellable state
    • Validates the subscription has a future payment (unless on hold)
  2. User Permissions
    • Verifies the user is logged in
    • Checks if the user has permission to cancel the subscription
    • Validates the user owns the subscription
  3. System Settings
    • Checks if cancellations are enabled in the system
    • Validates any additional business rules

3. Cancellation Process

  1. Link Generation
    • The system generates a cancellation URL using jc_get_users_change_status_link()
    • The URL includes necessary parameters for processing the cancellation
  2. Status Update
    • When clicked, the link triggers a status change to ‘cancelled’
    • The system handles any necessary cleanup or notifications
  3. Error Handling
    • Graceful error handling for various scenarios
    • User-friendly error messages
    • Debug logging when WP_DEBUG is enabled

Configuration

System Settings

The cancellation system can be configured through the WordPress admin:

  • enable_cancellations: Toggle to enable/disable the cancellation feature
  • cancellations_interstitial_page: Optional page to show before cancellation

Block Settings

The cancel link block can be configured through the block editor:

  • Link Text: Customize the text displayed on the link/button
  • Display as Button: Toggle between link and button styles
  • Alignment: Choose between left, center, or right alignment

Styling

The block includes default styling for both link and button variants:

.wp-block-journal-upgrades-downgrades-cancel-link {
    margin: 1em 0;
}

.wp-block-journal-upgrades-downgrades-cancel-link.has-text-align-center {
    text-align: center;
}

.wp-block-journal-upgrades-downgrades-cancel-link.has-text-align-right {
    text-align: right;
}

.wp-block-journal-upgrades-downgrades-cancel-link a:not(.button) {
    text-decoration: underline;
    font-style: italic;
    text-underline-offset: 3px;
}

The styling provides:

  • Basic margin spacing
  • Text alignment support (left, center, right)
  • Special styling for non-button links (underlined, italic)
  • Button styling is inherited from the theme’s default button styles

Integration

The cancellation system integrates with:

  • WooCommerce Subscriptions
  • WordPress User System
  • Gutenberg Block Editor

Error Messages

The system provides clear feedback in various scenarios:

  • “No Subscription ID Provided” - When no subscription ID is present
  • “No Subscription Found” - When the subscription doesn’t exist
  • “Cancellations are not enabled” - When the feature is disabled
  • “Cannot Cancel Subscription” - When user lacks permission or subscription can’t be cancelled
  • “No URL Found” - When the cancellation URL can’t be generated

Development

Building the Block

To build the cancel link block:

cd blocks/cancel-link
npm install
npm run build

Adding Custom Logic

To extend the cancellation system:

  1. Hook into the wcs_view_subscription_actions filter
  2. Use the maybe_add_cancel_link function as a reference
  3. Implement additional checks or modifications as needed

Security Considerations

  • All URLs are properly escaped
  • User permissions are strictly checked
  • Subscription ownership is verified
  • Error messages are sanitized
  • Debug information is only logged when WP_DEBUG is enabled