Skip to content

Instantly share code, notes, and snippets.

@ChrisFlannagan
Created January 12, 2018 18:12
Show Gist options
  • Select an option

  • Save ChrisFlannagan/ae68a43ec4b60ac741c95715e58dd001 to your computer and use it in GitHub Desktop.

Select an option

Save ChrisFlannagan/ae68a43ec4b60ac741c95715e58dd001 to your computer and use it in GitHub Desktop.

Revisions

  1. ChrisFlannagan revised this gist Jan 12, 2018. 1 changed file with 22 additions and 22 deletions.
    44 changes: 22 additions & 22 deletions cod-add-fee.php
    Original 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.
    // 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;
    }
    return;
    }

    // I get my fee from the COD gateway settings field we added
    $cod = new \WC_Gateway_COD();
    $fee = (float) $cod->settings[ 'custom_fee' ];
    // 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();
    if ( $fee <= 0 ) {
    return;
    }

    $order->add_item( $item );
    $order->calculate_totals();
    $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();
    } );
  2. ChrisFlannagan created this gist Jan 12, 2018.
    30 changes: 30 additions & 0 deletions cod-add-fee.php
    Original 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();
    } );