Skip to content

Instantly share code, notes, and snippets.

@JRyven
Created February 17, 2022 18:20
Show Gist options
  • Save JRyven/6337804d687b8c1d6ff70358924ec3ef to your computer and use it in GitHub Desktop.
Save JRyven/6337804d687b8c1d6ff70358924ec3ef to your computer and use it in GitHub Desktop.
WooCommerce Automatically Create and Apply Discount Coupons Based on WordPress User Role
<?php
/* Role_Based_Pricing
*
*
*
*/
/* Role_Based_Pricing_Get_User
*
* Get user ID, Role, Company (slugified)
* Assemble to user coupon code slug (to use or create later)
*/
class Role_Based_Pricing_Get_User {
public $user_id;
public $user_role;
public $user_eligable;
public $user_company;
public $user_email;
public $coupon_code;
/* */
public function __construct(){
$user = $this->rbp_get_user();
$this->user_id = intval($user->data->ID);
$this->user_role = $this->rbp_slugify($user->roles[0]);
$this->user_eligable= $this->rbp_user_eligability_check($this->user_role);
$customer = $this->rbp_get_user_customer();
$this->user_company = $this->rbp_slugify($customer->get_billing_company());
$this->user_email = $customer->get_billing_email();
$this->coupon_code = $this->rbp_coupon_code();
}
/* */
public function rbp_get_user(){
return wp_get_current_user();
}
/* */
public function rbp_get_user_customer(){
return new WC_Customer( $this->user_id );
}
/* */
public function rbp_slugify($text) {
$text = preg_replace('~[^\pL\d]+~u', '', $text);
$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
$text = preg_replace('~[^-\w]+~', '', $text);
$text = trim($text, '');
$text = preg_replace('~-+~', '', $text);
$text = strtolower($text);
return $text;
}
/* */
public function rbp_coupon_code(){
return 'c'.$this->user_role.'-'.$this->user_company.'-'.$this->user_id;
}
/* */
public function rbp_user_eligability_check(){
if( !in_array( $this->user_role, ['administrator', 'retail'] )):
return false;
endif;
return true;
}
}
/* Role_Based_Pricing_Get_Coupons
*
* Gather existing coupons as post [ID => SLUG]
*
*/
class Role_Based_Pricing_Get_Coupons {
public $coupons;
/* */
public function __construct(){
global $wpdb;
$coupons_array = $wpdb->get_results( $wpdb->prepare('
SELECT ID, post_name FROM wp_posts WHERE
post_type = %s AND post_status = %s',
'shop_coupon',
'publish'
), ARRAY_A );
$this->coupons = array_reduce( $coupons_array, function ($result, $item) {
$result[$item['ID']] = $item['post_name'];
return $result;
});
}
}
/* Role_Based_Pricing
*
* Check if user role is eligable
* Check if eligibale user has coupon
*
*/
class Role_Based_Pricing {
private $user;
private $coupons;
private $coupon_code;
/* */
public function rbp_create_coupon( $coupon_code, $user_role, $user_email ){
$discount_type = 'percent';
$amount = '0';
switch ($user_role) {
case 'administrator':
$amount = '90';
break;
default:
$amount = '5';
break;
}
$coupon = array(
'post_title' => $coupon_code,
'post_content' => '',
'post_status' => 'publish',
'post_type' => 'shop_coupon'
);
$cid = wp_insert_post( $coupon );
update_post_meta( $cid, 'discount_type',$discount_type );
update_post_meta( $cid, 'coupon_amount', $amount );
update_post_meta( $cid, 'customer_email', $user_email );
return $cid;
}
/* */
public function rpb_apply_coupon(){
WC()->cart->apply_coupon( $this->user->coupon_code );
wc_print_notices();
}
/* */
public function __construct(){
$this->user = new Role_Based_Pricing_Get_User();
$user_eligable = $this->user->user_eligable;
/* proceed if eligible */
if( $user_eligable ):
$coupon_code = $this->user->coupon_code;
$user_role = $this->user->user_role;
$user_email = $this->user->user_email;
$this->coupons = new Role_Based_Pricing_Get_Coupons();
/* create coupon code if not yet created */
if( !in_array( $coupon_code, $this->coupons->coupons ) ):
$coupon = $this->rbp_create_coupon($coupon_code, $user_role, $user_email);
endif;
/* apply coupon code */
add_action( 'woocommerce_before_cart', [ $this, 'rpb_apply_coupon' ], 999 );
endif;
}
}
/* Call the Class
*
*
*/
add_action('wp_head', 'rbp_activate');
function rbp_activate(){
/* don't bother for logged out users */
if( is_user_logged_in() ){
new Role_Based_Pricing();
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment