Create custom invoice on Order Received

woocommerce_before_thankyou” is a hook used to add content or process before the order details in Woocommerce Order Received Page

Sample Code:

Note: This sample code is for creating invoice with WPCargo Advance Invoice upon successful checkout.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
/************************************************
 * Custom Create Invoice on Successful Checkout
 ***********************************************/
function create_custom_invoice_on_checkout( $order_id ) {
    if ( ! $order_id ){
        return;
    }
    $order = wc_get_order( $order_id );
    $order_shipments = array();
 
    /***********************************************
     * Get assigned shipment from order received
    ***********************************************/
    foreach ( $order->get_items() as $item_id => $item ) {
        $meta_value = $item->get_meta( 'Saadetise', true );
        $shipment_id = get_page_by_title( $meta_value, OBJECT, 'wpcargo_shipment')->ID;
        array_push( $order_shipments , $shipment_id );
 
        $rate_id    = get_post_meta( $shipment_id, 'shipment_rate', true );
        $rateInfo   = get_wpcsr_rate_by_id( $rate_id );
    }
 
    $assigned_shipments = implode( ',', $order_shipments);
    $status         = '';
 
    if( $order->get_payment_method() != 'cod' ){
        $status     = 'wpci-paid';
    }else{
        $status     = 'wpci-unpaid';
    }
     
    $invoice_id         = (int)$_POST['invoice_id'];
    $process_type       = 'update';
    $invoice_number = wpcadvinv_generated_invoice_number();
 
    /*************************************************
     * SAVE INVOICE POICE TYPE AS ADVANCE INVOICE
     ************************************************/
    if( !$invoice_id ){
        $invoice_args = array(
            'post_title'    => $invoice_number,
            'post_status'   => 'publish',
            'post_type'     => WPC_ADVANCE_INVOICE_POSTTYPE
        );
        // Insert the post into the database
        $invoice_id         = wp_insert_post( $invoice_args );
        $process_type       = 'add';
    }
 
    if ( is_wp_error( $invoice_id ) ) {
        echo '<div id="message" class="error"><p>' . $invoice_id->get_error_message() . '</p></div>';
        wp_die();
    }
 
    /**********************************************************
     * Assign shipment to invoice
    **********************************************************/
    $old_post_meta      = null;
    $invoice_history    = array();
    if( $process_type == 'update' ){
        $old_post_meta      = get_post_meta( $invoice_id );
        $invoice_history    = $old_post_meta['_invoice_history'];
        unset( $old_post_meta['_invoice_history'] ); // Remove the Invoice history
    }
     
    update_post_meta( $invoice_id, '_invoice_status', $status );
    update_post_meta( $invoice_id, '_wpcadvinv_assigned_shipments', $assigned_shipments );
 
    $assigned_shipments = $assigned_shipments ? explode(',', $assigned_shipments ) : array();
    foreach ( $assigned_shipments as $shipment_id ) {
        if( !(int)$shipment_id ){
            continue;
        }
        update_post_meta( (int)$shipment_id, '_advance_invoice', $invoice_id );
    }
 
    /**********************************************************
     * Save billing fields for the advanced invoice
    *********************************************************/
    $order_data = $order->get_data();
 
    $user_data  = get_userdata( $user_id );
    $first_name = $order_data['billing']['first_name'];
    $last_name  = $order_data['billing']['last_name'];
    $company    = $order_data['billing']['company'];
    $address_1  = $order_data['billing']['address_1'];
    $city       = $order_data['billing']['city'];
    $postcode   = $order_data['billing']['postcode'];
    $email      = $order_data['billing']['email'];
    $phone      = $order_data['billing']['phone'];
 
    update_post_meta( $invoice_id, '_business_name', $company );
    update_post_meta( $invoice_id, '_business_phone', $phone );
    update_post_meta( $invoice_id, '_business_email', $email );
    update_post_meta( $invoice_id, '_business_address', $address_1 );
    update_post_meta( $invoice_id, '_business_city', $city );
    update_post_meta( $invoice_id, '_business_zipcode', $postcode );
 
    /******************************************
     * Send Invoice Email Notification
     * ****************************************/
 
    if( $email != '' ){
        wpcadvinv_invoice_send_mail( $invoice_id );
    }
 
    /********************************************
     * Update Invoice History
     *******************************************/
    do_action( 'wpcadv_invoice_after_data_save', $invoice_id );
    $new_history        = array(
        'date'       => current_time( $wpcargo->date_format ),
        'time'       => current_time( $wpcargo->time_format ),
        'old_data'   => $old_post_meta,
        'new_data'   => get_post_meta( $invoice_id ),
        'updated_by' => wp_get_current_user()->display_name,
        'type'       => $process_type
    );
    // Invoice History
    $invoice_history[]  = $new_history;
    add_post_meta( $invoice_id, '_invoice_history', $invoice_history );
    do_action( 'wpcadv_invoice_after_data_history_save', $invoice_id );
 
     
    /********************************************
     * Download invoice functionality
     *******************************************/
     $invoice_data = wpccustom_generate_invoice_pdf( $invoice_id );
    ?>
 
    <div class="text-center print-shipment">
       <a class="btn btn-primary" href = "<?php echo $invoice_data['file_url']; ?>" download > Download Invoice </a>
    </div>
 
    <?php
}
 
add_action('woocommerce_before_thankyou', 'create_custom_invoice_on_checkout', 10, 1);