Skip to main content

How to create custom guest popup for checkout with wordpress menu in wordpress.

Create custom guest popup for checkout with wordpress menu in wordpress.

Paste in function.php

function override_checkout_email_field( $fields ) {
global $wpdb;
  $popup_id = $_COOKIE['popup_id'];
global $wpdb;
$tablename = $wpdb->prefix . "guest_users";
$result = $wpdb->get_results ("SELECT * FROM $tablename WHERE id=$popup_id");
foreach ( $result as $print )   {

}
$survey_email = $print->email;
$survey_phone = $print->phone;
    global $woocommerce;
    //$survey_email = $woocommerce->session->get('survey_email');
    if(!is_null($survey_email)) {
      $fields['billing']['billing_email']['default'] = $survey_email;
    }
     if(!is_null($survey_phone)) {
      $fields['billing']['billing_phone']['default'] = $survey_phone;
    } 
    return $fields;
}

add_filter( 'woocommerce_checkout_fields' , 'override_checkout_email_field' );


// Show the popup to fetch email and phone number and store in the database




function guest_popup(){
$popup_id = $_COOKIE['popup_id'];
if(isset($_POST['guest_popup_bt'])){
$email = $_POST['email'];
$phone = $_POST['phone'];
date_default_timezone_set('Asia/Kolkata');
$date = date('d-m-Y h:i:s a');
global $wpdb;
$table_name = $wpdb->prefix . "guest_users";
$wpdb->insert($table_name, array('email' => $email, 'phone' => $phone, 'created_at' => $date) ); 
$lastid = $wpdb->insert_id;
if(!empty($lastid)){
//setcookie('popup_id', $lastid, strtotime('+1 day'), COOKIEPATH, COOKIE_DOMAIN);
setcookie('popup_id', $lastid, strtotime("+1 year"), COOKIEPATH, COOKIE_DOMAIN);
if ( ! WC()->cart->get_cart_contents_count() == 0 ) { 
wp_redirect(site_url().'/checkout');
}else{
wp_redirect(site_url().'/shop');
}
}
}
?>
<?php
if(!is_user_logged_in() && is_cart() && empty($popup_id)){
?>
<style>
.popup-container {
    width: 100%;
    height: 100vh;
    background-color: #000000bf;
    position: fixed;
    display: flex;
    justify-content: Center;
    align-items: center;
    top: 0;
    right: 0;
    bottom: 0;
    z-index: 9999999;
    left: 0;
}

.popup-box input[type="email"],input[type="number"] {
    width: 100%;
    font-size: 15px;
    padding: 7px 7px;
}
input.guest_popup_bt {
    padding: 5px 16px;
    background: #000;
    border: #000;
    color: white;
    font-weight: 400;
    font-size: 16px;
}
.form-filed-box {
    margin-top: 10px;
}
.popup-box h4 {
    margin: 0;
    font-size: 17px;
    text-align: center;
    font-weight: 400;
    letter-spacing: 1px;
    margin-bottom: 12px;
}
.popup-box {
    padding: 59px 37px;
    background: white;
    border-radius: 9px;
    width: 386px;
position:relative;
}

.popup_close_bt {
    background: #000;
    color: White;
    position: absolute;
    right: -15px;
    top: -15px;
    border-radius: 200%;
    width: 30px;
    height: 30px;
    text-align: center;
    display: flex;
    justify-content: center;
    align-items: center;
cursor:pointer;
}
</style>
<script>
jQuery(document).ready(function($){
$('.popup_close_bt').click(function(){
$('.popup-container').css('display','none');
});

if (typeof $.cookie('popup_id') === 'undefined'){

$('.checkout-button').click(function(e){
e.preventDefault;
jQuery('.popup-container').css('display','flex');
return false;
});
}
});
</script>
<div class="popup-container">
<div class="popup-box">
<div class="popup_close_bt">X</div>
<form action="" method="post">
<h4>Please enter the following detail to move forward</h4>
<div class="form-filed-box">
<label>Email</label>
<input type="email" name="email" >
</div>
<div class="form-filed-box">
<label>Phone no.</label>
<input type="text" name="phone" >
</div>
<div class="form-filed-box">
<input type="submit" name="guest_popup_bt" class="guest_popup_bt" value="Submit">
</div>
</form>
</div>
</div>
<?php
}
}
add_action('wp_head','guest_popup');


add_action("admin_menu", "addMenu");

function addMenu() {
    add_menu_page("Guest Info", "Guest Info", "edit_posts",
        "guest-popup", "displayPage", null,10);
}

add_filter("custom_menu_order", "allowMenuStructure");
add_filter("menu_order", "loadMenuStructure");

function displayPage() {
?>
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.12/css/jquery.dataTables.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/buttons/1.2.2/css/buttons.dataTables.min.css">



<script src="https://code.jquery.com/jquery-1.12.3.js"></script>
<script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/buttons/1.2.2/js/dataTables.buttons.min.js"></script>
<script src="https://cdn.datatables.net/buttons/1.2.2/js/buttons.flash.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/2.5.0/jszip.min.js"></script>
<script src="https://cdn.rawgit.com/bpampuch/pdfmake/0.1.18/build/pdfmake.min.js"></script>
<script src="https://cdn.rawgit.com/bpampuch/pdfmake/0.1.18/build/vfs_fonts.js"></script>
<script src="https://cdn.datatables.net/buttons/1.2.2/js/buttons.html5.min.js"></script>
<script src="https://cdn.datatables.net/buttons/1.2.2/js/buttons.print.min.js"></script>  



<script>

$(document).ready(function() {
    $('#example').DataTable( {
      /*   dom: 'Bfrtip',
        buttons: [
            'copy', 'excelFlash', 'excel', 'pdf', 'print',{
            text: 'Reload',
            action: function ( e, dt, node, config ) {
                dt.ajax.reload();
            }
        }
        ] */
    } );
} );

</script>


<table id="example" class="display nowrap" cellspacing="0" width="100%">
        <thead>
            <tr>
                <th>S.r</th>
                <th>Date</th>
                <th>Email</th>
                <th>Phone</th>
            </tr>
        </thead>
        <tfoot>
            <tr>
                <th>S.r</th>
                <th>Date</th>
                <th>Email</th>
                <th>Phone</th>
            </tr>
        </tfoot>
        <tbody>
<?php
global $wpdb;
$tablename = $wpdb->prefix . "guest_users";
$result = $wpdb->get_results ("SELECT * FROM $tablename order by Id DESC");
//print_r($result);
$i = 1 ;
foreach ( $result as $print )   {
?>
            <tr>
                <td><?php echo $i; ?></td>
                <td><?php echo $print->created_at; ?></td>
                <td><a href="mailto:<?php echo $print->email; ?>"><?php echo $print->email; ?></a></td>
                <td><a href="tel:<?php echo $print->phone; ?>"><?php echo $print->phone; ?></a></td>

            </tr>
<?php
$i++;
}
?>
        </tbody>
    </table>
<?php
}

Comments

Popular posts from this blog

How to check date format in PHP?

Check date format in PHP function isCorrectDateFromat($date){     if(!empty($date)){         $dateString = $date; // Replace this with your date string         $format = "Y-m-d"; // Replace this with your expected date format         $dateTime = DateTime::createFromFormat($format, $dateString);         if ($dateTime === false) { /*             echo "The date is not in the correct format."; */         } else {             $errors = DateTime::getLastErrors();             if (empty($errors)) { /*                 echo "The date is in the correct format."; */                 return true;             } else { /*                 echo "...