## Filter: wpd_ai_cost_price_per_unit

Filter the cost price per unit for a product before it is used in profit calculations.

### Description

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.

#### Location

File: `includes/wpd-functions.php`

Function: `wpdai_get_cost_price_per_unit( $product_id )`

### Parameters

Parameter | Type | Description
--- | --- | ---
$cost_price_per_unit | float | The calculated cost price per unit (before filter)
$product_id | int | WooCommerce product or variation ID

#### Return

Type: float

The cost price per unit to use (cast to float by the plugin).

### Example Usage

#### Override cost from external system

    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;
    }

#### Apply a global markup to cost

    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 );
    }

### Related Filters

- wpd_ai_default_product_cost_price_meta_keys – Default product meta keys for COGS fallback
- _wpd_ai_line_item_cost_per_unit_meta_keys – Line item meta keys for per-unit COGS
- _wpd_ai_override_total_product_cost – Override total order product COGS
- wpd_ai_default_product_cost_price_meta_keys – Default product meta keys for COGS fallback
- wpd_ai_custom_product_cost_default_value – Custom product cost default
- wpd_ai_calculate_cost_profit_by_order – Full profit calculation