Adding pre-orders support to your existing payment gateway extension is a great way to attract new customers and provide a valuable feature for your existing customers. Pre-Orders offers a full-featured API for pre-order management — you just need to add payment processing. This guide outlines the required steps for integrating a payment gateway with WooCommerce Pre-Orders. Note that your payment gateway must provide some way to tokenize a customer’s payment method so that it can be charged later. There are 3 steps to add pre-order support:
- Register support for Pre-Orders
- Handle initial processing of Pre-Orders
- Handle processing of pre-order completion payments
Register support for Pre-Orders
↑ Back to toppre-orders
(and products
if your gateway also supports product purchases).
For example, the __construct() function of your gateway class should include a line like the following:
class WC_Best_Payment_Gateway_Ever extends WC_Payment_Gateway { public function __construct() { ... $this->supports = array( 'products', 'pre-orders' ); ... } ... }Now whenever an order contains a pre-order charged upon release, your payment gateway will be shown as a payment option.
Processing payments for Pre-Orders
↑ Back to topWC_Pre_Orders_Order::order_contains_pre_order()
to determine if the order contains a valid pre-order. You may pass in an Order ID or an order object.
Use WC_Pre_Orders_Order::order_requires_payment_tokenization()
to determine if the order requires payment tokenization. Pre-Orders that are charged upon release typically require payment tokenization so that the customer’s payment method can be charged when the pre-order is released.
The best practice for handling these cases is :
public function process_payment( $order_id ) { ... if ( WC_Pre_Orders_Order::order_contains_pre_order( $order_id ) ) { // the order if ( WC_Pre_Orders_Order::order_requires_payment_tokenization( $order_id ) ) { ... // perform a simple authorization/void (or whatever method your gateway requires) // to get the payment token that may be used later to charged the customer's payment method // mark order as pre-ordered, this will also save meta that indicates a payment token // exists for the pre-order so that it may be charged upon release WC_Pre_Orders_Order::mark_order_as_pre_ordered( $order ); // here you should empty the cart and perform whatever other tasks a successful purchase requires ... } else { // handle pre-orders charged upfront, or paying for a newly-released pre-order with the gateway // process just like regular product return parent::process_payment( $order_id ); } } ... }The Authorize.net CIM and Braintree TR gateways support pre-orders so reviewing their code may help to understand this process better.
Processing Pre-Order Completion Payments
↑ Back to top// process batch pre-order payments add_action( 'wc_pre_orders_process_pre_order_completion_payment_' . $this->id, array( $this, 'process_pre_order_payments' ) );Inside the
process_pre_order_payments()
method, you should charge the order total to the payment method that was saved to the order during the initial checkout. Your code should look like:
public function process_pre_order_payments( $order ) { // the total amount to charge is the the order's total $total = $order->get_total(); ... // get the payment token and attempt to charge the transaction // success! if ( $transaction_successful ) { ... // complete the order $order->payment_complete(); } else { // failure // mark the order as failed ... } ... }There is no special order handling required for processing a pre-order payment completion. If the payment fails for any reason, you should mark the order as failed. In this case the customer will be emailed and instructed to come back to the store and pay for the order with a different payment method. You must call
$order->payment_complete()
when the transaction is successful, as this will change the pre-order’s status to completed
and let the shop admin know the pre-order can be shipped.