{"id":25758,"date":"2023-09-26T15:04:16","date_gmt":"2023-09-26T15:04:16","guid":{"rendered":"https:\/\/couponaffiliates.com\/docs\/developers\/"},"modified":"2023-09-26T15:04:16","modified_gmt":"2023-09-26T15:04:16","slug":"desarrolladores","status":"publish","type":"aisa_doc","link":"https:\/\/couponaffiliates.com\/es\/docs\/developers\/","title":{"rendered":"Desarrolladores: Fragmentos de c\u00f3digo y recursos"},"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\">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\/es\/wp-json\/wp\/v2\/aisa_doc\/25758","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/couponaffiliates.com\/es\/wp-json\/wp\/v2\/aisa_doc"}],"about":[{"href":"https:\/\/couponaffiliates.com\/es\/wp-json\/wp\/v2\/types\/aisa_doc"}],"author":[{"embeddable":true,"href":"https:\/\/couponaffiliates.com\/es\/wp-json\/wp\/v2\/users\/1"}],"version-history":[{"count":0,"href":"https:\/\/couponaffiliates.com\/es\/wp-json\/wp\/v2\/aisa_doc\/25758\/revisions"}],"wp:attachment":[{"href":"https:\/\/couponaffiliates.com\/es\/wp-json\/wp\/v2\/media?parent=25758"}],"wp:term":[{"taxonomy":"aisa_doc_category","embeddable":true,"href":"https:\/\/couponaffiliates.com\/es\/wp-json\/wp\/v2\/aisa_doc_category?post=25758"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}