Filter: wpd_ai_payment_gateway_fee_meta_keys

Filter the list of order meta keys that Alpha Insights checks for payment gateway fees. This allows you to add support for custom payment gateways or modify which meta keys are checked.

Description

Alpha Insights automatically detects payment gateway fees by checking specific order meta keys. This filter allows you to add custom meta keys for payment gateways that aren't supported by default, or remove keys that you don't want to track.

Location

File: includes/classes/WPDAI_Order_Calculator.php

Method: WPDAI_Order_Calculator::calculate_payment_gateway_cost()

Line: ~510

Parameters

Parameter Type Description
$meta_keys array Array of order meta keys to check for payment gateway fees (default: common gateway meta keys)

Return

Type: array

Description: Modified array of meta key strings

Default Meta Keys

Alpha Insights checks these meta keys by default:

Example Usage

Add Custom Payment Gateway Meta Keys

add_filter( 'wpd_ai_payment_gateway_fee_meta_keys', 'add_custom_gateway_keys' );
function add_custom_gateway_keys( $meta_keys ) {
    // Add meta keys for custom payment gateways
    $custom_keys = array(
        '_square_fee',
        '_authorize_net_fee',
        '_braintree_fee',
        '_custom_gateway_fee'
    );
    return array_merge( $meta_keys, $custom_keys );
}

Remove Specific Gateway Keys

add_filter( 'wpd_ai_payment_gateway_fee_meta_keys', 'remove_specific_gateway_keys' );
function remove_specific_gateway_keys( $meta_keys ) {
    // Remove keys you don't want to track
    $keys_to_remove = array( 'PayPal Transaction Fee' );
    return array_diff( $meta_keys, $keys_to_remove );
}

Replace Entire List

add_filter( 'wpd_ai_payment_gateway_fee_meta_keys', 'replace_gateway_keys_list' );
function replace_gateway_keys_list( $meta_keys ) {
    // Use only your specific gateway keys
    return array(
        '_stripe_fee',
        '_square_fee',
        '_custom_gateway_fee'
    );
}

Add Gateway Keys Based on Active Plugins

add_filter( 'wpd_ai_payment_gateway_fee_meta_keys', 'add_keys_for_active_gateways' );
function add_keys_for_active_gateways( $meta_keys ) {
    // Check which payment gateways are active and add their keys
    if ( class_exists( 'WC_Square' ) ) {
        $meta_keys[] = '_square_fee';
    }
    
    if ( class_exists( 'WC_Authorize_Net' ) ) {
        $meta_keys[] = '_authorize_net_fee';
    }
    
    if ( function_exists( 'is_plugin_active' ) && is_plugin_active( 'woocommerce-braintree/woocommerce-braintree.php' ) ) {
        $meta_keys[] = '_braintree_fee';
    }
    
    return $meta_keys;
}

Best Practices

Finding Your Gateway's Meta Key

To find the meta key your payment gateway uses:

  1. Create a test order with the payment gateway
  2. Go to WooCommerce → Orders → Edit the order
  3. Scroll down to "Order Meta" section
  4. Look for keys containing "fee" or the gateway name
  5. Common patterns: _{gateway}_fee, {Gateway} Fee

Important Notes

Related Filters

Related Classes

Debugging

add_filter( 'wpd_ai_payment_gateway_fee_meta_keys', 'debug_gateway_meta_keys', 999 );
function debug_gateway_meta_keys( $meta_keys ) {
    error_log( 'Alpha Insights Payment Gateway Meta Keys: ' . implode( ', ', $meta_keys ) );
    return $meta_keys;
}

To see what meta keys exist on an order:

// Add to functions.php temporarily
add_action( 'woocommerce_admin_order_data_after_order_details', 'debug_order_meta_keys' );
function debug_order_meta_keys( $order ) {
    $all_meta = get_post_meta( $order->get_id() );
    error_log( 'Order #' . $order->get_id() . ' Meta Keys: ' . print_r( array_keys( $all_meta ), true ) );
}