Filter the cost price per unit for a product before it is used in profit calculations.
Alpha Insights calculates product cost from various sources (WooCommerce native COGS, product meta, default cost settings). This filter runs after that calculation so you can override or adjust the cost price per unit for a specific product. The value is cached per product for the request.
File: includes/wpd-functions.php
Function: wpdai_get_cost_price_per_unit( $product_id )
| Parameter | Type | Description |
|---|---|---|
| $cost_price_per_unit | float | The calculated cost price per unit (before filter) |
| $product_id | int | WooCommerce product or variation ID |
Type: float
The cost price per unit to use (cast to float by the plugin).
add_filter( 'wpd_ai_cost_price_per_unit', 'my_custom_cost_from_erp', 10, 2 );
function my_custom_cost_from_erp( $cost_price_per_unit, $product_id ) {
$erp_cost = get_post_meta( $product_id, '_my_erp_cost', true );
if ( is_numeric( $erp_cost ) && $erp_cost >= 0 ) {
return (float) $erp_cost;
}
return $cost_price_per_unit;
}
add_filter( 'wpd_ai_cost_price_per_unit', 'add_safety_margin_to_cost', 10, 2 );
function add_safety_margin_to_cost( $cost_price_per_unit, $product_id ) {
// Add 5% to cost for safety margin in profit reports
return round( $cost_price_per_unit * 1.05, 4 );
}