Développeurs : Extraits de code et ressources
Updated septembre 21, 2026
The REST API is wcusage/v2. Enable it under Coupon Affiliates > Admin Tools > API, then see API & Webhooks for merchant setup (keys, scopes, webhooks) and the API documentation for authentication, routes, payloads and examples.
Below are PHP snippets and resources that customers have asked for. They are not a substitute for the REST API reference.
Tables de la base de données
Le plugin possède ses propres tables de base de données à partir desquelles vous pouvez obtenir certaines données :
wcusage_activity - Journal d'activité.
wcusage_api_keys – REST API keys (hashed), with their scopes, status and last-used date.
wcusage_campaigns - Campagnes d'URL de référence.
wcusage_clicks - Journal des clics de l'URL de référence.
wcusage_directlinks - Domaines de suivi des liens directs.
wcusage_mlainvites - Invitations d'affiliés multi-niveaux.
wcusage_payouts - Paiement des commissions
wcusage_register - Applications d'enregistrement des affiliés.
wcusage_rewards_log – Log of performance bonuses (rewards) earned by affiliates, including the reward name, requirement met and bonuses granted.
Webhooks are not stored in a table; they are saved in the wcusage_api_webhooks option.
Commandes des affiliés
Obtenir toutes les commandes d'affiliés pour un coupon, sans certaines plages de données. Cette fonction renvoie un tableau de données comprenant le total des calculs pour toutes les commandes et toutes les commandes individuelles.
$orders = wcusage_wh_getOrderbyCouponCode( $coupon_code, $start_date, $end_date, '', 1 );
Données de commande
Cette fonction vous permet d'obtenir un tableau de données relatives à l'affiliation pour une certaine commande. Cette fonction renvoie un tableau de données comprenant les revenus de la commission.
$order_data = wcusage_calculate_order_data( $order_id, $coupon_code, 0, 1 ) ;
Pour obtenir la commission de la commande, par exemple :
$commission = $order_data['totalcommission'];
URL de l'affilié
La fonction suivante permet d'obtenir l'URL d'affiliation d'un coupon :
$coupon_code = ""; // Set the coupon code name here.
$affiliate_url = wcusage_get_affiliate_url($coupon_code);
Si vous souhaitez créer votre propre URL, voici un exemple :
$coupon_code = ""; // Set the coupon code name here.
$prefix = wcusage_get_setting_value('wcusage_field_urls_prefix', 'coupon');
$affiliate_url = get_home_url() . "?" . $prefix . "=" . $coupon_code;
Hook (Filter): Change the Referral URL
Every affiliate link is built from a base URL, which comes from the “Default Referral URL Page” setting. That setting is corrected back to your WordPress domain whenever the two disagree, so that a URL left behind by a site migration repairs itself rather than sending your affiliates to a domain the shop no longer owns.
If you deliberately serve your shop from a different domain to the WordPress install – a separate front end or a headless setup, for example – use the “wcusage_default_ref_url” filter rather than the setting:
add_filter( 'wcusage_default_ref_url', function( $url ) {
return 'https://shop.example.com/';
} );
The URL you return is used exactly as given, so include a trailing slash (or a path) if you want one. It is not checked against your site’s own domain, on the understanding that requests to it have to reach your site for referrals to be tracked.
This filter covers the affiliate dashboard, the referral URL generator, the social share buttons, QR codes, creatives, the affiliate widget, the referral URL shortcodes and the registration emails.
To replace a whole link instead – a vanity or shortened link, for example – use the “wcusage_affiliate_url” filter, which runs after the coupon code has been added:
add_filter( 'wcusage_affiliate_url', function( $affiliate_url, $coupon_code ) {
return 'https://go.example.com/' . rawurlencode( $coupon_code );
}, 10, 2 );
This one covers the affiliate dashboard, QR codes, creatives, the referral URL shortcodes and the registration emails.
Recording Referral Clicks from a Custom Front End
If your referral links are handled outside of WordPress – a headless or decoupled front end, for example – you can record the click yourself, so that it still reaches the affiliate’s URL statistics. Use the “wcusage_record_click()” function:
$click_id = wcusage_record_click( $coupon_code, array(
'campaign' => '', // Campaign / "src" value.
'page' => 0, // Post ID of the landing page. 0 shows as the homepage.
'referrer' => '', // A full URL or a domain. Only the domain is stored.
'ip' => null, // Visitor IP, or an ID of up to 64 characters.
'converted' => false,
) );
It returns the ID of the click it saved, or false if the coupon was not found, the referrer is one of your blocked domains, or the click could not be saved.
- The first parameter is a coupon code, or a coupon ID.
- Setting ‘ip’ to null detects it from the current request the way the plugin’s own tracking does, which is only meaningful when the visitor’s browser is what made the request.
- Unlike the plugin’s own tracking, this does no throttling. If your front end can be hit more than once for the same visit (bots, prefetching, a page refresh) you should decide for yourself whether a visit is a new click before calling it.
A “wcusage_click_recorded” action fires afterwards, passing the click ID, the coupon ID, and the details the click was recorded with.
Marking the conversion
Conversions are normally matched using a cookie set when the visitor follows the referral link, which a front end recording its own clicks has no way to set. Keep hold of the click ID returned above – saved against the order, for example – and hand it back with the “wcusage_conversion_click_id” filter:
add_filter( 'wcusage_conversion_click_id', function( $click_id, $order_id ) {
return get_post_meta( $order_id, 'my_referral_click_id', true );
}, 10, 2 );
Return an empty value to leave the order unattributed. Note that this filter runs where the plugin marks conversions, on the WooCommerce order received page, so a front end that never loads that page will need to mark the conversion itself.
Total des ventes et des commissions
Obtenez le total des ventes et des commissions gagnées par un certain coupon, sans une certaine plage de dates. Leave $start_date empty to get all sales.
$orders = wcusage_wh_getOrderbyCouponCode( $coupon_code, $start_date, $end_date, '', 1 );
$total_orders = $orders['total_orders']; // Total Sales
$total_discounts = $orders['full_discount']; // Total Discounts
$total_commission = $orders['total_commission']; // Total Commission
$order_count = $orders['total_count']; // Orders Count
Commission non rémunérée
Obtenir la commission totale non payée pour un coupon d'affilié.
$coupon_id = ""; // Set the coupon code ID here.
$unpaid_commission = get_post_meta( $coupon_id, 'wcu_text_unpaid_commission', true );
Crochet (Action) : Inscription d'un nouvel affilié
Lorsqu'il y a une nouvelle inscription d'affilié, il lance le hook "wcusage_hook_registration_new" qui transmet l'ID de l'inscription, l'ID de l'utilisateur et le code du coupon.
Vous pouvez donc appeler ce crochet pour accepter automatiquement l'enregistrement sous certaines conditions avec la fonction "wcusage_set_registration_status". Par exemple, cette fonction acceptera automatiquement l'enregistrement si le rôle de l'utilisateur est "exemple" :
add_action( 'wcusage_hook_registration_new', 'trigger_wcusage_hook_registration_new', 10, 3 );
function trigger_wcusage_hook_registration_new( $registration_id, $user_id, $coupon_code ) {
$user_info = get_userdata( $user_id );
$roles = $user_info->roles;
if ( in_array( 'example', $roles ) ) {
wcusage_set_registration_status( 'accepted', $registration_id, $user_id, $coupon_code, '', '' );
}
}
Crochet (Action) : Nouvel affilié accepté
Lorsqu'un nouvel affilié est accepté et ajouté à votre programme, il exécute le "wcusage_hook_affiliate_register_accepted" qui transmet les éléments suivants :
- $id - ID d'enregistrement.
- $userid - Identifiant de l'utilisateur.
- $coupon_code - Code Coupon.
- $message - Le message d'acceptation de l'enregistrement.
- $status - Le statut de leur enregistrement.
add_action( 'wcusage_hook_affiliate_register_accepted', 'trigger_wcusage_hook_registration_accepted', 10, 5 );
function trigger_wcusage_hook_registration_accepted( $id, $userid, $coupon_code, $message, $status ) {
// Your code here.
}
Hook (Action): Trigger a Performance Bonus from a Custom Event (PRO)
Performance Bonuses can be triggered from your own theme or plugin code. When creating a bonus, set the trigger type to “Developers: Custom Event / Action” and enter a Custom Event Key. Then fire the “wcusage_custom_event” action with that same key whenever the event happens:
do_action( 'wcusage_custom_event', 'your_event_key', $user_id );
// Or target a specific affiliate coupon:
do_action( 'wcusage_custom_event', 'your_event_key', $user_id, array(), 'COUPONCODE' );
- ‘your_event_key’ must match the Custom Event Key entered on the bonus.
- $user_id is the WordPress user ID of the affiliate to reward.
- The fourth parameter (optional) is a coupon code or coupon post ID to target a specific affiliate coupon. When omitted, all coupons belonging to the user are checked.
See (PRO) Primes de performance for how to set up the bonus itself.
Legacy v1 API
The original three endpoints under woo-coupon-usage/v1 are still registered for existing integrations. They require a WordPress administrator role. They are not the current API. New integrations should use wcusage/v2.
GET /wp-json/woo-coupon-usage/v1/coupon-info with coupon_id. Returns nom_du_coupon, commission_non_rémunéréeet paiements en attente.
GET /wp-json/woo-coupon-usage/v1/users-coupons with utilisateur (the login name). Returns coupon IDs assigned to that user, with unpaid commission.
POST /wp-json/woo-coupon-usage/v1/request-payout with coupon_id et utilisateur (login). Returns 1 on success or 0 on failure. PRO.
Vous avez besoin d'aide pour une fonction personnalisée ? N'hésitez pas à nous contacter et nous pourrons peut-être vous orienter dans la bonne direction.