Alpha Insights (Pro) - Monthly - Single Site
$19 / month
Alpha Insights (Pro) - Monthly - Single Site
$19 / month
Alpha Insights (Pro) - Annual - Single Site
$99 / year
Alpha Insights (Pro) - Monthly - Five Sites
$49 / month
Alpha Insights (Pro) - Annual - Five Sites
$299 / year
Alpha Insights (Pro) - Annual - Three Sites
$199 / year
Alpha Insights (Pro) - Quarterly - Single SIte
$39 every 3 months
Subtotal : $59
This is the main function that we use to calculate and return the profit analytics for a given order.
Parameter 1 ($order_id) will accept an order object, or a order_id which is the order we are seeking to calculate.
Parameter 2 will force a recalculation on this order and save the updated figures into the calculation cache to be called next time.
If a Refund Order Object is passed into this function, we will revert back to the refund object’s original order for the calculation.
/**
*
* Calculate the profit of an order by order_id or order object.
*
* Will return an associative array with detailed calculations for this order.
* By default, will try fetch the values from our calculation cache, unless the $update_values parameter is set to true, which will refresh the cache.
*
* @param int|WC_order $order_id / $order The order_id or order object
* @param bool $update_values (Default False) True to fore a recalculation and save the values to database
*
* @since 1.0.0
* @version 4.4.15
* @return array Wil return an associative array for of the calculation values saved for this order
*
*/
function wpd_calculate_cost_profit_by_order( $order_id = null, $update_values = false )
$results = array(
// ID
'order_id' => $order_id,
// Main Calculations
'total_order_revenue_inc_tax_and_refunds' => $order_revenue + $refund_amount, // Including Tax
'total_order_revenue' => $order_revenue, // Including Tax
'total_order_revenue_excluding_tax' => $order_revenue - $order_tax,
'total_order_cost' => $total_order_cost, // Does not include tax
'total_order_tax' => $order_tax, // All tax paid on this order
'total_order_profit' => $total_order_profit, // Total order revenue, minus order costs & tax
'total_order_margin' => $total_order_margin, // The bottomline profit compared to total revenue
'total_shipping_charged' => $shipping_total, // Contributes to total order tax
'total_shipping_cost' => $meta_total_shipping_cost, // Contributes to total order tax
'payment_gateway_cost' => $meta_payment_gateway_cost, // Contributes to total order tax
// Custom order costs
'total_custom_order_costs' => $total_custom_order_costs,
'custom_order_cost_data' => $custom_order_cost_array,
// Product Data
'total_product_revenue_at_rrp' => $total_product_revenue_at_rrp,
'total_product_revenue' => $total_product_revenue,
'total_product_discounts' => $total_product_discounts,
'total_product_discount_percent' => wpd_calculate_percentage( $total_product_discounts, $total_product_revenue_at_rrp ),
'total_qty_sold' => $total_qty_sold,
'total_product_profit' => $total_product_profit,
'total_skus_sold' => $total_skus_sold,
'total_product_cost' => $total_product_cost,
'total_product_custom_costs' => $total_product_custom_costs,
'product_data' => $product_data,
// Coupon Data
'total_order_revenue_before_coupons' => $total_coupon_discounts + $order_revenue,
'total_coupon_discounts' => $total_coupon_discounts,
'total_coupon_discount_percent' => wpd_calculate_percentage( $total_coupon_discounts, ( $total_coupon_discounts + $order_revenue ) ),
'coupons_used' => $coupon_data,
// Discount Data
'total_order_revenue_before_discounts' => $total_order_revenue_before_discounts,
'total_order_discounts' => $total_order_discounts,
'total_order_discount_percent' => wpd_calculate_percentage( $total_order_discounts, $total_order_revenue_before_discounts ),
// Order Meta
'cache_version' => WPD_AI_CACHE_UPDATE_REQUIRED_VER,
'class_type' => get_class($order),
'created_via' => $order->get_created_via(),
'order_type' => $order->get_type(),
'order_status' => $order_status,
'date_paid' => $date_paid,
'date_created' => $date_created,
'is_paid' => $is_paid,
'order_currency' => $currency_array,
'payment_gateway' => $payment_gateway,
'landing_page_url' => $landing_page_url_raw,
'referral_source_url' => $referral_source_url_raw,
'user_agent' => $user_agent_data,
// Subscription Data
'is_parent_subscription' => (int) $is_subscription_parent_order,
'is_renewal_subscription_order' => (int) $is_subscription_renewal_order,
'parent_subscription_ids' => $parent_subscription_ids,
'renewal_subscription_ids' => $renewal_subscription_ids,
// Customer Data
'customer_id' => $customer_id,
'user_id' => $user_id,
'is_registered_user' => $is_registered_user,
'new_returning_customer' => $new_customer,
'billing_email' => $billing_email,
'billing_phone' => $billing_phone,
'billing_first_name' => $billing_first_name,
'billing_last_name' => $billing_last_name,
'billing_country' => $billing_country,
'billing_state' => $billing_state,
'billing_postcode' => $billing_postcode,
'billing_company' => $billing_company,
// Refund Data
'refund_order_id' => $refund_order_id,
'total_refund_amount' => $refund_amount,
'total_order_revenue_before_refunds' => $order_revenue_before_refunds,
'total_refund_quantity' => $total_quantity_refunded,
'total_refund_sku_count' => $refunded_sku_count,
'full_refund' => $full_refund,
'partial_refund' => $partial_refund,
'refund_data' => $refund_data,
// Tax Data
'tax_data' => $tax_breakdown,
);
This is an example of something similar to what we use in Alpha Insights to output the values into the admin columns on the orders list.
/**
*
* Adds 'Profit' column content to 'Orders' page immediately after 'Total' column.
*
*/
public function display_admin_order_column_data( $column, $post_id_or_order_object ) {
if ( 'order_profit' == $column || 'order_margin' == $column || 'wpd_ai_new_vs_returning' == $column ) {
// Get order ID
if ( is_a($post_id_or_order_object, 'WC_Order') ) {
$order_id = $post_id_or_order_object->get_id();
} else {
if ( is_numeric($post_id_or_order_object) && $post_id_or_order_object > 0 ) {
$order_id = $post_id_or_order_object;
} else {
global $post;
$order_id = $post->ID;
}
}
// Data
$order_data = wpd_calculate_cost_profit_by_order( $order_id );
$calculated_profit = $order_data['total_order_profit'];
$margin = $order_data['total_order_margin'];
$new_returning = $order_data['new_returning_customer'];
if ( 'order_profit' == $column ) {
if ( empty( $calculated_profit ) ) {
echo wpd_store_price( 0 );
} else {
echo wpd_store_price( $calculated_profit );
}
} elseif ( 'order_margin' == $column ) {
echo $margin . '%';
} elseif ( 'wpd_ai_new_vs_returning' == $column ) {
$data_tip = ($new_returning == 'new') ? 'This is the first order placed by this email address' : 'This email address has placed an order prior to this date';
echo ''.ucfirst( $new_returning ) .' Customer';
}
}
}
Supercharge your WooCommerce store with WP Davies.
© 2019-2025 WP Davies | ABN: 23 988 145 173