“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.
<?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' => $wpcargo->user_fullname( get_current_user_id() ),
'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);