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.
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().
File: includes/classes/WPDAI_Order_Calculator.php
Method: WPDAI_Order_Calculator::calculate_product_data()
Line: ~929
| 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 |
Type: array
Description: Array of line item meta key strings to check for per-unit COGS values
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_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;
}
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;
}
_wpd_ai_product_cogs on the line item is not numeric$item->get_meta()10 and 3 accepted arguments when hooking this filter