Override the total product cost of goods sold (COGS) for an order after line items have been calculated.
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.
_wpd_ai_product_cogs, fallback meta keys, or product cost settings)_wpd_ai_total_product_cost, if numeric, overrides the summed valueFile: includes/classes/WPDAI_Order_Calculator.php
Method: WPDAI_Order_Calculator::calculate_product_data()
Line: ~1150
| 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 |
Type: float
Description: The total product COGS to use for the order. Must be numeric to take effect.
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;
}
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 );
}
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;
}
total_product_cost is recalculated as total_product_cogs + total_product_custom_costs