2

I use cash on delivery payment method. When the user select this payment method an extra fee should be added to the order total. NOT to the product line items. Any ideas? I read i a post that a workaround for that is to add a custom line item and charge it the extra fee. Anyway I didn't understand how i should that

I put this code to my custom rule action in order to add a price component via a line item. The problem is it throws an error when i finally save the order in the last line. The error is:

Warning: Invalid argument supplied for foreach() in commerce_price_components_combine() (line 972 of /Users/Sakis/Sites/Under_Construction/e-wireless/profiles/commerce_kickstart/modules/commerce/modules/price/commerce_price.module).

$default_currency_code = commerce_default_currency();
if ($balance = commerce_payment_order_balance($order)) {
   $default_currency_code = $balance['currency_code'];
}
$line_item = commerce_line_item_new('cod_fee', $order->order_id);
dsm($line_item,'line_item');
// Wrap the line item and order to simplify manipulating their field data.
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);

$line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item); 
$line_item_wrapper->line_item_label = 'Αντικαταβολή';
$line_item_wrapper->quantity = 1;
$line_item_wrapper->commerce_unit_price->amount = '200';
$line_item_wrapper->commerce_unit_price->currency_code = $default_currency_code;
$line_item_wrapper->commerce_unit_price->data = commerce_price_component_add(
    $line_item_wrapper->commerce_unit_price->value(),
    'cod_fee',
    $line_item_wrapper->commerce_unit_price->value(),
    TRUE,
    FALSE
);
commerce_line_item_save($line_item);
// Add it to the order's line item reference value.
$order_wrapper->commerce_line_items[] = $line_item;

// Save the order.
$order_wrapper->save();
1

1 Answer 1

1

If you create the COD payment method yourself you can in the code that would normally process payments ect. add the fee to the order by adding a line item to the order. You could use a custom price formatter instead of showing the line item.

I have answered how add a line item in code here.

5
  • I am using Commerce COD module. The line item can't be inserted via rules? If not which hook i need to use to insert the custom line item?
    – dianikol
    Commented Mar 27, 2012 at 8:43
  • @dianikol You could create a custom action that does it, probably will be easier to do it that way. You can create entities via the rules interface as well, but I find it a bit bothersome compared to what little effort is needed when you do it in code.
    – googletorp
    Commented Mar 27, 2012 at 8:48
  • Thanks a lot! I 'll give it a try. Just one more question. Where i should put the code? In a module or in custom action in rules?
    – dianikol
    Commented Mar 27, 2012 at 15:14
  • @dianikol You need to create the new action in a custom module using a hook from rules, hook_rules_action_info or something like that.
    – googletorp
    Commented Mar 27, 2012 at 15:16
  • I edited my post with some code i took from your solution but it doesn't seem to work
    – dianikol
    Commented Mar 27, 2012 at 18:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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