wpd_ai_order_payment_gateway_cost_default_value
This filter is used to alter the default payment gateway cost for an order. This value will be overriden if you manually assign a cost on a particular 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.
/**
*
* Filters the default payment gateway cost of an order
*
* @param float The current payment gateway cost assigned to this order
* @param WC_Order The order object
*
* @return float The updated payment gateway cost
*
**/
$payment_gateway_cost = (float) apply_filters( 'wpd_ai_order_payment_gateway_cost_default_value', $payment_gateway_cost, $this->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.
Note: Filtering this value will require a cache refresh after you have made your change.
add_filter( 'wpd_ai_order_payment_gateway_cost_default_value', 'wpd_adjust_paypal_gateway_fee_multicurrency', 10, 2 );
function wpd_adjust_paypal_gateway_fee_multicurrency( $payment_gateway_cost, $order ) {
// Safety Checks
if ( ! function_exists( 'wpd_get_order_currency_conversion_rate' ) ) return $payment_gateway_cost;
if ( ! function_exists( 'wpd_get_store_currency' ) ) return $payment_gateway_cost;
if ( ! is_a($order, 'WC_Order') ) return $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 $payment_gateway_cost;
}
This filter will override the default settings found in Alpha Insights > General Settings but will be overriden by the cost overrides in the order edit page.
If you set a value from the order edit page for a particular order, this saved value will take precedence over this filtered value.
Review the payment gateway cost calculation method below for how the logic is applied.
/**
*
* Calculate payment gateway fees
*
**/
private function calculate_payment_gateway_fees() {
// Default Payment Gateway Cost
$payment_gateway_cost_multiplier = (float) wpd_divide( $this->cost_defaults['default_payment_cost_percent'], 100 );
$payment_gateway_cost_fee = (float) $this->cost_defaults['default_payment_cost_fee'];
$payment_gateway_cost = ( $this->results['total_order_revenue'] * $payment_gateway_cost_multiplier ) + $payment_gateway_cost_fee;
// Check if we have pre-defined fee keys stored in meta
$payment_gateway_fee_meta_keys = array( '_stripe_fee', '_paypal_fee', 'PayPal Transaction Fee', 'HitPay_fees' );
foreach( $payment_gateway_fee_meta_keys as $meta_key ) {
// Try find stored meta value
$stored_fee = $this->order->get_meta( $meta_key );
if ( is_numeric($stored_fee) ) {
$payment_gateway_cost = (float) $stored_fee;
break;
}
}
// Assume no gateway fees if there's no income
if ( $this->results['total_order_revenue'] == 0 ) $payment_gateway_cost = 0;
/**
*
* Filters the default payment gateway cost of an order
*
* @param float The current payment gateway cost assigned to this order
* @param WC_Order The order object
*
* @return float The updated payment gateway cost
*
**/
$payment_gateway_cost = apply_filters( 'wpd_ai_order_payment_gateway_cost_default_value', $payment_gateway_cost, $this->order );
// Saved Value
$meta_payment_gateway_cost = $this->order->get_meta( '_wpd_ai_total_payment_gateway_cost' );
if ( is_numeric($meta_payment_gateway_cost) ) $payment_gateway_cost = (float) $meta_payment_gateway_cost;
// Store in main var
$this->results['payment_gateway_cost'] = (float) $payment_gateway_cost;
}