Skip to content

Instantly share code, notes, and snippets.

@ChrisFlannagan
Created January 12, 2018 18:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChrisFlannagan/ae68a43ec4b60ac741c95715e58dd001 to your computer and use it in GitHub Desktop.
Save ChrisFlannagan/ae68a43ec4b60ac741c95715e58dd001 to your computer and use it in GitHub Desktop.
<?php
add_action( 'woocommerce_checkout_create_order', function( $order, $data ) {
// For my example I only want to do this on payments through cash on delivery gateway
// This is not a requirement though, you can do it for any reason you want.
if ( $order->get_payment_method() !== 'cod' ) {
return;
}
// I get my fee from the COD gateway settings field we added
$cod = new \WC_Gateway_COD();
$fee = (float) $cod->settings[ 'custom_fee' ];
if ( $fee <= 0 ) {
return;
}
$item = new \WC_Order_Item_Fee();
$item->set_props( array(
'name' => __( 'Cash on delivery fee', 'textdomain' ),
'tax_class' => 0,
'total' => $fee,
'total_tax' => 0,
'order_id' => $order->get_id(),
) );
$item->save();
$order->add_item( $item );
$order->calculate_totals();
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment