Hooks and Filters

Alpha Insights Documentation

Hooks & Filters Available In Alpha Insights

Hooks & filters are being introduced as of Version 2.0.20, these are used to modify Alpha Insights behaviour. If you are unsure of anything please do not hesitate to get in touch with us.

Filters

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
		 * 
		 * 	@return array $results
		 *  @param WC_Order $order WooCommerce Order Object
		 * 
		 **/
		$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.

				
					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;
	
}
				
			
wpd_ai_order_shipping_cost

This filter is used to alter the shipping cost of an order.

Parameter 1 is the total shipping cost, which is a floating (decimal) number, your filter must return a decimal number. The WC_Order object is also passed as an additional argument.

				
						$meta_total_shipping_cost = apply_filters( 'wpd_ai_order_shipping_cost', $meta_total_shipping_cost, $order );

				
			
wpd_ai_order_payment_gateway_cost

This filter is used to alter the payment gateway cost of an order.

Parameter 1 is the total payment gateway cost, which is a floating (decimal) number, your filter must return a decimal number. The WC_Order object is also passed as an additional argument.

				
					$meta_payment_gateway_cost = apply_filters( 'wpd_ai_order_payment_gateway_cost', $meta_payment_gateway_cost, $order );
				
			

Example

This is an example of a snippet we created for one of our clients as they had a PayPal fee being reported in a different currency to the currency that there store was in, so needed to be converted for correct cost calculations.

				
					add_filter( 'wpd_ai_order_payment_gateway_cost', 'wpd_adjust_paypal_gateway_fee_multicurrency', 10, 2 );
function wpd_adjust_paypal_gateway_fee_multicurrency( $meta_payment_gateway_cost, $order ) {
	
		// Safety Checks
		if ( ! function_exists( 'wpd_get_order_currency_conversion_rate' ) ) return $meta_payment_gateway_cost;
		if ( ! function_exists( 'wpd_get_store_currency' ) ) return $meta_payment_gateway_cost;
		if ( ! is_a($order, 'WC_Order') ) return $meta_payment_gateway_cost;
	
		// Collect Variables
		$order_id = $order->get_id();
		$paypal_fee_data = get_post_meta( $order_id, '_ppcp_paypal_fees', true );
	
		if ( is_array($paypal_fee_data) && ! empty($paypal_fee_data) ) {
			
			if ( array_key_exists('paypal_fee', $paypal_fee_data) ) {
				
				$paypal_fee = $paypal_fee_data['paypal_fee']['value'];
				$paypal_currency_code = $paypal_fee_data['paypal_fee']['currency_code'];
				$store_currency_code = wpd_get_store_currency();
				
				// If it looks like we need to convert currency, lets give it a go
				if ( $store_currency_code != $paypal_currency_code ) {
					
					$conversion_rate = wpd_get_order_currency_conversion_rate( $order );
					$rate = $conversion_rate['exchange_rate'];
					
					if ( is_numeric($rate) ) {
						return $paypal_fee * $rate;
					}
					
				}
				
			}
			
		}
		
		return $meta_payment_gateway_cost;
	
}