wpd_ai_calculate_cost_profit_by_order
This filter is used to alter the results of the main Alpha Insights calculations on orders.
Parameter 1 is the array of data which has been calculated, parameter 2 is the order object, parameter 1 ($results) must be returned and in array format, the $result array must be returned with all the existing array keys passed back.
/**
*
* Filters all the calculation values after the function is complete but before it has been returned or saved to meta
*
* @param array $results The current order calculation object as an associate array
* @param WC_Order $order WooCommerce Order Object
*
* @return array $results
*
**/
$results = apply_filters( 'wpd_ai_calculate_cost_profit_by_order', $results, $order );
Example:
This is an example of a snippet we created for one of our clients because they had stripe payments coming in a different currency, which was the absolute value of money received, so calculations needed to be adjusted for this to be the net revenue of the order.
Note: Filtering this value will require a cache refresh after you have made your change.
add_filter( 'wpd_ai_calculate_cost_profit_by_order', 'wpd_adjust_stripe_net_revenue', 10, 2 );
function wpd_adjust_stripe_net_revenue( $calculations, $order ) {
// Safety Checks
if ( ! function_exists( 'wpd_calculate_margin' ) ) return $calculations;
if ( ! is_a($order, 'WC_Order') ) return $calculations;
// Get variables
$order_id = $order->get_id();
$stripe_fee = (float) get_post_meta( $order_id, '_stripe_fee', true );
$stripe_net = (float) get_post_meta( $order_id, '_stripe_net', true );
$net_payment = $stripe_fee + $stripe_net;
// Check if we have values & make adjustments
if ( $net_payment > 0 ) {
$calculations['total_order_revenue'] = $net_payment;
$calculations['total_order_revenue_excluding_taxes'] = $net_payment - $calculations['order_tax_paid'];
$calculations['total_order_revenue_before_refunds'] = $net_payment + $calculations['total_refund_amount'];
$calculations['total_order_profit'] = $net_payment - $calculations['total_order_cost'];
$calculations['total_order_profit_after_tax_deduction'] = $net_payment - $calculations['total_order_cost'] - $calculations['order_tax_paid'];
$calculations['total_order_margin'] = wpd_calculate_margin( $calculations['total_order_profit'], $calculations['total_order_revenue']);
}
return $calculations;
}