Skip to content

Instantly share code, notes, and snippets.

@MCKLtech
Last active January 24, 2022 21:20
Show Gist options
  • Save MCKLtech/addccffd32b4061a011bf781850f577c to your computer and use it in GitHub Desktop.
Save MCKLtech/addccffd32b4061a011bf781850f577c to your computer and use it in GitHub Desktop.
A basic blocker for WooCommerce that works on Groundhogg.io Tags
<?php
/*
* Plugin Name: WooCommerce Customer Blocker for Groundhogg
* Plugin URI:
* Description: Block customers at the checkout using tags in Groundhogg
* Version: 1.0
* Author: Colin Longworth
* Author URI: http://www.wooninja.io
* Developer: Colin Longworth
* Developer URI: http://www.wooninja.io
* Text Domain: woocommerce-groundhogg-blocker
* License: GPLv2
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2, as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
if (!defined('ABSPATH')) {
exit;
}
use function Groundhogg\get_contactdata;
use function Groundhogg\is_a_contact;
class WooCommerce_Groundhogg_Blocker
{
const WC_GH_BLOCK_TAG = "Banned";
public function __construct()
{
add_action('woocommerce_after_checkout_validation', [$this, 'check_for_block_tags'], 10, 2);
}
/**
* Validate the email at the checkout stage
*
* @param $fields
* @param $errors
*/
public function check_for_block_tags($fields, $errors)
{
$banned = false;
/**
* Check if logged in first
*/
if (is_user_logged_in()) {
$wp_user = wp_get_current_user();
if ($this->validate_email($wp_user->user_email)) $banned = true;
}
/**
* Check the billing email to avoid a logged in user avoiding the block
*/
if (isset($fields['billing_email'])) {
if ($this->validate_email($fields['billing_email']))$banned = true;
}
if($banned) $errors->add('validation', 'Please contact us by phone to place this order');
}
/**
* Check if the given email is a GH contact AND if they are tagged to be blocked
*
* @param $email
* @return bool
*/
private function validate_email($email)
{
$contact = get_contactdata($email);
if (is_a_contact($contact) && $contact->has_tag(self::WC_GH_BLOCK_TAG)) return true;
return false;
}
}
new WooCommerce_Groundhogg_Blocker();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment