Filter: _wpd_ai_override_total_product_cost

Override the total product cost of goods sold (COGS) for an order after line items have been calculated.

Description

Alpha Insights normally totals product COGS by summing each line item's per-unit cost multiplied by quantity. This filter runs near the end of product calculations and lets you replace that summed COGS value with a custom total—useful for ERP integrations, manual adjustments, or systems that store order-level product cost rather than per-line-item cost.

The filtered value replaces total_product_cogs only. Custom product costs (packaging, fulfillment, etc.) are added separately to produce total_product_cost.

Calculation precedence

  1. Sum line item COGS (from _wpd_ai_product_cogs, fallback meta keys, or product cost settings)
  2. Order meta _wpd_ai_total_product_cost, if numeric, overrides the summed value
  3. This filter runs last and can override the value from steps 1–2

Location

File: includes/classes/WPDAI_Order_Calculator.php

Method: WPDAI_Order_Calculator::calculate_product_data()

Line: ~1150

Parameters

Parameter Type Description
$total_product_cogs float The current total product COGS for the order (sum of line items, or overridden by order meta)
$order WC_Order The WooCommerce order being calculated

Return

Type: float

Description: The total product COGS to use for the order. Must be numeric to take effect.

Example Usage

Override from an external ERP

add_filter( '_wpd_ai_override_total_product_cost', 'wpd_ai_erp_product_cost_override', 10, 2 );
function wpd_ai_erp_product_cost_override( $total_product_cogs, $order ) {
    $erp_cost = get_post_meta( $order->get_id(), '_erp_total_product_cost', true );
    if ( is_numeric( $erp_cost ) && $erp_cost >= 0 ) {
        return (float) $erp_cost;
    }
    return $total_product_cogs;
}

Apply a fixed adjustment to calculated COGS

add_filter( '_wpd_ai_override_total_product_cost', 'wpd_ai_add_handling_buffer_to_cogs', 10, 2 );
function wpd_ai_add_handling_buffer_to_cogs( $total_product_cogs, $order ) {
    // Add a flat $2 handling buffer to product COGS for all orders
    return round( $total_product_cogs + 2.00, 2 );
}

Override only for specific order types

add_filter( '_wpd_ai_override_total_product_cost', 'wpd_ai_wholesale_cogs_override', 10, 2 );
function wpd_ai_wholesale_cogs_override( $total_product_cogs, $order ) {
    if ( $order->get_meta( '_is_wholesale_order' ) !== 'yes' ) {
        return $total_product_cogs;
    }

    $wholesale_cogs = $order->get_meta( '_wholesale_total_product_cost' );
    if ( is_numeric( $wholesale_cogs ) ) {
        return (float) $wholesale_cogs;
    }

    return $total_product_cogs;
}

Important Notes

Related Filters