## Filter: wpd_ai_default_product_cost_price_meta_keys

Configure the product post meta keys Alpha Insights checks when resolving a default per-unit cost price—after Alpha Insights' own `_wpd_ai_product_cost` field is empty.

### Description

When a product or variation has no cost saved in Alpha Insights' `_wpd_ai_product_cost` meta field, Alpha Insights loops through the meta keys returned by this filter on that product/variation ID. The first numeric value found is used as the per-unit COGS.

By default, Alpha Insights includes WooCommerce native COGS and several popular third-party COGS plugins so cost data is picked up automatically without extra configuration.

#### Location

File: `includes/wpd-functions.php`

Function: `wpdai_get_default_cost_price_by_product_id( $product_id )`

Line: ~358

### Parameters

Parameter | Type | Description
--- | --- | ---
$meta_keys | array | Product post meta keys to check for per-unit COGS

#### Return

Type: array

Array of product post meta key strings. Each key is read via `get_post_meta( $product_id, $key, true )` on the product or variation being resolved.

### Default meta keys

Alpha Insights passes this array as the filter default (first match wins):

Meta key | Source
--- | ---
`_cogs_total_value` | WooCommerce native COGS (WooCommerce 10.0+)
`_wc_cog_cost` | WooCommerce Cost of Goods (official extension)
`_alg_wc_cog_cost` | Cost of Goods for WooCommerce by WP Factory
`_purchase_price` | ATUM Stock Management for WooCommerce (Stock Central)

To disable a built-in source, remove its key from the array. To add your own integration, append additional keys—the earlier a key appears in the array, the higher its priority.

### Cost price resolution hierarchy

Product cost is resolved at two levels: order line items (per order) and products (catalog fallback). This filter affects the product fallback path only.

#### Order line item COGS (per line item in an order)

1. `_wpd_ai_product_cogs` on the line item (saved when the order is calculated)
2. Line item meta keys from _wpd_ai_line_item_cost_per_unit_meta_keys — last numeric value wins
3. Product cost via `wpdai_get_cost_price_by_product_id()` (see below)

#### Product cost (`wpdai_get_cost_price_by_product_id()`)

1. Request object cache (`_wpd_ai_product_cost_price`) — skips all lookups if this function already ran for the product in the current request
2. `_wpd_ai_product_cost` on the product or variation (Alpha Insights product cost field)
3. Default cost resolution via `wpdai_get_default_cost_price_by_product_id()` (see next section)
4. Bundle parent products (WPC Bundles / WooCommerce Product Bundles) — cost set to `0`; child items carry cost
5. wpd_ai_cost_price_per_unit — final override filter on the resolved value

#### Default product cost (`wpdai_get_default_cost_price_by_product_id()`) — where this filter runs

Only evaluated when `_wpd_ai_product_cost` is empty on the product or variation.

1. This filter — `wpd_ai_default_product_cost_price_meta_keys` on the product/variation — first numeric value wins (defaults include Woo native and common plugin keys; see table above)
2. If the product is a variation and still no cost: parent `_wpd_ai_product_cost`
3. If the product is a variation and still no cost: same default meta keys from this filter, checked on the parent variable product
4. General settings default — percentage of regular price (e.g. 30% of RRP) from Settings → Cost Defaults
5. `0` if nothing else applies

#### Precedence summary

Source | Takes precedence over | Notes
--- | --- | ---
Alpha Insights `_wpd_ai_product_cost` | This filter, parent meta, general settings | Checked before `wpdai_get_default_cost_price_by_product_id()` runs
This filter (default meta keys) | Parent meta, general settings | First numeric key in the array wins; runs on the variation/product ID
Parent `_wpd_ai_product_cost` and default meta keys | General settings | Variation fallback only; parent is checked with the same meta key list from this filter
`wpd_ai_cost_price_per_unit` | All resolved product cost values | Runs after the full product resolution chain
Line item `_wpd_ai_product_cogs` | Product cost for that line item | Order-level; does not use this filter directly

### Example usage

#### Add a custom ERP meta key

    add_filter( 'wpd_ai_default_product_cost_price_meta_keys', 'wpd_ai_add_erp_cost_meta_key' );
    function wpd_ai_add_erp_cost_meta_key( $meta_keys ) {
        // Check ERP cost before built-in plugin keys
        array_unshift( $meta_keys, '_my_erp_unit_cost' );
        return $meta_keys;
    }

#### Append a fallback key after built-in defaults

    add_filter( 'wpd_ai_default_product_cost_price_meta_keys', 'wpd_ai_append_supplier_cost_meta' );
    function wpd_ai_append_supplier_cost_meta( $meta_keys ) {
        $meta_keys[] = '_supplier_unit_cost';
        return $meta_keys;
    }

#### Disable WooCommerce native COGS in the default list

    add_filter( 'wpd_ai_default_product_cost_price_meta_keys', 'wpd_ai_remove_native_cogs_meta_key' );
    function wpd_ai_remove_native_cogs_meta_key( $meta_keys ) {
        return array_values( array_diff( $meta_keys, array( '_cogs_total_value' ) ) );
    }

#### Replace the entire default list

    add_filter( 'wpd_ai_default_product_cost_price_meta_keys', 'wpd_ai_custom_only_cost_meta_keys' );
    function wpd_ai_custom_only_cost_meta_keys( $meta_keys ) {
        return array( '_my_custom_unit_cost' );
    }

### Important notes

- Values must be numeric (or numeric strings). Non-numeric or empty meta is skipped
- Values are treated as per-unit COGS, not line totals
- Meta keys are case-sensitive — use the exact key stored on the product
- Unlike _wpd_ai_line_item_cost_per_unit_meta_keys, the first numeric match wins (not the last)
- On variations, this filter checks the variation ID first; if no match, the parent variable product is checked for `_wpd_ai_product_cost` then the same default meta key list
- For order-specific overrides, use line item meta via `_wpd_ai_line_item_cost_per_unit_meta_keys` or `wpd_ai_cost_price_per_unit`
- For overriding total product COGS on an entire order, see _wpd_ai_override_total_product_cost

### Related filters

- wpd_ai_cost_price_per_unit – Final per-unit cost override at the product level
- _wpd_ai_line_item_cost_per_unit_meta_keys – Line item meta keys for per-unit COGS on orders
- _wpd_ai_override_total_product_cost – Override total order product COGS
- wpd_ai_calculate_cost_profit_by_order – Full profit calculation

### Related functions

- `wpdai_get_cost_price_by_product_id( $product_id )` – Primary product cost resolver
- `wpdai_get_default_cost_price_by_product_id( $product_id )` – Default/fallback cost resolver (runs this filter)
- `wpdai_get_default_cost_price_from_general_settings( $product_id )` – Percent-of-RRP fallback from settings