I need to get the fees of my orders by code. There are two possible fees: stripe and paypal.
The Stripe Fee is saved within the order meta: _stripe_fee
I need to get the amount that's in there. Or: all of the possible fees (paypal and stripe).
While I could just get the meta field, I really don't want to hardcode the fieldname.
Is there a way to get alle the fees for an order?
I tried to get it like that:
$order = wc_get_order( 8012 );
var_dump($order->get_fees());
But the Array is empty.
But
var_dump($order)
Shows that there is a stripe fee (and the backend and database also says so)
So, theoretically $order->fees() should return fees. It does this by returning the array fee_line. Unfortunately neither Stripe nor Paypal appear to write to thatfee_line. Instead they write to the meta property.
This doesn't stop you from rolling your own function to return either Stripe or Paypal fees though:
function get_order_fees(WCOrder $order) {
switch(true) {
// get fees from Stripe, if exists
case $fees = $order->get_meta("_stripe_fee");
break;
// get fees from Paypal, if exists
case $fees = $order->get_meta("_paypal_transaction_fee"):
break;
// otherwise fee is 0
default:
$fees = 0;
break;
}
return $fees;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With