woocommerce process payment php code example

Example 1: woocommerce custom payment process method

function process_payment( $order_id ) {
    global $woocommerce;
    $order = new WC_Order( $order_id );

    // Mark as on-hold (we're awaiting the cheque)
    $order->update_status('on-hold', __( 'Awaiting cheque payment', 'woocommerce' ));

    // Remove cart
    $woocommerce->cart->empty_cart();

    // Return thankyou redirect
    return array(
        'result' => 'success',
        'redirect' => $this->get_return_url( $order )
    );
}

Example 2: woocommerce gateway process payment

public function process_payment( $order_id ) {
    
    $order = wc_get_order( $order_id );
            
    // Mark as on-hold (we're awaiting the payment)
    $order->update_status( 'on-hold', __( 'Awaiting offline payment', 'wc-gateway-offline' ) );
            
    // Reduce stock levels
    $order->reduce_order_stock();
            
    // Remove cart
    WC()->cart->empty_cart();
            
    // Return thankyou redirect
    return array(
        'result'    => 'success',
        'redirect'  => $this->get_return_url( $order )
    );
}

Tags:

Misc Example