phpwordpresswoocommercefee

How to get sum of shipping and payment method fees in WooCommerce


I'm using WooCommerce for my e-commerce store and I'd like to know if there is some option, how to get a total price (fee) of shipping and total price (fee) of payment methods and their sum on Checkout page. (shipping fee + payment fee = result). I found out how to get a shipping fee without currency symbol ($), which is what I was looking for. But I can't find out the solution how to get payment fee (total), because sometimes when you use Cash-on-delivery tax, you need to get sum of all payment method fees. And so on I need to get sum of shipping fees and payment method fees.

For now I got something like the code, which is mentioned below, but it's not as good as I need.

<?php echo $shipping_price = WC()->session->get('cart_totals') €['shipping_total'];?> 
    <?php foreach ( WC()->cart->get_fees() as $fee ) : ?>,
        <?php $checkout_fee = wc_cart_totals_fee_html( $fee ); 
                    
           echo $checkout_fee;
                    
        ?>
<?php endforeach; ?>

With the code above I get both values, but one is without currency symbol and the second one is with currency symbol and also placed in foreach loop. So I can't get a sum of them.

If anybody could help, I'd be very grateful.

Thanks.


Solution

  • Instead you could use directly some related WC_Cart methods, to get what you expect, like:

    <?php 
        $shipping_total = WC()->cart->get_shipping_total() + WC()->cart->get_shipping_tax();
        $fees_total     = WC()->cart->get_fee_total() + WC()->cart->get_fee_tax();
    
        // Output formatted total
        echo '<p>Total shipping and fees: ' . wc_price( $fees_total + $shipping_total ) . '</p>';  
    ?>