Why are my custom fees disappearing from my orders? I've set up my shop so that I am able to create an entire order programmatically. Line items register fine. However, I can't get fees to stick through the ordering process. I'm using the following code:

$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
$fee = 'machine_name_of_fee';
commerce_fees_apply($order_wrapper, $fee, 500, 'USD');
commerce_order_save($order);


I then push the order through to checkout so that I can manage it on the backend: 

commerce_order_status_update($order, 'checkout_checkout');


I get no errors, but when I check the order in the admin backend, there are no fees that show up. I've double checked that the machine name is correct, and have even confirmed that the commerce fees module returns as 'empty' if the machine name doesn't exist. This leads me to believe that the fees are indeed being added, but that they are being removed at some point after a checkout stage. Is there a specific stage where fees have to be added in order to stick to and affect the order total? Am I implementing commerce_fees_apply() incorrectly?
You need to apply commerce fees after "checkout_checkout" status and also move order to next status i assume "checkout_review".

commerce_fees_apply() is a rule callback and you are calling it directly from your code.This function first removes all fee type line item and then adds add the same and expects values from rules config.

I tried this and it worked.

module_load_include('inc','commerce_fees','commerce_fees.rules');
$order = commerce_order_load(3);
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
$fee = 'test';
commerce_fees_apply($order_wrapper, $fee, 500, 'USD');
commerce_order_save($order);
commerce_order_status_update($order, 'checkout_review');


But curious to know why you are writing code for this.You can easily achieve this using rules.