Developer guide
How the plugin stores data, the public functions and services, templates and the Pro API.
Architecture
- Free boots on
plugins_loadedafter checking PHP, WordPress and WooCommerce, then firessiteryn_woocommerce_affiliates_loaded. Pro boots on that action and checks Free's version and extension API (Plugin::API_VERSION=1.0). - Order events are bridged once into the plugin's own actions (
siteryn_woocommerce_affiliates_order_*), for classic and Store API checkout, HPOS and legacy storage. - Storage sits behind four replaceable services:
affiliates,commissions,payoutsandvisits. Register a replacement duringsiteryn_woocommerce_affiliates_loadedwithServices::register( $name, $factory ); it must implement the matching interface (AffiliateRepositoryInterface,CommissionRepositoryInterface,PayoutRepositoryInterface,VisitRepositoryInterface). - No custom database tables and no direct SQL on order tables.
Data stored
| What | Where |
|---|---|
| Affiliates | Private posts of type siteryn_affiliate (post name = referral code); meta _siteryn_wcaff_* (status, user, rate, website, promotion, payment email, note, daily visit counters _siteryn_wcaff_visits_Ymd, language) |
| Commissions | Posts of type siteryn_commission with statuses prefixed wcaff-; meta order id, amount, base, currency, lines, source, coupon, payout id, note |
| Payouts | Posts of type siteryn_payout with statuses prefixed wcaffp-; meta amount, currency, commission ids, reference, payment email, note, paid date |
| Orders | Order meta _siteryn_wcaff_affiliate_id, _siteryn_wcaff_referral_source (cookie, session, coupon, manual; Pro adds lifetime), _siteryn_wcaff_referral_time, _siteryn_wcaff_referral_coupon |
| Coupons, products, categories | _siteryn_wcaff_affiliate_id on coupons; _siteryn_wcaff_rate_type / _siteryn_wcaff_rate on products and product categories |
| Settings | siteryn_woocommerce_affiliates_settings, siteryn_woocommerce_affiliates_version; Pro: siteryn_woocommerce_affiliates_pro_settings, _pro_tiers, _pro_fields, _pro_rules, _pro_version |
| Cookies | siteryn_wcaff_ref (affiliate id, time, signature); Pro siteryn_wcaff_pro_cmp (campaign and UTM) |
| Pro affiliate data | Taxonomies siteryn_wcaff_group, siteryn_wcaff_tag; meta _siteryn_wcaff_pro_tier, _pro_tier_history, _pro_notes, _pro_window_days, _pro_field_{key}; commission meta _siteryn_wcaff_pro_campaign, _pro_customer, _pro_ttc, _pro_adjustments |
Functions
| Function | Returns |
|---|---|
siteryn_woocommerce_affiliates_get_setting( $key ) | A Free setting value |
siteryn_woocommerce_affiliates_get_affiliate( $affiliate_id ) | Affiliate or null |
siteryn_woocommerce_affiliates_get_current_affiliate() | The signed-in user's affiliate, or null |
siteryn_woocommerce_affiliates_get_referral_url( $affiliate_id, $base = '' ) | Referral link to the home page or another page of the store |
siteryn_woocommerce_affiliates_get_order_affiliate( $order ) | The affiliate credited for an order, or null |
siteryn_woocommerce_affiliates_get_stats( $affiliate_id = 0, $from = '', $to = '' ) | Visits, orders, conversions, rate and per-currency amounts (0 = whole program) |
siteryn_woocommerce_affiliates_service( $name ) | A registered service |
siteryn_woocommerce_affiliates_api_version() · siteryn_woocommerce_affiliates_pro_api_version() | 1.0 |
Status changes should go through Affiliates\AffiliateManager (register(), set_status(), change_code(), set_rate()), Commissions\CommissionManager (set_status(), assign( $order, $affiliate_id )) and Payouts\PayoutManager (due(), create(), mark_paid(), cancel()) so the transition rules, events and emails apply.
Shortcode
[siteryn_affiliate_area] shows the application form, the affiliate's status or the dashboard. It takes no attributes.
Templates
| Template | Copy to your theme as |
|---|---|
affiliate-area/apply.php, dashboard.php, status.php, login.php, closed.php | siteryn-woocommerce-affiliates/affiliate-area/… |
emails/siteryn-wcaff-admin-new-affiliate.php, siteryn-wcaff-affiliate-approved.php, siteryn-wcaff-affiliate-rejected.php (and emails/plain/) | woocommerce/emails/… |
Each template has an @version; an admin notice lists theme copies that are older than the plugin's. Template variables can be changed with siteryn_woocommerce_affiliates_template_args and siteryn_woocommerce_affiliates_area_args.
AJAX endpoints and scripts
- Admin only:
siteryn_woocommerce_affiliates_save_settings(save a tab),siteryn_woocommerce_affiliates_load_tab(load a Program tab) and Pro'ssiteryn_woocommerce_affiliates_pro_license_action. All check a nonce andmanage_woocommerce. - Every other admin change (affiliate status, code, rate, commissions, payouts, reset) is a nonce-checked
admin-post.phpform. - The storefront form posts (application, payment email) are handled on
wp_loadedwith nonces and rate limits. There is no REST API. - Free ships a storefront AJAX router (
Ajax::register( $name, $callback, $args ), actions prefixedsiteryn_woocommerce_affiliates_, nonce and rate limit built in) with no endpoints registered in 1.0.0. The storefront script exposeswindow.siterynWooCommerceAffiliates.request( name, data )for it and handles the Copy link button. It fires no custom JavaScript events. - Storefront CSS and JS load only where the affiliate area renders, or everywhere with
add_filter( 'siteryn_woocommerce_affiliates_load_frontend_assets', '__return_true' ).
Examples
// Give a VIP group of products 20% regardless of other rates.
add_filter( 'siteryn_woocommerce_affiliates_line_rate', function ( $rate, $line, $affiliate ) {
if ( has_term( 'vip', 'product_tag', $line['product_id'] ) ) {
return array( 'type' => 'percentage', 'rate' => 20, 'source' => 'vip' );
}
return $rate;
}, 10, 3 );
// Respect your cookie banner before setting the referral cookie.
add_filter( 'siteryn_woocommerce_affiliates_track_visit', function ( $track ) {
return $track && isset( $_COOKIE['my_consent_marketing'] );
} );
Pro API
Pro 1.0.0 manages these through PHP only (no admin screens):
use Siteryn_WooCommerce_Affiliates_Pro\Affiliates\Tiers;
use Siteryn_WooCommerce_Affiliates_Pro\Affiliates\Groups;
use Siteryn_WooCommerce_Affiliates_Pro\Affiliates\CustomFields;
use Siteryn_WooCommerce_Affiliates_Pro\Commissions\Rules;
$gold = Tiers::save( array( 'name' => 'Gold', 'rank' => 2, 'rate_type' => 'percentage', 'rate' => 15, 'window_days' => 60 ) );
Tiers::assign( $affiliate_id, $gold['slug'], 'Top seller' );
$group = Groups::create( 'Influencers' );
Groups::add( $affiliate_id, array( $group ) );
CustomFields::save_definitions( array(
array( 'label' => 'Audience size', 'type' => 'select', 'choices' => "Under 1,000\n1,000 - 10,000", 'required' => true, 'application' => true ),
) );
Rules::save( array( 'name' => 'New customers 15%', 'kind' => 'rate', 'customer' => 'new', 'rate_type' => 'percentage', 'rate' => 15, 'priority' => 20 ) );
Rules::save( array( 'name' => 'Monthly cap', 'kind' => 'cap', 'amount' => 500, 'cap_period' => 'month' ) );
Also: Tags (by name), Notes::add(), AffiliateSearch::search( $criteria ), BulkActions::apply( $ids, $action, $value ), Tracking\AttributionWindows::set_window() and Tracking\AttributionReport::by( 'source'|'campaign'|'customer', $from, $to ). Pro modules can be added with Features::register() on siteryn_woocommerce_affiliates_pro_register_modules. See the Hooks reference.