1
votes

question regarding Woocommerce payment gateways.

I’ve customized a payment gateway to take purchase orders, and its loading correctly from a plugin, and appearing on the checkout page the way id like it to; I think its a pretty good start.

Anyway, I have a non-standard form entry (purchase order number) that id like people to fill out, but I don’t know how to attach it to my custom payment gateway so that it appears on the resulting admin side page.

This all works fine:

function payment_fields(){
    if ( $description = $this->get_description() )
        echo wpautop( wptexturize( $description ) );
    global $woocommerce;
    ?>
<form class="form-horizontal" role="form" id="bv-form">
<fieldset>
<!-- Form Name -->
  <legend>Purchase Order</legend>
  <!-- Text input-->
    <div class="form-group">
      <label class="control-label" for="po_no">PO Number</label>  
      <div class="">
      <input id="po_no" name="po_no" type="text" placeholder="" class="form-control input-md">
      </div>
    </div>
...

But when I get here, I don’t know how to modify process_payment( $order_id ) (or what to add to functions.php) to grab the form values from the payment fields. I see that there are $order->billingAddress1 etc., how would I submit the extra couple form fields along with the order, and then secondly, how would I access that to get po_no out of the newly created WC_Order?

  • WP: 3.9.2
  • WC: 2.1.11

Thanks for your help!

1

1 Answers

4
votes

If you are creating a custom Payment Gateway then you should be implementing process_payment() within it. This code will have access to the $_POST variables were submitted by the payment form, so you can refer to it as:

$po_number = ( isset( $_POST['po_no'] ) )? $_POST['po_no'] : 0;

That said it sounds like from your question you may not be actually writing a custom gateway and are instead trying to attach custom fields to the order. If you want to hook into another gateway (like PayPal, Authorize.net, etc) then you should use the WooCommerce action hooks that are called as part of WC_Order.payment_complete() which is what the gateway calls after it collects payment. The actions are woocommerce_pre_payment_complete which runs before anything else and takes the order id as an argument, and woocommerce_payment_complete after stock/statuses/etc are set.

add_action( 'woocommerce_payment_complete', 'my_woocommerce_payment_complete');
function my_woocommerce_payment_complete( $order_id ){
    $po_number = ( isset( $_POST['po_no'] ) )? $_POST['po_no'] : 0;
    update_post_meta( $order_id, 'po_number', $po_number );
}

You can then display this custom field on the Order Edit page by adding a meta box. You'll need to hook into save_post and change the po_number to an input if you want to make it editable.

add_action( 'add_meta_boxes',  'po_number_metabox' );
function add_po_number_metabox(){
    global $post;
    if ( $post->post_type = 'shop_order' ){
            add_meta_box( 'po-number', 'Title',  'po_number_metabox', 'shop_order', 'side', 'default');
    }
}
function po_number_metabox(){
    global $post;
    $po_number = get_post_meta( $post->ID, 'po_number', true);
    echo "PO:> {$po_number}";
}