You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过自定义WordPress插件程序化修改WooCommerce订单关联的AffiliateWP联盟会员

如何通过自定义WordPress插件程序化修改WooCommerce订单关联的AffiliateWP联盟会员

嘿,我刚好折腾过类似的需求!既然你没法改动AffiliateWP或WooCommerce的核心代码,只能靠自定义插件来实现,那我给你一套亲测可行的方案:

首先得摸透AffiliateWP的存储逻辑:它把订单和联盟会员的关联关系,主要存在两个关键地方——

  • WooCommerce订单的postmeta表,键名是_affiliatewp_referral_id,值对应AffiliateWP推荐记录的ID
  • AffiliateWP专属的affiliate_wp_referrals表,每条记录对应一个订单的推荐关联,包含order_id、affiliate_id、amount等核心信息

所以我们的核心操作就是同步更新这两处的数据,下面是完整的自定义插件代码:

<?php
/*
Plugin Name: 调整订单关联联盟会员
Description: 程序化修改WooCommerce订单对应的AffiliateWP联盟会员
Version: 1.0
Author: 自定义开发者
*/

// 核心函数:修改指定订单的关联联盟会员
function change_order_associated_affiliate( $order_id, $target_affiliate_id ) {
    // 权限校验:只允许管理员执行此操作
    if ( ! current_user_can( 'manage_options' ) ) {
        return new WP_Error( 'invalid_permission', '无权限执行此操作' );
    }

    // 验证订单是否真实存在
    $order = wc_get_order( $order_id );
    if ( ! $order ) {
        return new WP_Error( 'invalid_order', '指定订单不存在' );
    }

    // 验证目标联盟会员是否有效
    if ( ! affiliatewp()->affiliates->get( $target_affiliate_id ) ) {
        return new WP_Error( 'invalid_affiliate', '指定联盟会员不存在' );
    }

    global $wpdb;
    $referrals_table = $wpdb->prefix . 'affiliatewp_referrals';

    // 1. 查找订单原有的推荐记录(如果存在)
    $old_referral = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $referrals_table WHERE order_id = %d", $order_id ) );

    // 2. 若有旧记录,先清理掉(避免重复关联)
    if ( $old_referral ) {
        $wpdb->delete( $referrals_table, array( 'referral_id' => $old_referral->referral_id ) );
        delete_post_meta( $order_id, '_affiliatewp_referral_id' );
    }

    // 3. 生成新的推荐记录
    $referral_data = array(
        'affiliate_id' => $target_affiliate_id,
        'amount'       => $order->get_total(), // 用订单总金额作为推荐基数,可按需调整
        'currency'     => $order->get_currency(),
        'order_id'     => $order_id,
        'context'      => 'woocommerce',
        'status'       => 'pending', // 和AffiliateWP默认新推荐状态保持一致
        'date'         => current_time( 'mysql' ),
    );

    $wpdb->insert( $referrals_table, $referral_data );
    $new_referral_id = $wpdb->insert_id;

    // 4. 把新推荐记录绑定到订单
    if ( $new_referral_id ) {
        update_post_meta( $order_id, '_affiliatewp_referral_id', $new_referral_id );
        return array( 'status' => 'success', 'new_referral_id' => $new_referral_id );
    } else {
        return new WP_Error( 'create_referral_failed', '创建新推荐记录失败' );
    }
}

// 示例调用(实际使用时替换成你的订单ID和目标联盟会员ID)
// change_order_associated_affiliate( 123, 45 );

一些实用提醒:

  • 这个插件完全独立,不会碰任何核心代码,你只需要把它存成change-order-affiliate.php,上传到wp-content/plugins/目录启用就行
  • 推荐记录的status我设为了pending,你可以根据需求改成approved直接生效,或者其他AffiliateWP支持的状态
  • 如果需要批量修改订单,可以写个循环调用这个函数,但记得加适当延迟,避免给数据库造成过大压力
  • 测试时建议先在 staging 环境操作,稳妥点总没错

备注:内容来源于stack exchange,提问作者Mitchell Bennett

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.13 16:33:00