Created
January 12, 2018 18:12
-
-
Save ChrisFlannagan/ae68a43ec4b60ac741c95715e58dd001 to your computer and use it in GitHub Desktop.
Revisions
-
ChrisFlannagan revised this gist
Jan 12, 2018 . 1 changed file with 22 additions and 22 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,30 +1,30 @@ <?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(); } ); -
ChrisFlannagan created this gist
Jan 12, 2018 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,30 @@ <?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(); } );