Filter: wpd_ai_custom_order_cost_default_value

Filter the default value for custom order-level costs before they are used in profit calculations.

Description

This filter allows you to programmatically set or modify custom order costs such as insurance, handling fees, or any other order-level expenses. Unlike product-level costs, these apply once per order regardless of the number of items.

Location

File: includes/classes/WPDAI_Order_Calculator.php

Method: WPDAI_Order_Calculator::get_custom_order_costs()

Line: ~725

Parameters

Parameter Type Description
$custom_cost_value float The default cost value for this custom cost type
$cost_slug string The unique slug identifier for this custom cost
$order WC_Order The WooCommerce order object

Return

Type: float

Description: Modified custom cost value

Example Usage

Add Insurance Cost Based on Order Value

add_filter( 'wpd_ai_custom_order_cost_default_value', 'dynamic_insurance_cost', 10, 3 );
function dynamic_insurance_cost( $cost, $cost_slug, $order )
{
    // Only apply to insurance cost
    if ( $cost_slug !== 'insurance' )
    {
        return $cost;
    }
    $order_total = $order->get_total();
    // Tiered insurance rates
    if ( $order_total > 500 )
    {
        return 25.00;
        // High-value orders
    }
    elseif ( $order_total > 200 )
    {
        return 12.00;
        // Medium-value orders
    }
    elseif ( $order_total > 100 )
    {
        return 5.00;
        // Standard orders
    }
    return 0;
    // No insurance for small orders
}

International Handling Fee

add_filter( 'wpd_ai_custom_order_cost_default_value', 'international_handling', 10, 3 );
function international_handling( $cost, $cost_slug, $order )
{
    if ( $cost_slug !== 'international_handling' )
    {
        return $cost;
    }
    // Check if international order
    $shipping_country = $order->get_shipping_country();
    $store_country = WC()->countries->get_base_country();
    if ( $shipping_country !== $store_country )
    {
        // Add customs paperwork handling
        return 15.00;
    }
    return 0;
    // No fee for domestic orders
}

Wholesale vs Retail Fulfillment

add_filter( 'wpd_ai_custom_order_cost_default_value', 'wholesale_fulfillment', 10, 3 );
function wholesale_fulfillment( $cost, $cost_slug, $order )
{
    if ( $cost_slug !== 'fulfillment_fee' )
    {
        return $cost;
    }
    // Check customer role
    $customer_id = $order->get_customer_id();
    if ( $customer_id )
    {
        $user = get_userdata( $customer_id );
        if ( in_array( 'wholesale_customer', $user->roles ) )
        {
            // Lower fulfillment fee for wholesale
            return 8.00;
        }
    }
    // Standard retail fulfillment fee
    return 15.00;
}

Fragile Item Handling

add_filter( 'wpd_ai_custom_order_cost_default_value', 'fragile_handling_fee', 10, 3 );
function fragile_handling_fee( $cost, $cost_slug, $order )
{
    if ( $cost_slug !== 'special_handling' )
    {
        return $cost;
    }
    $has_fragile = false;
    // Check if order contains fragile products
    foreach ( $order->get_items() as $item )
    {
        $product = $item->get_product();
        if ( $product && has_term( 'fragile', 'product_cat', $product->get_id() ) )
        {
            $has_fragile = true;
            break;
        }
    }
    return $has_fragile ? 10.00 : 0;
}

Seasonal Peak Fee

add_filter( 'wpd_ai_custom_order_cost_default_value', 'seasonal_peak_fee', 10, 3 );
function seasonal_peak_fee( $cost, $cost_slug, $order )
{
    if ( $cost_slug !== 'peak_season_fee' )
    {
        return $cost;
    }
    $order_date = $order->get_date_created();
    $month = (int) $order_date->format('n');
    // November and December (holiday season)
    if ( in_array( $month, [11, 12] ) )
    {
        return 7.50;
        // Peak season surcharge
    }
    return 0;
}

Integration with ERP System

add_filter( 'wpd_ai_custom_order_cost_default_value', 'erp_custom_costs', 10, 3 );
function erp_custom_costs( $cost, $cost_slug, $order )
{
    // Fetch actual costs from ERP system
    $order_number = $order->get_order_number();
    $erp_costs = my_erp_get_order_costs( $order_number );
    if ( $erp_costs && isset( $erp_costs[$cost_slug] ) )
    {
        return (float) $erp_costs[$cost_slug];
    }
    return $cost;
}

Best Practices

Important Notes

How Custom Order Costs Work

Custom order costs are configured in:
Alpha Insights → Settings → Custom Costs → Order-Level Costs

Each custom cost needs:

Debugging

add_filter( 'wpd_ai_custom_order_cost_default_value', 'debug_custom_costs', 999, 3 );
function debug_custom_costs( $cost, $cost_slug, $order )
{
    error_log( sprintf( 
        'Order #%d - Custom Cost "%s": $%s',
        $order->get_id(),
        $cost_slug,
        number_format( $cost, 2 )
    ));
    return $cost;
}

Related Filters

Related Functions