Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WooCommerce get Stripe Fee of Order

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)

like image 533
nuriye Avatar asked Oct 24 '25 14:10

nuriye


1 Answers

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;
}
like image 160
chatnoir Avatar answered Oct 27 '25 04:10

chatnoir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!