WP Manifestindependent plugin directory
manifest / users / pmp-store-credits-integration

PMP Store Credits Integration

WordPress plugin: recurring store credits for Paid Memberships Pro members, redeemed at WooCommerce checkout.

★ 0stars

Install

Download source zip No release zip yet

This is the repository archive. It installs, but the folder name will carry the branch suffix and updates will not flow.

Install straight from GitHub with WP-CLI:

wp plugin install https://github.com/geo266/pmp-store-credits-integration/archive/refs/heads/main.zip
No built-in updater. New versions won't appear under Dashboard → Updates. Install new releases yourself, or let Git Updater track the repository.

A WordPress plugin that issues recurring store credit to Paid Memberships Pro members and lets them redeem it at WooCommerce checkout.

Status: reference implementation / handover snapshot. This is the single-file plugin as it ran in production, published for documentation and as a starting point for the refactor described in HANDOVER.md. It is not a polished public plugin: the known issues below are real and include one high-severity bug. Read them before deploying.


Why this exists

From the original handover — full text in HANDOVER.md §2:

  • Reward recurring membership automatically. A paying member should get immediate, tangible value on every renewal, without an administrator touching the account.
  • Control rollover and expiry. Unused credit may carry over for a defined grace period and then expire — enough to keep members engaged and buying, not so much that the store carries indefinite liability on its balance sheet.
  • Stay lightweight. Bridge PMPro and WooCommerce natively rather than install a heavy, expensive loyalty/points plugin that fights the site's architecture.

It was written because nothing off the shelf did this: neither PMPro nor WooCommerce could issue recurring credit tied to a billing cycle with custom rollover caps and time-based expiry; commercial credit plugins broke inside Breakdance's custom checkout templates or collided with its theme; and running WooCommerce 11.x alongside stale add-ons produced fatal function mismatches and schema conflicts.

The rollover half of that intent is not in the shipped code — it resets the balance each cycle instead of carrying unused credit forward. See known issues #2.


What it does

Capability How
Grant credit on joining the paid level pmpro_after_checkout — compares the order's membership_id with the configured target level
Grant credit on each renewal pmpro_subscription_payment_completed
Revoke credit when the member leaves the level pmpro_after_change_membership_level + pmpro_membership_post_membership_expiry
Catch level changes made by an admin admin_init sweep of pmpro_memberships_users rows modified in the last 5 minutes
Apply credit to the cart Negative WooCommerce fee, capped at the cart subtotal, non-taxable, auto-applied once a balance exists
Show the balance My Account dashboard, cart, checkout, and two shortcodes
Expire stale credit Daily WP-Cron event pmp_expire_credits
Admin tooling Manual grant to one user, bulk grant to existing members, last 50 ledger rows, plugin status panel

Requirements

  • WordPress 5.x or newer (uses dbDelta, WP-Cron, shortcodes, wp_doing_ajax).
  • PHP 7.0+ — the code uses the null-coalescing operator. 7.4 / 8.x recommended; it was not written for 8.3 deprecation-clean output.
  • Paid Memberships Pro — needs pmpro_getMembershipLevelForUser, pmpro_hasMembershipLevel, pmpro_after_checkout, pmpro_subscription_payment_completed.
  • WooCommerce — needs WC()->cart->add_fee() and HPOS-era order CRUD (wc_get_order).

The plugin registers its hooks only when the corresponding dependency is present, but it does not currently abort bootstrap when both are missing — see known issues #7.

Install

  1. Copy this folder to wp-content/plugins/pmp-store-credits-integration/.
  2. Activate PMP Store Credits Integration in Plugins.
  3. The ledger table {prefix}pmp_store_credits is created on the next init (and again on activation).
  4. Grant the level-1 cohort an opening balance from Store Credits → Award Credits to Existing Members.

Configuration

Three private properties at the top of PMP_Store_Credits are the entire configuration surface:

private $credit_amount          = 5;   // amount granted per cycle, in store currency (£)
private $target_membership_level = 1;  // PMPro level ID that earns credit
private $expiration_days        = 30;  // validity window of each grant

Change them in the file; there is no settings screen. Moving them to constants or filters is on the roadmap.

Hosting notes

  • Breakdance / Breakdance Zero Theme — the theme requires the Breakdance plugin to stay active; never disable all plugins globally to debug, the front end renders a blank "Breakdance not enabled" notice.
  • SpeedyCache Pro / LiteSpeed — exclude My Account, cart, checkout and admin-ajax.php from page cache and JS/CSS minification, otherwise balances render stale or the apply-credit AJAX call breaks.
  • Cron — expiry runs as a single unbounded query plus a per-member re-grant loop. On a large list, batch it (see roadmap) or raise max_execution_time to 300 and memory_limit to 512M.

How it works

Allocation. A grant is remove_all_credits() followed by one insert, so a member always holds exactly one active row. add_credits() also cancels the previous balance, which means the "rollover" goal from the handover is not implemented — the current model is "reset to £5 at the start of each cycle", not "carry unused credit forward".

Redemption. woocommerce_cart_calculate_fees adds a fee named Store Credit with a negative amount, capped at the cart subtotal. The flag use_store_credits is stored in the WC session and set automatically the first time a balance is seen.

Expiry. The daily job expires every active row past expiration_date, then re-grants credit to members who still hold the target level. Credit therefore renews through cron as well as through PMPro payment hooks.

Hook map

Hook Callback Purpose
plugins_loaded (priority 20) new PMP_Store_Credits() Bootstrap after other plugins
pmpro_after_checkout award_credits_after_checkout Grant on joining
pmpro_subscription_payment_completed award_credits_on_payment Grant on renewal
pmpro_after_change_membership_level two separate closures Revoke on leave/downgrade
pmpro_membership_post_membership_expiry remove_credits_on_cancellation Revoke on expiry
admin_init check_pending_level_assignments Reconcile admin-side level edits
woocommerce_cart_calculate_fees apply_credit_discount Apply balance as a fee
woocommerce_checkout_order_processed deduct_credits_on_purchase Mark credit used (does not fire — see #1)
woocommerce_account_dashboard, woocommerce_before_cart_table, woocommerce_before_checkout_form display callbacks Balance UI
wp schedule_credit_expiration Register the daily event
pmp_expire_credits expire_old_credits Expire + re-grant
init check_and_create_table Table existence check

Database schema

{prefix}pmp_store_credits

Column Type Notes
id mediumint(9) auto Primary key
user_id bigint(20) Indexed
credit_amount decimal(10,2) In store currency
earned_date datetime Defaults to CURRENT_TIMESTAMP
expiration_date datetime Indexed
status varchar(20) active, used, cancelled, expired (indexed)
source varchar(50) checkout, renewal, level_change, signup, admin, admin_change, auto_renewal

Balances are always read as SUM(credit_amount) WHERE status='active' AND expiration_date > NOW(), gated on pmpro_hasMembershipLevel() for the target level.

Shortcodes and admin UI

Shortcode Output
[store_credits_balance] Balance card for the logged-in member
[store_credits_form] Balance + "Apply Discount" button; renders nothing at zero balance
[credit_status] Debug line (user ID, balance, membership state) — administrators only

Store Credits appears twice in wp-admin: as a submenu under Memberships (or Tools when PMPro is inactive) and as a top-level menu, both pointing at the same page. The page grants credit to a user ID, bulk-grants to existing level members, lists the last 50 ledger rows, and shows a status panel for PMPro / WooCommerce / the table.

Known issues

Findings from the pre-publication review of the shipped code. #1 and #2 change behaviour, not just style.

# Severity Issue
1 High Redemption is never booked. add_fee() names the fee Store Credit, but deduct_credits_on_purchase() looks for a fee named Store Credit Applied. The match never happens, so the ledger stays active and a member can spend the same balance on repeated orders until it expires. deduct_credits() also ignores its $amount argument and marks every active row used, so partial redemption is not modelled.
2 High No rollover. add_credits() calls remove_all_credits() first, cancelling any unused balance. The handover's rollover requirement is unfulfilled; members are reset to the fixed amount each cycle.
3 Medium assets/credits.js is enqueued on every front-end page but the file is not in the repository, so each page load requests a missing script. The real handler is inline in display_credit_form().
4 Medium pmpro_after_change_membership_level is registered twice with overlapping logic, and one handler echoes `