Desarrolladores: Fragmentos de código y recursos
Updated septiembre 26, 2023
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.
Tablas de la base de datos
El plugin tiene sus propias tablas de base de datos de las que puedes obtener ciertos datos:
wcusage_activity - Registro de actividad.
wcusage_api_keys – REST API keys (hashed), with their scopes, status and last-used date.
wcusage_campaigns - Campañas de URL de referencia.
wcusage_clicks - Registro de clics en URL de referencia.
wcusage_directlinks - Dominios de seguimiento de enlaces directos.
wcusage_mlainvites - Invitaciones para afiliados multinivel.
wcusage_payouts - Pago de comisiones
wcusage_register - Aplicaciones de registro de afiliados.
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.
Pedidos de afiliados
Obtiene todos los pedidos de afiliados de un cupón, sin un rango de datos determinado. Esto devuelve un array de datos que incluye los cálculos totales de todos los pedidos, y todos los pedidos individuales.
$orders = wcusage_wh_getOrderbyCouponCode( $coupon_code, $start_date, $end_date, '', 1 );
Datos del pedido
Puede obtener un array de datos relacionados con afiliados para un determinado pedido a través de esta función. Esto devuelve una matriz de datos incluyendo las ganancias de comisión.
$order_data = wcusage_calculate_order_data( $order_id, $coupon_code, 0, 1 );
Para obtener la comisión del pedido, por ejemplo:
$commission = $order_data['totalcommission'];
URL de afiliado
Obtenga la URL de afiliación de un cupón mediante la siguiente función:
$coupon_code = ""; // Set the coupon code name here.
$affiliate_url = wcusage_get_affiliate_url($coupon_code);
O si quieres construir tu propia URL personalizada aquí tienes un ejemplo:
$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;
Total ventas y comisiones
Obtenga el total de ventas y comisiones obtenidas por un cupón determinado, sin un intervalo de fechas determinado. 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
Comisión no pagada
Obtén la comisión total no pagada por un cupón de afiliado.
$coupon_id = ""; // Set the coupon code ID here.
$unpaid_commission = get_post_meta( $coupon_id, 'wcu_text_unpaid_commission', true );
Gancho (Acción): Se presenta el registro de nuevos afiliados
Cuando haya un nuevo registro de afiliado, se ejecutará el hook "wcusage_hook_registration_new" que pasará el ID de registro, el ID de usuario y el código de cupón.
Por lo tanto, podría llamar a este hook para aceptar automáticamente el registro bajo ciertas condiciones con la función "wcusage_set_registration_status". Por ejemplo, esto aceptará automáticamente el registro si el rol del usuario es "ejemplo":
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, '', '' );
}
}
Gancho (Acción): Nuevo afiliado aceptado
Cuando un nuevo afiliado es aceptado y añadido a su programa, se ejecutará el "wcusage_hook_affiliate_register_accepted" que pasará lo siguiente:
- $id - ID de registro.
- $userid - ID de usuario.
- $coupon_code - Código del cupón.
- $message - El mensaje de aceptación del registro.
- $status - El estado de su registro.
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) Primas de rendimiento 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 cupón_id. Returns nombre_cupón, comisión_no_pagaday pagos_pendientes.
GET /wp-json/woo-coupon-usage/v1/users-coupons with usuario (the login name). Returns coupon IDs assigned to that user, with unpaid commission.
POST /wp-json/woo-coupon-usage/v1/request-payout with cupón_id y usuario (login). Returns 1 on success or 0 on failure. PRO.
¿Necesita ayuda con una función personalizada? Póngase en contacto con nosotros y le indicaremos cómo hacerlo.