Function: wpd_get_cost_price_by_product_id
Retrieve the cost price (COGS) for a product by its ID. This is the main function for accessing product cost data.
Description
Returns the cost of goods sold (COGS) for a specific product. Checks cache first for performance, retrieves from database if needed, and applies the wpd_ai_cost_price_per_unit filter before returning.
Location
File:includes/wpd-functions.php
Line:~193
Function Signature
wpd_get_cost_price_by_product_id( $product_id )
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
| $product_id | int | Yes | The WooCommerce product ID |
Return Value
Type: float
Description: The cost price per unit. Returns 0.00 if no cost set or product doesn’t exist.
Example Usage
Basic Usage
// Get cost for product ID 123
$cost = wpd_get_cost_price_by_product_id( 123 );
echo 'Product cost: $' . number_format( $cost, 2 );
Calculate Profit for Product Sale
$product_id = 456;
$sell_price = 50.00;
$quantity = 2;
$cost_per_unit = wpd_get_cost_price_by_product_id( $product_id );
$total_cost = $cost_per_unit * $quantity;
$total_revenue = $sell_price * $quantity;
$profit = $total_revenue - $total_cost;
echo sprintf( 'Selling %d units: $%s revenue, $%s cost, $%s profit',
$quantity,
number_format( $total_revenue, 2 ),
number_format( $total_cost, 2 ),
number_format( $profit, 2 )
);
Display Cost in Custom Location
// Add cost to product admin columns
add_filter( 'manage_edit-product_columns', 'add_cost_column' );
function add_cost_column( $columns ) {
$columns['product_cost'] = 'Cost';
return $columns;
}
add_action( 'manage_product_posts_custom_column', 'display_cost_column', 10, 2 );
function display_cost_column( $column, $post_id ) {
if ( $column === 'product_cost' ) {
$cost = wpd_get_cost_price_by_product_id( $post_id );
echo wc_price( $cost );
}
}
Check If Product Has Cost Set
$product_id = 789;
$cost = wpd_get_cost_price_by_product_id( $product_id );
if ( $cost > 0 ) {
echo 'Cost is set: $' . number_format( $cost, 2 );
} else {
echo 'No cost set for this product';
}
Bulk Cost Retrieval
// Get costs for multiple products
$product_ids = array( 100, 101, 102, 103 );
$costs = array();
foreach ( $product_ids as $product_id ) {
$costs[$product_id] = wpd_get_cost_price_by_product_id( $product_id );
}
print_r( $costs );
// Output: Array ( [100] => 25.00 [101] => 18.50 [102] => 32.00 [103] => 0.00 )
Calculate Margin on Product Page
add_action( 'woocommerce_single_product_summary', 'display_product_margin', 25 );
function display_product_margin() {
global $product;
$product_id = $product->get_id();
$cost = wpd_get_cost_price_by_product_id( $product_id );
$price = $product->get_price();
if ( $cost > 0 && $price > 0 ) {
$profit = $price - $cost;
$margin = ( $profit / $price ) * 100;
echo '';
echo 'Profit Margin: ' . number_format( $margin, 1 ) . '%';
echo ' (Cost: ' . wc_price( $cost ) . ', Profit: ' . wc_price( $profit ) . ')';
echo '';
}
}
Integration with External System
// Sync cost to external inventory system
add_action( 'save_post_product', 'sync_cost_to_erp', 20 );
function sync_cost_to_erp( $product_id ) {
$product = wc_get_product( $product_id );
if ( ! $product ) {
return;
}
$cost = wpd_get_cost_price_by_product_id( $product_id );
$sku = $product->get_sku();
if ( $cost > 0 && $sku ) {
// Send to ERP API
my_erp_api_update_product_cost( $sku, $cost );
}
}
Performance Notes
Caching
Function uses WordPress object cache:
- Cache key:
_wpd_ai_product_cost_price - Cache group: Product ID
- Results cached until product updated
Optimization Tips
- Results are cached – safe to call multiple times for same product
- For bulk operations, costs cached after first retrieval
- Cache automatically cleared when product cost updated
Special Cases
Variable Products
For variable products (with variations):
// Parent product - returns average or default
$parent_cost = wpd_get_cost_price_by_product_id( $parent_id );
// Specific variation - returns variation-specific cost
$variation_cost = wpd_get_cost_price_by_product_id( $variation_id );
// Each variation can have different cost
Products Without Cost
// Returns 0.00 if:
// - Cost not set in product
// - Product doesn't exist
// - Product ID invalid
$cost = wpd_get_cost_price_by_product_id( 999999 );
// Returns: 0.00
Bundled Products
For product bundles:
- Returns cost of the bundle product itself
- Does NOT automatically sum child product costs
- Set bundle cost manually to sum of components
Filtering Cost Value
Cost can be filtered using wpd_ai_cost_price_per_unit:
add_filter( 'wpd_ai_cost_price_per_unit', 'modify_retrieved_cost', 10, 2 ); function modify_retrieved_cost( $cost, $product_id ) { // Your custom logic return $cost; }
See wpd_ai_cost_price_per_unit filter documentation
Debugging
// Debug cost retrieval $product_id = 123; $cost = wpd_get_cost_price_by_product_id( $product_id ); error_log( sprintf( 'Product #%d cost: $%s (Type: %s)', $product_id, number_format( $cost, 2 ), gettype( $cost ) ));
Related Functions
wpd_get_default_cost_price_by_product_id( $product_id )– Get default cost without filterswpd_calculate_cost_profit_by_order( $order_id )– Calculate order profitwpd_get_additional_costs_by_product_id( $product_id )– Get custom product costs
Related Filters
- wpd_ai_cost_price_per_unit– Filter returned cost
- wpd_ai_custom_product_cost_options– Register custom cost types