skip to Main Content

I use more then one discount using add_fee form, for example

$wc_cart->add_fee( sprintf(__('fee 1', 'fee1') )."", -10, false );
$wc_cart->add_fee( sprintf(__('fee 2', 'fee1') )."", -20, false );
$wc_cart->add_fee( sprintf(__('fee 3', 'fee1') )."", -30, false );

I want to calculate and display the sum of whole add_fee values like

Total fee : 60

How to get add fee value from Woo-commerce.

Thanks in advance.

2

Answers


  1. You can use the below filter to calculate add_fee() total please check and add fee 2 and fee 3 too : This just for example you can modify the code according to your need

    add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
    function woocommerce_custom_surcharge() {
    global $woocommerce;
    
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;
    
        $percentage = 0.03;
        $taxes = array_sum($woocommerce->cart->taxes);
        $surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total + $taxes ) * $percentage;   
        // Make sure that you return false here.  We can't double tax people!
        $woocommerce->cart->add_fee( 'Fee 1', $surcharge, false, '' );
    
    }
    
    Login or Signup to reply.
  2. You can use below code to get all fees and display after cart subtotal,

    <?php
    function get_all_fees() {
        $fee_object = WC()->cart->fees_api();
        $allfeedata = $fee_object->get_fees();
        $totalfee = array_sum(array_column($allfeedata, 'amount'));
        ?>
                        <tr class="fee-total">
                    <th><?php esc_html_e('Total Fee', 'woocommerce'); ?></th>
                    <td data-title="<?php esc_attr_e('Total Fee', 'woocommerce'); ?>"><?php echo absint($totalfee); ?></td>
                </tr>
        <?php
    }
    
    add_action('woocommerce_cart_totals_before_order_total', 'get_all_fees');
    ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search