Essentials WooCommerce Custom Functions
<?php
/**
* Confirmation
*
* @see agrotech_confirmation_notification()
*/
add_action( 'woocommerce_order_details_after_order_table', 'agrotech_confirmation_notification', 10 );
/**
* Order Email - Confirmation
*
* @see agrotech_confirmation_notification_email()
*/
add_action( 'woocommerce_email_after_order_table', 'agrotech_confirmation_notification_email', 10 );
/**
* Payment Confirmation Form
*
* @see agrotech_confirmation_form()
*/
add_action( 'agrotech_page_confirmation', 'agrotech_confirmation_form', 10 );
<?php
if ( ! function_exists( 'agrotech_email_payment' ) ) {
/**
* Send Notification Email to Admin
*
* @param array $data
* @param string $receipt_url
* @since 1.0.0
*/
function agrotech_email_payment($data, $receipt_url) {
$to = get_option('info_email');
$subject = '[Payment Confirmation] Order #' . $data['order_id'];
$message = 'Order ID: ' . $data['order_id'] . "\r\n";
$message .= 'Account Name: ' . $data['account_name'] . "\r\n";
$message .= 'Order Email: ' . $data['order_email'] . "\r\n";
$message .= 'Transfer Bank: ' . $data['transfer_bank'] . "\r\n";
$message .= 'Transfer\'s Date: ' . $data['transfer_date'] . "\r\n";
$message .= 'Transfer\'s Amount: ' . $data['transfer_amount'] . "\r\n";
$message .= 'Notes: ' . $data['notes'];
$headers = 'From: Prima Agro Tech <no-reply@agrotech.id>' . "\r\n";
$attachments = array( $receipt_url );
echo '<pre>';
var_dump($subject);
var_dump($message);
var_dump($attachments);
echo '</pre>';
// Send email notification
wp_mail( $to, $subject, $message, $headers, $attachments );
}
}
if ( ! function_exists( 'agrotech_save_payment' ) ) {
/**
* Display the payment confirmation notification on checkout
*
* @param array $data
* @param file $files
* @since 1.0.0
*/
function agrotech_save_payment($data, $files) {
// Payment Title
$post_title = 'Payment Order ' . $data['order_id'];
// Payment Data
$payment = array(
'post_title' => $post_title,
'post_content' => $data['notes'],
'post_author' => 1,
'post_type' => 'agro_payment',
'post_status' => 'publish',
);
// Insert Payment
$post_id = wp_insert_post( $payment, true );
var_dump($post_id);
// Order ID
update_field('order_id', $data['order_id'], $post_id);
// Account Name
update_field('account_name', $data['account_name'], $post_id);
// Order Email
update_field('order_email', $data['order_email'], $post_id);
// Transfer Bank
update_field('transfer_bank', $data['transfer_bank'], $post_id);
// Transfer Date
update_field('transfer_date', $data['transfer_date'], $post_id);
// Transfer Amount
update_field('transfer_amount', $data['transfer_amount'], $post_id);
// Save Receipt
$file = $_FILES['transfer_receipt'];
agrotech_save_payment_receipt( $post_id, $file );
// Redirect
wp_redirect( '/konfirmasi/?status=success' );
}
}
if ( ! function_exists( 'agrotech_save_payment_receipt' ) ) {
/**
* Display the payment confirmation notification on checkout
*
* @param int $post_id
* @param file $file
* @since 1.0.0
*/
function agrotech_save_payment_receipt($post_id, $file) {
if ( ! function_exists( 'wp_handle_upload' ) ) {
require_once( ABSPATH . 'wp-admin/includes/file.php' );
}
$uploadedfile = $file;
$upload_overrides = array( 'test_form' => false );
$movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
if ( $movefile && !isset( $movefile['error'] ) ) {
// echo "File is valid, and was successfully uploaded.\n";
// var_dump( $movefile);
// $filename should be the path to a file in the upload directory.
$filename = $movefile['file'];
// The ID of the post this attachment is for.
$parent_post_id = $post_id;
// Check the type of file. We'll use this as the 'post_mime_type'.
$filetype = wp_check_filetype( basename( $filename ), null );
// Get the path to the upload directory.
$wp_upload_dir = wp_upload_dir();
// Prepare an array of post data for the attachment.
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ),
'post_mime_type' => $filetype['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
'post_content' => '',
'post_status' => 'inherit'
);
// Insert the attachment.
$attach_id = wp_insert_attachment( $attachment, $filename, $parent_post_id );
// Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
require_once( ABSPATH . 'wp-admin/includes/image.php' );
// Generate the metadata for the attachment, and update the database record.
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );
// Update File Field
update_field('transfer_receipt', $attach_id, $post_id);
} else {
/**
* Error generated by _wp_handle_upload()
* @see _wp_handle_upload() in wp-admin/includes/file.php
*/
// echo $movefile['error'];
}
}
}
if ( ! function_exists( 'agrotech_confirmation_form' ) ) {
/**
* Display the payment confirmation notification on checkout
*
* @since 1.0.0
*/
function agrotech_confirmation_form($order) { ?>
<?php
$today = date('d-m-Y');
$language_domain = 'agrotech';
$order_id = '';
$account_name = '';
$order_email = '';
$transfer_amount = '';
if ( isset($order) && $order != '') {
$order_id = $order->id;
$account_name = $order->billing_first_name . ' ' . $order->billing_last_name;
$order_email = $order->billing_email;
$transfer_amount = $order->order_total;
}
?>
<!-- Agrotech Payment Confirmation Form -->
<form method="post" action="" class="form payment-confirmation-form" id="agrotech-payment-confirmation-form" enctype="multipart/form-data">
<div class="form-group">
<label for="confirmation-order-id"><?php _e( 'Order ID', $language_domain ); ?></label>
<input type="text" name="atpc_data[order_id]" class="input form-control" id="confirmation-order-id" value="<?php echo $order_id; ?>" aria-required="true" required>
</div>
<div class="form-group">
<label for="confirmation-account-name"><?php _e( 'Account Name', $language_domain ); ?></label>
<input type="text" name="atpc_data[account_name]" class="input form-control" id="confirmation-account-name" value="<?php echo $account_name; ?>" aria-required="true" required>
</div>
<div class="form-group">
<label for="confirmation-email"><?php _e( 'Email', $language_domain ); ?></label>
<input type="email" name="atpc_data[order_email]" class="input form-control" id="confirmation-email" value="<?php echo $order_email; ?>" aria-required="true" required>
</div>
<div class="form-group">
<label for="confirmation-transfer-bank"><?php _e( 'Transfer to', $language_domain ); ?></label>
<select name="atpc_data[transfer_bank]" class="select form-control" id="confirmation-transfer-bank" aria-required="true" required>
<option value="BCA">BCA</option>
</select>
</div>
<div class="form-group">
<label for="confirmation-transfer-date"><?php _e( 'Transfer Date', $language_domain ); ?></label>
<input type="text" name="atpc_data[transfer_date]" class="input form-control has-datepicker" id="confirmation-transfer-date" value="<?php echo $today; ?>" aria-required="true" required>
</div>
<div class="form-group">
<label for="confirmation-transfer-amount"><?php _e( 'Transfer Amount', $language_domain ); ?></label>
<input type="text" name="atpc_data[transfer_amount]" class="input form-control" id="confirmation-transfer-amount" value="<?php echo $transfer_amount; ?>" aria-required="true" required>
</div>
<div class="form-group">
<label for="confirmation-transfer-receipt"><?php _e( 'Transfer Receipt', $language_domain ); ?></label>
<input type="file" name="transfer_receipt" class="input form-control" id="confirmation-transfer-receipt" aria-required="true" required>
</div>
<div class="form-group">
<label for="confirmation-notes"><?php _e( 'Notes', $language_domain ); ?></label>
<textarea type="file" name="atpc_data[notes]" class="input form-control" id="confirmation-notes" aria-required="false"></textarea>
</div>
<button type="submit" class="btn btn-lg btn-default"><?php _e( 'Send', $language_domain ); ?></button>
</form>
<?php
}
}
if ( ! function_exists( 'agrotech_confirmation_notification' ) ) {
/**
* Display the payment confirmation notification on checkout
*
* @since 1.0.0
*/
function agrotech_confirmation_notification($order) { ?>
<div class="payment-notification" id="agrotech-payment-confirmation-notification">
<?php
$format = __('Once you make the payment, please confirm at <a href="%s?order_id=%d" class="payment-notification__link">Payment Confirmation »</a>', 'agrotech' );
$link = get_page_link( 439 );
echo sprintf($format, $link, $order->id);
?>
</div>
<?php
}
}
if ( ! function_exists( 'agrotech_confirmation_notification_email' ) ) {
/**
* Display the payment confirmation notification on checkout
*
* @since 1.0.0
*/
function agrotech_confirmation_notification_email($order) { ?>
<?php
$text = __('Once you make your payment, please confirm at link below', 'agrotech' );
$format = __('<a href="%s?order_id=%d" class="button">Payment Confirmation »</a>', 'agrotech' );
$link = get_page_link( 439 );
$button = sprintf($format, $link, $order->id);
?>
<table cellspacing="0" cellpadding="0" style="margin:30px 0;width:100%;text-align:center">
<tbody>
<tr><td style="padding:12px"><?php echo $text; ?></td></tr>
<tr><td style="padding-top:20px;padding:12px"><?php echo $button; ?></td></tr>
</tbody>
</table>
<?php
}
}
if ( ! function_exists( 'agrotech_switch_languages' ) ) {
/**
* Display the switch language button
*
* @since 1.0.0
**/
function agrotech_switch_languages() {
?>
<div class="language-switcher">
<div class="nav navbar-nav">
<div class="dropdown">
<button class="language-button" id="dLabel" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" title="<?php _e( 'Languages', 'agrotech' ); ?>">
<!-- 1. This code show current language -->
<?php if (qtranxf_getLanguage()=='en') : ?>
<img src="<?php echo get_stylesheet_directory_uri(); ?>/assets/images/gb.png">
<?php elseif (qtranxf_getLanguage()=='id') : ?>
<img src="<?php echo get_stylesheet_directory_uri(); ?>/assets/images/id.png">
<?php endif ?>
<!-- /1. -->
<span class="caret"></span>
</button>
<div class="dropdown-menu language-dropdown" aria-labelledby="dLabel">
<!-- 2. This code show qTranslate-X widget -->
<?php echo qtranxf_generateLanguageSelectCode('image'); ?>
<!-- /2. -->
</div><!-- /dropdown-menu -->
</div><!-- /dropdown -->
</div><!-- /nav -->
</div>
<?php
}
}
<?php
if ($_POST) {
$data = $_POST['atpc_data'];
$files = $_FILES['transfer_receipt'];
if ( function_exists('agrotech_save_payment') ) {
agrotech_save_payment( $data, $files );
}
}
?>
<?php get_header(); ?>
<div id="primary" class="content-area content-full-width">
<main id="main" class="site-main" role="main">
<?php while ( have_posts() ) : the_post();
get_template_part( 'content', 'page' );
endwhile; // End of the loop. ?>
<div class="page-content page-content-wide">
<?php get_template_part( 'content', 'confirmation' ); ?>
</div><!-- .page-content -->
</main><!-- #main -->
</div><!-- #primary -->
<?php get_footer(); ?>
<?php
$order = '';
if ($_GET['order_id']) {
$order = new WC_Order( $_GET['order_id'] );
}
?>
<?php if ( isset($_GET['status']) && $_GET['status'] == 'success' ): ?>
<div class="callout callout-success">
<h4>Thank You</h4>
<p>We will check your payment confirmation and continue processing your order</p>
</div>
<?php endif; ?>
<?php do_action( 'agrotech_page_confirmation', $order ); ?>