• Resolved Sleneau

    (@sleneau)


    This bothered me for a while, but I found a solution:

    There is no native function to remove fees, you have to retrieve all the fees, and then cycle through them, checking the names, and storing all the fee objects in an array. Check the “name” property, and if you find the fee(s) you wish to delete, simply exclude them from the process. Afterward, replace the entire ‘fees’ session variable with the updated array of fees.

    I’m sure the code could be refactored into a much more elegant plugin, but this is how I did it.

    $fees = WC()->cart->get_fees;
    $newfees = array();
    
    foreach ($fees as $fee) {
    if ($fee->name == 'NAME OF FEE YOU WANT TO DELETE') {} //Don't add Shipping Insurance
    else {$newfees[] = $fee;}
    }
    
    WC()->session->set('fees',$newfees);
Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Sleneau

    (@sleneau)

    Here’s the refactor. It simply takes the name of the fee you want to delete as the argument, and removes it from the Woocommerce session. Drop it into your functions.php, and then call it whenever you want to delete a fee.

    It’d probably be best employed in an action hook, though.

    function delete_fee ($fee_name) {
    $fees = WC()->cart->get_fees;
    $newfees = array();
    
    foreach ($fees as $fee) {
    if ($fee->name == $fee_name) {} //Don't add Shipping Insurance
    else {$newfees[] = $fee;}
    }
    
    WC()->session->set('fees',$newfees);
    }

    Hi Sleneau,
    I am trying to user delete_fee function with ajax. after setting up session i have called jQuery( ‘body’ ).trigger( ‘update_checkout’ ); function after response. But it is not removing fee.I am stuck.

    Thanks in advanced.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘[Plugin: Woocommerce] How to Delete Custom Fees’ is closed to new replies.