-1

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

1

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 '16 at 15:32
  • You'll need to call module_load_include('inc', 'commerce_feeds', 'commerce_fees.rules');. – nvahalik Apr 20 '16 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 '16 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 '16 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 '16 at 22:36
1

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

$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
$fee = 'FEE_MACHINE_NAME';
commerce_fees_apply($order_wrapper, $fee, 500, 'USD');
$order_wrapper->save();

Good luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.