Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save warnakey/1d53a525c91429066b98e13856fcbcf4 to your computer and use it in GitHub Desktop.

Select an option

Save warnakey/1d53a525c91429066b98e13856fcbcf4 to your computer and use it in GitHub Desktop.
This code prevents Spam robots from completing orders on WooCommerce if they type the city field as 'dsfsdf' then kicks them off the website
/* You can put this in your footer.php file */
/* This code checks if Spam robots type the City field in the WooCommerce checkout page as 'dsfsdf' and kicks them out */
/* This can be modified to any condition you like by changing the if statements and event listeners around */
<?php if( $post->ID == 2309 ) { ?> // change this to your Checkout page's ID
<script>
// get variables for the different fields in the woocommerce checkout form (note 'billing' vs 'shipping' side)
var cityfield = document.getElementById('billing_city');
var postfield = document.getElementById('billing_postcode');
var emailfield = document.getElementById('billing_email');
var shippingemailfield = document.getElementById('shipping_email');
var billingphone = document.getElementById('billing_phone');
// create an event listener that fires when they start to type in the Zip code field
postfield.addEventListener('focus', (event) => {
// if the robot previously typed the city as 'dsfsdf' start the routine
if (cityfield.value == "dsfsdf") {
// replace whatever they typed in the city field with the following message - so they know you are on to them
document.getElementById("billing_city").value = "Go away. I am sick of you. Please get a life.";
// after they leave the zip code field, continue with the rest of the routine
postfield.addEventListener('blur', (event) => {
// delete some fields, as well as the place order and payment buttons so its impossible to finish ordering
var removeZipField = document.getElementById('billing_postcode');
removeZipField.remove();
var removeEmailField = document.getElementById('billing_email_field');
removeEmailField.remove();
var removePlaceOrder = document.getElementById('place_order');
removePlaceOrder.remove();
var removePayment = document.getElementById('payment');
removePayment.remove();
// just for laughs, let's grab the IP address that the robot was using just to scare them
// this is my API key for ipgeolocation.abstractapi.com
let apiKey = '163265ecf3ac49e0b0b95652529642b4';
// use the API to grab the IP address
jQuery(function($) {
$.getJSON('https://ipgeolocation.abstractapi.com/v1/?api_key=' + apiKey, function(data) {
var ipToBlock = data.ip_address;
// send an alert telling the robot their IP address has been blocked (even though its not really!)
alert("Your IP address " + ipToBlock + " has been blocked.");
// wait 1 second, then redirect the page to an article explaining how they can stop being so annoying
setTimeout(function () {
window.location.href = "https://www.psychologytoday.com/us/blog/valley-girl-brain/201303/6-ways-stop-being-annoying";
}, 1000); //will call the function after 1 second
});
});
});
}
});
<script>
<?php } ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment