{"id":25758,"date":"2023-09-26T15:04:16","date_gmt":"2023-09-26T15:04:16","guid":{"rendered":"https:\/\/couponaffiliates.com\/docs\/developers\/"},"modified":"2026-09-21T23:57:54","modified_gmt":"2026-09-21T23:57:54","slug":"developpeurs","status":"publish","type":"aisa_doc","link":"https:\/\/couponaffiliates.com\/fr\/docs\/developers\/","title":{"rendered":"D\u00e9veloppeurs : Extraits de code et ressources"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The REST API is <code>wcusage\/v2<\/code>. Enable it under <strong>Coupon Affiliates &gt; Admin Tools &gt; API<\/strong>, then see <a href=\"https:\/\/couponaffiliates.com\/docs\/api-webhooks\/\">API &amp; Webhooks<\/a> for merchant setup (keys, scopes, webhooks) and the <a href=\"https:\/\/couponaffiliates.com\/api-docs\/\">API documentation<\/a> for authentication, routes, payloads and examples.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Below are PHP snippets and resources that customers have asked for. They are not a substitute for the REST API reference.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Database Tables<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The plugin does have its own database tables that you can get certain data from:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">wcusage_activity &#8211; Activity log.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">wcusage_api_keys &#8211; REST API keys (hashed), with their scopes, status and last-used date.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">wcusage_campaigns &#8211; Referral URL campaigns.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">wcusage_clicks &#8211; Referral URL clicks log.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">wcusage_directlinks &#8211; Direct link tracking domains.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">wcusage_mlainvites &#8211; Multi-Level affiliates invites.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">wcusage_payouts &#8211; Commission Payouts<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">wcusage_register &#8211; Affiliate registration applications.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">wcusage_rewards_log &#8211; Log of performance bonuses (rewards) earned by affiliates, including the reward name, requirement met and bonuses granted.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Webhooks are not stored in a table; they are saved in the <code>wcusage_api_webhooks<\/code> option.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Affiliate Orders<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Get all affiliate orders for a coupon, without certain data range. This returns an array of data including the total calculations for all the orders, and all the individual orders.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$orders = wcusage_wh_getOrderbyCouponCode( $coupon_code, $start_date, $end_date, &#039;&#039;, 1 );<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Order Data<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You can get an array of affiliate related data for a certain order via this function. This returns an array of data including the commission earnings.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$order_data = wcusage_calculate_order_data( $order_id, $coupon_code, 0, 1 );<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">To get the commission from the order for example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$commission = $order_data[&#039;totalcommission&#039;];<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Affiliate URL<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Get the affiliate URL for a coupon via the following function:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$coupon_code = &quot;&quot;; \/\/ Set the coupon code name here.\n$affiliate_url = wcusage_get_affiliate_url($coupon_code);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Or if you want to build your own custom URL here&#8217;s an example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$coupon_code = &quot;&quot;; \/\/ Set the coupon code name here.\n$prefix = wcusage_get_setting_value(&#039;wcusage_field_urls_prefix&#039;, &#039;coupon&#039;);\n$affiliate_url = get_home_url() . &quot;?&quot; . $prefix . &quot;=&quot; . $coupon_code;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Hook (Filter): Change the Referral URL<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Every affiliate link is built from a base URL, which comes from the &#8220;Default Referral URL Page&#8221; 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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you deliberately serve your shop from a different domain to the WordPress install &#8211; a separate front end or a headless setup, for example &#8211; use the &#8220;wcusage_default_ref_url&#8221; filter rather than the setting:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>add_filter( &#039;wcusage_default_ref_url&#039;, function( $url ) {\n    return &#039;https:\/\/shop.example.com\/&#039;;\n} );<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">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&#8217;s own domain, on the understanding that requests to it have to reach your site for referrals to be tracked.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To replace a whole link instead &#8211; a vanity or shortened link, for example &#8211; use the &#8220;wcusage_affiliate_url&#8221; filter, which runs after the coupon code has been added:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>add_filter( &#039;wcusage_affiliate_url&#039;, function( $affiliate_url, $coupon_code ) {\n    return &#039;https:\/\/go.example.com\/&#039; . rawurlencode( $coupon_code );\n}, 10, 2 );<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This one covers the affiliate dashboard, QR codes, creatives, the referral URL shortcodes and the registration emails.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Recording Referral Clicks from a Custom Front End<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">If your referral links are handled outside of WordPress &#8211; a headless or decoupled front end, for example &#8211; you can record the click yourself, so that it still reaches the affiliate&#8217;s URL statistics. Use the &#8220;wcusage_record_click()&#8221; function:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$click_id = wcusage_record_click( $coupon_code, array(\n    &#039;campaign&#039;  =&gt; &#039;&#039;,   \/\/ Campaign \/ &quot;src&quot; value.\n    &#039;page&#039;      =&gt; 0,    \/\/ Post ID of the landing page. 0 shows as the homepage.\n    &#039;referrer&#039;  =&gt; &#039;&#039;,   \/\/ A full URL or a domain. Only the domain is stored.\n    &#039;ip&#039;        =&gt; null, \/\/ Visitor IP, or an ID of up to 64 characters.\n    &#039;converted&#039; =&gt; false,\n) );<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The first parameter is a coupon code, or a coupon ID.<\/li>\n\n\n\n<li>Setting &#8216;ip&#8217; to null detects it from the current request the way the plugin&#8217;s own tracking does, which is only meaningful when the visitor&#8217;s browser is what made the request.<\/li>\n\n\n\n<li>Unlike the plugin&#8217;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.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">A &#8220;wcusage_click_recorded&#8221; action fires afterwards, passing the click ID, the coupon ID, and the details the click was recorded with.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Marking the conversion<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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 &#8211; saved against the order, for example &#8211; and hand it back with the &#8220;wcusage_conversion_click_id&#8221; filter:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>add_filter( &#039;wcusage_conversion_click_id&#039;, function( $click_id, $order_id ) {\n    return get_post_meta( $order_id, &#039;my_referral_click_id&#039;, true );\n}, 10, 2 );<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Total Sales and Commission<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Get the total sales and commission earned by a certain coupon, without a certain date range. <em>Leave $start_date empty to get all sales.<\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$orders = wcusage_wh_getOrderbyCouponCode( $coupon_code, $start_date, $end_date, &#039;&#039;, 1 );\n$total_orders = $orders[&#039;total_orders&#039;]; \/\/ Total Sales\n$total_discounts = $orders[&#039;full_discount&#039;]; \/\/ Total Discounts\n$total_commission = $orders[&#039;total_commission&#039;]; \/\/ Total Commission\n$order_count = $orders[&#039;total_count&#039;]; \/\/ Orders Count<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Unpaid Commission<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Get the total unpaid commission for an affiliate coupon.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$coupon_id = &quot;&quot;; \/\/ Set the coupon code ID here.\n$unpaid_commission = get_post_meta( $coupon_id, &#039;wcu_text_unpaid_commission&#039;, true );<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Hook (Action): New Affiliate Registration Submitted<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">When there is a new affiliate registration, it will run the &#8220;wcusage_hook_registration_new&#8221; hook that will pass the registration ID, user ID, and coupon code.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You could therefore call this hook to automatically accept the registration under certain conditions with the &#8220;wcusage_set_registration_status&#8221; function. For example this will automatically accept the registration if the users role is &#8220;example&#8221;:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>add_action( &#039;wcusage_hook_registration_new&#039;, &#039;trigger_wcusage_hook_registration_new&#039;, 10, 3 );\nfunction trigger_wcusage_hook_registration_new( $registration_id, $user_id, $coupon_code ) {\n    $user_info = get_userdata( $user_id );\n    $roles = $user_info-&gt;roles;\n\n    if ( in_array( &#039;example&#039;, $roles ) ) {\n        wcusage_set_registration_status( &#039;accepted&#039;, $registration_id, $user_id, $coupon_code, &#039;&#039;, &#039;&#039; );\n    }\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Hook (Action): New Affiliate Accepted<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">When a new affiliate is accepted and added to your program, it will run the &#8220;wcusage_hook_affiliate_register_accepted&#8221; that will pass the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>$id &#8211; Registration ID.<\/li>\n\n\n\n<li>$userid &#8211; User ID.<\/li>\n\n\n\n<li>$coupon_code &#8211; Coupon Code.<\/li>\n\n\n\n<li>$message &#8211; The registration acceptance message.<\/li>\n\n\n\n<li>$status &#8211; The status of their registration.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>add_action( &#039;wcusage_hook_affiliate_register_accepted&#039;, &#039;trigger_wcusage_hook_registration_accepted&#039;, 10, 5 );\nfunction trigger_wcusage_hook_registration_accepted( $id, $userid, $coupon_code, $message, $status ) {\n\/\/ Your code here.\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Hook (Action): Trigger a Performance Bonus from a Custom Event (PRO)<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Performance Bonuses can be triggered from your own theme or plugin code. When creating a bonus, set the trigger type to &#8220;Developers: Custom Event \/ Action&#8221; and enter a Custom Event Key. Then fire the &#8220;wcusage_custom_event&#8221; action with that same key whenever the event happens:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>do_action( &#039;wcusage_custom_event&#039;, &#039;your_event_key&#039;, $user_id );\n\/\/ Or target a specific affiliate coupon:\ndo_action( &#039;wcusage_custom_event&#039;, &#039;your_event_key&#039;, $user_id, array(), &#039;COUPONCODE&#039; );<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>&#8216;your_event_key&#8217; must match the Custom Event Key entered on the bonus.<\/li>\n\n\n\n<li>$user_id is the WordPress user ID of the affiliate to reward.<\/li>\n\n\n\n<li>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.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">See <a href=\"https:\/\/couponaffiliates.com\/docs\/pro-bonuses\/\">(PRO) Performance Bonuses<\/a> for how to set up the bonus itself.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Legacy v1 API<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The original three endpoints under <code>woo-coupon-usage\/v1<\/code> are still registered for existing integrations. They require a WordPress <strong>administrator<\/strong> role. They are not the current API. New integrations should use <code>wcusage\/v2<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>GET<\/strong> <code>\/wp-json\/woo-coupon-usage\/v1\/coupon-info<\/code> with <code>coupon_id<\/code>. Returns <code>coupon_name<\/code>, <code>unpaid_commission<\/code>, and <code>pending_payouts<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>GET<\/strong> <code>\/wp-json\/woo-coupon-usage\/v1\/users-coupons<\/code> with <code>user<\/code> (the login name). Returns coupon IDs assigned to that user, with unpaid commission.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>POST<\/strong> <code>\/wp-json\/woo-coupon-usage\/v1\/request-payout<\/code> with <code>coupon_id<\/code> and <code>user<\/code> (login). Returns <code>1<\/code> on success or <code>0<\/code> on failure. PRO.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Need help with a custom function? Feel free to contact us and we may be able to point you in the right direction.<\/p>\n","protected":false},"author":1,"menu_order":0,"template":"","meta":{"_kad_blocks_custom_css":"","_kad_blocks_head_custom_js":"","_kad_blocks_body_custom_js":"","_kad_blocks_footer_custom_js":"","_links_to":"","_links_to_target":""},"aisa_doc_category":[166],"class_list":["post-25758","aisa_doc","type-aisa_doc","status-publish","hentry","aisa_doc_category-developers","wpbf-post"],"taxonomy_info":{"aisa_doc_category":[{"value":166,"label":"Developers"}]},"featured_image_src_large":[],"author_info":{"display_name":"Elliot Sowersby","author_link":"#"},"comment_info":"","_links":{"self":[{"href":"https:\/\/couponaffiliates.com\/fr\/wp-json\/wp\/v2\/aisa_doc\/25758","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/couponaffiliates.com\/fr\/wp-json\/wp\/v2\/aisa_doc"}],"about":[{"href":"https:\/\/couponaffiliates.com\/fr\/wp-json\/wp\/v2\/types\/aisa_doc"}],"author":[{"embeddable":true,"href":"https:\/\/couponaffiliates.com\/fr\/wp-json\/wp\/v2\/users\/1"}],"version-history":[{"count":1,"href":"https:\/\/couponaffiliates.com\/fr\/wp-json\/wp\/v2\/aisa_doc\/25758\/revisions"}],"predecessor-version":[{"id":25917,"href":"https:\/\/couponaffiliates.com\/fr\/wp-json\/wp\/v2\/aisa_doc\/25758\/revisions\/25917"}],"wp:attachment":[{"href":"https:\/\/couponaffiliates.com\/fr\/wp-json\/wp\/v2\/media?parent=25758"}],"wp:term":[{"taxonomy":"aisa_doc_category","embeddable":true,"href":"https:\/\/couponaffiliates.com\/fr\/wp-json\/wp\/v2\/aisa_doc_category?post=25758"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}