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_keyslast 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 filterwpd_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

Related filters

Related functions