Created
January 12, 2018 18:12
-
-
Save ChrisFlannagan/ae68a43ec4b60ac741c95715e58dd001 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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