## Filter: _wpd_ai_line_item_cost_per_unit_meta_keys

Filter the list of order line item meta keys that Alpha Insights checks for per-unit cost of goods sold (COGS) when calculating order profit.

### Description

During order profit calculation, Alpha Insights first looks for a saved per-unit COGS value on each line item in the `_wpd_ai_product_cogs` meta key. If that value is not numeric, this filter lets you supply additional line item meta keys to check—such as third-party COGS plugins or custom integrations.

Each key in the returned array is read from the current line item. If a meta value is numeric, it is used as the per-unit COGS for that line item. When multiple keys return numeric values, the last numeric value in the array wins. If no numeric value is found, Alpha Insights falls back to the product's configured cost via `wpdai_get_cost_price_by_product_id()`.

#### Location

File: `includes/classes/WPDAI_Order_Calculator.php`

Method: `WPDAI_Order_Calculator::calculate_product_data()`

Line: ~929

### Parameters

Parameter | Type | Description
--- | --- | ---
$meta_keys | array | Line item meta keys to check for per-unit COGS (default: empty array)
$order | WC_Order | The WooCommerce order being calculated
$item | WC_Order_Item_Product | The current order line item

#### Return

Type: array

Description: Array of line item meta key strings to check for per-unit COGS values

### Example Usage

#### Support WooCommerce native COGS line item meta

WooCommerce's native Cost of Goods Sold feature stores per-unit cost on line items as `_cogs_value`. Add it when Alpha Insights has not already saved `_wpd_ai_product_cogs` on the line item:

    add_filter( '_wpd_ai_line_item_cost_per_unit_meta_keys', 'wpd_ai_add_wc_native_cogs_meta_key', 10, 3 );
    function wpd_ai_add_wc_native_cogs_meta_key( $meta_keys, $order, $item ) {
        $meta_keys[] = '_cogs_value';
        return $meta_keys;
    }

#### Add a custom integration meta key

    add_filter( '_wpd_ai_line_item_cost_per_unit_meta_keys', 'wpd_ai_add_erp_line_item_cost_meta', 10, 3 );
    function wpd_ai_add_erp_line_item_cost_meta( $meta_keys, $order, $item ) {
        $meta_keys[] = '_my_erp_unit_cost';
        return $meta_keys;
    }

#### Conditional meta keys per order or line item

    add_filter( '_wpd_ai_line_item_cost_per_unit_meta_keys', 'wpd_ai_conditional_line_item_cost_meta', 10, 3 );
    function wpd_ai_conditional_line_item_cost_meta( $meta_keys, $order, $item ) {
        // Only check ERP cost meta for wholesale orders
        if ( $order->get_meta( '_is_wholesale_order' ) === 'yes' ) {
            $meta_keys[] = '_wholesale_unit_cost';
        }
        return $meta_keys;
    }

### Important Notes

- This filter only runs when `_wpd_ai_product_cogs` on the line item is not numeric
- Values are treated as per-unit COGS, not line item totals
- Meta keys are case-sensitive—use the exact key name stored on the line item
- Order meta keys in the array are not supported; only line item meta is read via `$item->get_meta()`
- Use priority `10` and `3` accepted arguments when hooking this filter

### Related Filters

- wpd_ai_default_product_cost_price_meta_keys – Default product meta keys for COGS fallback
- wpd_ai_cost_price_per_unit – Override per-unit cost at the product level
- wpd_ai_default_product_cost_price_meta_keys – Default product meta keys for COGS fallback
- _wpd_ai_override_total_product_cost – Override total order product COGS
- wpd_ai_calculate_cost_profit_by_order – Full profit calculation