Skip to content

Instantly share code, notes, and snippets.

@Anatal
Last active January 20, 2022 21:45
Show Gist options
  • Save Anatal/7c3aca05cbd9642b47ee381db980865f to your computer and use it in GitHub Desktop.
Save Anatal/7c3aca05cbd9642b47ee381db980865f to your computer and use it in GitHub Desktop.
Generate leads on checkout page (woocommerce) - call JS one checkout page only
function cart_enqueue() {
wp_register_script('jquery', get_stylesheet_directory_uri() . '/js/jquery-3.6.0.min.js', array(), '3.6.0'); // jQuery
wp_enqueue_script('jquery'); // Enqueue it!
wp_register_script( 'generate-leads-on-checkout', get_stylesheet_directory_uri() . '/js/generate-leads-on-checkout.js', array('jquery'), true );
wp_enqueue_script( 'generate-leads-on-checkout' );
wp_localize_script( 'generate-leads-on-checkout', 'ajax_vars', array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
)
);
}
add_action( 'wp_enqueue_scripts', 'cart_enqueue' );
add_action( 'wp_ajax_cart_data_abandonment', 'cart_data_abandonment_func' ); // If called from admin panel
add_action( 'wp_ajax_nopriv_cart_data_abandonment', 'cart_data_abandonment_func' ); // If called from front end
function cart_data_abandonment_func() {
$cart = WC()->cart;
$cart_total = WC()->cart->total;
if (isset($_POST)){
//user posted variables
$message = json_encode($cart); // All the cart items
$message.= '<br>Cart Total: '.$cart_total;
$message.= '<br>first name: '.$_POST['billing_first_name'];
$message.= '<br>last name: '.$_POST['billing_last_name'];
$message.= '<br>street: '.$_POST['billing_address_1'];
$message.= '<br>city: '.$_POST['billing_city'];
$message.= '<br>phone: '.$_POST['billing_phone'];
$message.= '<br>email: '.$_POST['billing_email'];
$headers = array('Content-Type: text/html; charset=UTF-8');
//php mailer variables
$to = get_option( 'admin_email' );
$subject = "Abandonment of the cart";
//Here put your Validation and send mail
$sent = wp_mail($to, $subject, strip_tags($message), $headers);
if($sent) {
echo 'Cart data abandonment lead:<br>'.$message;
}//message sent!
else {
echo 'Cart data abandonment lead failed!';
}//message wasn't sent
}
die();
}
After a user is inactive on the checkout page, send an email with the user's data to admin email if the user has provided a phone number, email, or both.
The details in the lead to send:
1. Products in cart and quantity of each
2. Cart total
3. Any fields filled by the user
// jQuery fix for Wordpress
jQuery(function ($) {
$(document).on("click", function (event) {
if ($(event.target).closest(".site-main").length === 0) {
// console.log('You clicked outside of the site-main element');
var testEmail = /^[A-Z0-9._%+-]+@([A-Z0-9-]+\.)+[A-Z]{2,4}$/i;
var testPhone = /\(?([0-9]{3})\)?([-]?)([0-9]{3})\2([0-9]{4})/;
if(testEmail.test($('#billing_email').val()) || testPhone.test($('#billing_phone').val())){ // grab the email or the phone number after the field is filled out
$.ajax({
type: 'post',
dataType: 'json',
url: ajax_vars.ajax_url+'?action=cart_data_abandonment',
data: {
billing_first_name : $('#billing_first_name').val(),
billing_last_name : $('#billing_last_name').val(),
billing_address_1 : $('#billing_address_1').val(),
billing_city : $('#billing_city').val(),
billing_phone : $('#billing_phone').val(),
billing_email : $('#billing_email').val()
},
success: function(msg){
console.log(msg);
}
});
}
} else {
// console.log('You clicked inside of the site-main element');
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment