0
votes

I have a checkout page that adds a custom fee "Delivery Charge" of $100.

Here's my code:

$cart->add_fee( 'Delivery Charge', 100, false, '' );

However I want to remove that "Delivery Charge" fee from my checkout when a certain function fires.

I looked online but can't seem to find any documentation for clearing all custom fees from a checkout page.

Is there a specific method such as "remove_fee" that enables me to remove any fee that's currently on the checkout page?

1

1 Answers

1
votes

calling $cart->fees_api() gives you access to WC_Cart_Fees apis

This is will remove all fees from the cart:

$cart->fees_api()->set_fees();

It accepts first argument as an array of fees data, but if nothing gets passed then it'll clear out all fees.

To remove a specific fee such as 'Delivery Charge', you have to do this:

$fees = $cart->get_fees();
foreach ($fees as $key => $fee) {
    // unset that specific fee from the array
    if($fees[$key]->name === __( "Delivery Charge")) {
        unset($fees[$key]);
    }
}
// set new fee array
$cart->fees_api()->set_fees($fees);