I know how to add a line item programmatically, but I can't seem to find a way to specifically add a custom commerce fee in PHP. Commerce fees seem to register in two locations in the order array: Once under commerce_line_items, and once under commerce_order_total (See attached image). They are different than a regular line item in that they don't show up as products, but rather in the order totals box at the end of the order. The fee shown in the attachment is one that has been entered using the Edit form in the admin section. I need to add one in code. Any ideas?

enter image description here

2 Answers 2


What you're seeing is the commerce price component and the relevant line item that were added to the order. It gets added by the commerce_fees_apply() function from commerce_feeds.rules.inc.

So, if you're wanting to programmatically add a $5 fee to an order, then you can do so like this:

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

Where 'machine_name_of_fee' would be the machine name of the fee that was created using the UI. Remember that the 3rd argument will be an amount and not a decimal. You'll also need to pass in the currency code. If you're wanting it to take the value of the currency code of the order, you could replace 'USD' with something like.

$currency_code = $order->commerce_order_total[LANGUAGE_NONE][0]['currency_code'];
commerce_fees_apply($order_wrapper, $fee, 500, $currency_code);
  • thank you so much! that makes sense. Although I get this error when called from my custom module: Fatal error: Call to undefined function commerce_fees_apply() in /Users/.../my_module.module on line 588 How do I get my module to see that function?
    – sakeferret
    Apr 20, 2016 at 15:32
  • You'll need to call module_load_include('inc', 'commerce_feeds', 'commerce_fees.rules');.
    – nvahalik
    Apr 20, 2016 at 15:34
  • Thanks. Was missing the 'inc' part. It's loading now, but now I'm getting this error: Warning: Illegal offset type in isset or empty in commerce_fees_load() (line 168 of /Users/.../sites/all/modules/commerce_fees/commerce_fees.module). That's the line for commerce_fees_load which appears to only require a string... which is what I'm giving it. I've also double checked that the machine name is correct. At any rate, thanks for the direction, I'll keep tooling away at why it isn't registering. (Also, you typed in "feeds" a couple of times... I assume you meant "fees".) Thanks!
    – sakeferret
    Apr 20, 2016 at 16:08
  • OK for the life of me I can't figure out why commerce_fees_load() is returning an "empty" error. I've even called $feeTypes = commerce_fees_get_types(); dsm($feeTypes); to make sure that all my custom fee types are available, and sure enough they all appear with the correct machine names. If you have any other ideas, i'd appreciate it!
    – sakeferret
    Apr 20, 2016 at 18:02
  • Does anyone know at what point fees get applied? When I apply them before sending the order through the checkout process, they don't seem to register.
    – sakeferret
    Apr 22, 2016 at 22:36

If you stucked in why #1 doesn't work, you should try something like this:

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

Good luck!

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .