Wpd Ai Cost Price Per Unit

Alpha Insights Documentation

Docs Navigation



Filter: wpd_ai_cost_price_per_unit

Filter the cost price retrieved for a product before it’s used in profit calculations. Allows dynamic cost adjustments based on product, date, quantity, or any custom logic.

Description

This filter runs when retrieving the cost price for any product. It’s called by wpd_get_cost_price_by_product_id() and affects all profit calculations using that product.

Location

File:includes/wpd-functions.php

Function:wpd_get_cost_price_by_product_id()

Line:~274

Parameters

Parameter Type Description
$cost_price_per_unit float The cost price retrieved from product meta
$product_id int ID of the product

Return

Type: float

Description: Modified cost price per unit

Example Usage

Add Percentage Markup to All Costs

add_filter( 'wpd_ai_cost_price_per_unit', 'add_cost_markup', 10, 2 );

function add_cost_markup( $cost, $product_id ) {
    // Add 10% markup to account for additional handling
    return $cost * 1.10;
}

Different Costs by Product Category

add_filter( 'wpd_ai_cost_price_per_unit', 'category_based_cost_adjustment', 10, 2 );

function category_based_cost_adjustment( $cost, $product_id ) {
    $product = wc_get_product( $product_id );
    if ( ! $product ) {
        return $cost;
    }
    
    // Check if product is in "Fragile" category
    if ( $product->is_type( 'simple' ) && has_term( 'fragile', 'product_cat', $product_id ) ) {
        // Add $3 for extra packaging
        $cost += 3.00;
    }
    
    return $cost;
}

Volume-Based Cost Adjustment

add_filter( 'wpd_ai_cost_price_per_unit', 'volume_discount_cost', 10, 2 );

function volume_discount_cost( $cost, $product_id ) {
    // Get total units sold this month for this product
    $units_this_month = get_product_units_sold_this_month( $product_id );
    
    // Apply volume discount from supplier
    if ( $units_this_month > 100 ) {
        // High volume: 15% cost reduction
        $cost = $cost * 0.85;
    } elseif ( $units_this_month > 50 ) {
        // Medium volume: 10% cost reduction
        $cost = $cost * 0.90;
    }
    
    return $cost;
}

Seasonal Cost Adjustments

add_filter( 'wpd_ai_cost_price_per_unit', 'seasonal_cost_adjustment', 10, 2 );

function seasonal_cost_adjustment( $cost, $product_id ) {
    $current_month = date( 'n' );
    
    // Supplier charges more during summer (June-August)
    if ( in_array( $current_month, [6, 7, 8] ) ) {
        $cost = $cost * 1.15; // 15% summer premium
    }
    
    return $cost;
}

Integration with External Inventory System

add_filter( 'wpd_ai_cost_price_per_unit', 'erp_cost_override', 10, 2 );

function erp_cost_override( $cost, $product_id ) {
    // Get product SKU
    $product = wc_get_product( $product_id );
    $sku = $product ? $product->get_sku() : '';
    
    if ( empty( $sku ) ) {
        return $cost;
    }
    
    // Fetch current cost from ERP system (cached for performance)
    $erp_cost = wp_cache_get( 'erp_cost_' . $sku, 'my_erp_costs' );
    
    if ( false === $erp_cost ) {
        // Not in cache, fetch from API
        $erp_cost = my_erp_api_get_product_cost( $sku );
        // Cache for 1 hour
        wp_cache_set( 'erp_cost_' . $sku, $erp_cost, 'my_erp_costs', 3600 );
    }
    
    // Use ERP cost if available, otherwise fall back to Alpha Insights cost
    return $erp_cost ? (float) $erp_cost : $cost;
}

Product-Type Specific Logic

add_filter( 'wpd_ai_cost_price_per_unit', 'subscription_cost_adjustment', 10, 2 );

function subscription_cost_adjustment( $cost, $product_id ) {
    $product = wc_get_product( $product_id );
    
    // For subscription products, adjust for monthly value
    if ( $product && class_exists( 'WC_Subscriptions_Product' ) ) {
        if ( WC_Subscriptions_Product::is_subscription( $product ) ) {
            // Account for acquisition cost spread over subscription lifetime
            $expected_lifetime_months = 12; // Average subscriber stays 12 months
            $cost = $cost / $expected_lifetime_months;
        }
    }
    
    return $cost;
}

Best Practices

  • Always return a float value
  • Handle cases where product doesn’t exist gracefully
  • Cache expensive operations (API calls, complex queries)
  • Use early returns for better readability
  • Test with variable products (variations have different costs)
  • Document your custom logic for future reference

Important Notes

  • Filter runs every time cost is retrieved (frequently!)
  • Results are cached, but filter still runs on cache misses
  • Don’t make database queries inside this filter if possible
  • Return original cost if your logic doesn’t apply

Debugging

add_filter( 'wpd_ai_cost_price_per_unit', 'debug_cost_retrieval', 999, 2 );

function debug_cost_retrieval( $cost, $product_id ) {
    error_log( sprintf( 'Product #%d cost: $%s', $product_id, number_format( $cost, 2 ) ));
    return $cost;
}

Related Filters

Related Functions

  • wpd_get_cost_price_by_product_id( $product_id )– Retrieve product cost
  • wpd_calculate_cost_profit_by_order( $order_id )– Calculate order profit

Got A Question?

Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Alpha Insights

Alpha Insights

The World's Most Advanced WooCommerce Drag & Drop Report Builder.

5/5 – Trustpilot

Alpha Insights