Filter the default value for custom product-level costs during order profit calculations.
This powerful filter allows you to dynamically calculate custom product costs per line item in an order. These costs are calculated per unit and multiplied by quantity. Use this to add packaging costs, custom fees, or any product-specific expenses that vary by item.
File: includes/classes/WPDAI_Order_Calculator.php and includes/classes/WPDAI_Core.php
Method: WPDAI_Order_Calculator::get_custom_product_costs()
Line: ~1123 and ~343
| Parameter | Type | Description |
|---|---|---|
| $default_calculated_value | float | The calculated cost value based on product settings |
| $custom_cost_slug | string | The unique slug identifier for this custom cost type |
| $order | WC_Order | The WooCommerce order object |
| $item | WC_Order_Item_Product | The order line item (contains product data, quantity, etc.) |
Type: float
Description: Modified custom product cost per unit
add_filter( 'wpd_ai_custom_product_cost_default_value', 'weight_based_packaging', 10, 4 );
function weight_based_packaging( $cost, $slug, $order, $item )
{
if ( $slug !== 'packaging_cost' )
{
return $cost;
}
$product = $item->get_product();
if ( ! $product )
{
return $cost;
}
$weight = (float) $product->get_weight();
// Tiered packaging based on weight
if ( $weight > 10 )
{
return 5.00;
// Large packaging
}
elseif ( $weight > 5 )
{
return 3.00;
// Medium packaging
}
elseif ( $weight > 1 )
{
return 1.50;
// Small packaging
}
return 0.75;
// Minimal packaging
}
add_filter( 'wpd_ai_custom_product_cost_default_value', 'gift_wrap_cost', 10, 4 );
function gift_wrap_cost( $cost, $slug, $order, $item )
{
if ( $slug !== 'gift_wrap' )
{
return $cost;
}
// Check if gift wrapping was requested
$gift_wrap_meta = $item->get_meta('_gift_wrap', true);
if ( $gift_wrap_meta === 'yes' )
{
return 3.50;
// Cost of gift wrapping materials + labor
}
return 0;
}
add_filter( 'wpd_ai_custom_product_cost_default_value', 'category_handling_fees', 10, 4 );
function category_handling_fees( $cost, $slug, $order, $item )
{
if ( $slug !== 'handling_fee' )
{
return $cost;
}
$product = $item->get_product();
if ( ! $product )
{
return $cost;
}
$product_id = $product->get_id();
// Extra handling for fragile items
if ( has_term( 'fragile', 'product_cat', $product_id ) )
{
return 4.00;
}
// Extra handling for oversized items
if ( has_term( 'oversized', 'product_cat', $product_id ) )
{
return 6.50;
}
// Electronics need special packaging
if ( has_term( 'electronics', 'product_cat', $product_id ) )
{
return 2.50;
}
return 1.00;
// Standard handling
}
add_filter( 'wpd_ai_custom_product_cost_default_value', 'customization_labor', 10, 4 );
function customization_labor( $cost, $slug, $order, $item )
{
if ( $slug !== 'labor_cost' )
{
return $cost;
}
// Check if product was customized
$has_engraving = $item->get_meta('_engraving_text', true);
$custom_color = $item->get_meta('_custom_color', true);
$labor_cost = 0;
if ( ! empty( $has_engraving ) )
{
$labor_cost += 8.00;
// Engraving labor
}
if ( ! empty( $custom_color ) )
{
$labor_cost += 5.00;
// Custom color mixing
}
return $labor_cost;
}
add_filter( 'wpd_ai_custom_product_cost_default_value', 'bulk_cost_discount', 10, 4 );
function bulk_cost_discount( $cost, $slug, $order, $item )
{
if ( $slug !== 'setup_fee' )
{
return $cost;
}
$quantity = $item->get_quantity();
// Setup fee per unit decreases with quantity
if ( $quantity >= 100 )
{
return 0.50;
// Bulk discount
}
elseif ( $quantity >= 50 )
{
return 1.00;
}
elseif ( $quantity >= 10 )
{
return 2.00;
}
return 5.00;
// Standard setup fee per unit
}
add_filter( 'wpd_ai_custom_product_cost_default_value', 'percentage_based_fee', 10, 4 );
function percentage_based_fee( $cost, $slug, $order, $item )
{
if ( $slug !== 'marketplace_fee' )
{
return $cost;
}
// 12% marketplace fee on product price
$product_price = $item->get_total() / $item->get_quantity();
// Price per unit
return $product_price * 0.12;
}
add_filter( 'wpd_ai_custom_product_cost_default_value', 'erp_product_costs', 10, 4 );
function erp_product_costs( $cost, $slug, $order, $item )
{
$product = $item->get_product();
if ( ! $product )
{
return $cost;
}
$sku = $product->get_sku();
if ( empty( $sku ) )
{
return $cost;
}
// Fetch actual cost from ERP
$erp_costs = wp_cache_get( 'erp_costs_' . $sku, 'my_erp' );
if ( false === $erp_costs )
{
$erp_costs = my_erp_get_product_costs( $sku );
wp_cache_set( 'erp_costs_' . $sku, $erp_costs, 'my_erp', 3600 );
}
if ( isset( $erp_costs[$slug] ) )
{
return (float) $erp_costs[$slug];
}
return $cost;
}
$custom_cost_slug to target specific cost types$item->get_meta() to access custom product dataIf you return $3.00 and the customer ordered 5 units:
Total Custom Cost = $3.00 × 5 = $15.00
add_filter( 'wpd_ai_custom_product_cost_default_value', 'debug_product_costs', 999, 4 );
function debug_product_costs( $cost, $slug, $order, $item )
{
$product = $item->get_product();
error_log( sprintf(
'Order #%d - Item: %s - Cost Type: %s - Value: $%s × %d units',
$order->get_id(),
$product ? $product->get_name() : 'Unknown',
$slug,
number_format( $cost, 2 ),
$item->get_quantity()
));
return $cost;
}
wpd_get_custom_product_cost_options( $product_id ) - Get registered custom costswpd_calculate_cost_profit_by_order( $order_id ) - Calculate order profit