Home / eCommerce / Add Customer Name Column to Affiliate Portal Referrals Tab
Duplicate Snippet

Embed Snippet on Your Site

Add Customer Name Column to Affiliate Portal Referrals Tab

Adds a Customer Name column to the main Referrals table in the Affiliate Portal. Pulls the customer name directly from WooCommerce or Easy Digital Downloads using the order ID stored on each referral. No additional addons required.

Code Preview
php
<?php
<?php
add_action( 'init', function() {
    if ( ! class_exists( '\AffiliateWP_Affiliate_Portal\Core\Controls_Registry' ) ) {
        return;
    }
    $registry = \AffiliateWP_Affiliate_Portal\Core\Controls_Registry::instance();
    $column = new \AffiliateWP_Affiliate_Portal\Core\Components\Controls\Table_Column_Control( array(
        'id'     => 'custom_customer_name_column',
        'parent' => 'referrals-table',
        'args'   => array(
            'title'           => __( 'Customer Name', 'affiliatewp-affiliate-portal' ),
            'priority'        => 30,
            'render_callback' => function( \AffWP\Referral $row, $table_control_id ) {
                $customer_name = '-';
                switch ( $row->context ) {
                    case 'woocommerce':
                        if ( function_exists( 'wc_get_order' ) ) {
                            $order = wc_get_order( $row->reference );
                            if ( $order ) {
                                $name = trim( $order->get_billing_first_name() . ' ' . $order->get_billing_last_name() );
                                if ( ! empty( $name ) ) {
                                    $customer_name = $name;
                                }
                            }
                        }
                        break;
                    case 'edd':
                        if ( class_exists( 'EDD_Payment' ) ) {
                            $payment = new EDD_Payment( $row->reference );
                            $name    = trim( $payment->first_name . ' ' . $payment->last_name );
                            if ( ! empty( $name ) ) {
                                $customer_name = $name;
                            }
                        }
                        break;
                }
                return \AffiliateWP_Affiliate_Portal\Core\Components\Controls\Text_Control::create(
                    "{$table_control_id}_customer_name",
                    $customer_name
                );
            },
        ),
    ) );
    $registry->add_control( $column );
}, 20 );

Comments

Add a Comment