Give user free/trial visibility package on registration

After a few request from clients, I have come up with a method that you can automagically give a user a package when they register on your site, which can be used as a way to provide a “free trial” or “free” package initially.  This does require adding some PHP code to your child theme’s functions.php file (or using the Code Snippets plugin).

As of  version 1.2.1+ a feature has been added to “give” a package to user on registration in the product configuration page.  You can then set the product  as hidden, and users won’t be able to select the package from package selection page, but will be given the package on registration.

I have been looking at different ways to integrate this into the plugin for a future update, probably when creating a new visibility package you can select a checkbox that says something like “Hide from cart, and assign to new users on registration”, but if you would like this functionality before I can release this update, here’s some example code to do this:

<?php
add_action( 'user_register', 'give_wpjmp_vis_job_user_package_on_registration' );
add_action( 'user_register', 'give_wpjmp_vis_resume_user_package_on_registration' );
/**
* Give user custom Job Visibility Package on registration
*
*
* @since @@version
*
* @param $user_id
*/
function give_wpjmp_vis_job_user_package_on_registration( $user_id ) {
global $wpdb;
if ( wpjmp_give_pack_check_user_role( 'employer', $user_id ) ) {
$wpdb->insert(
"{$wpdb->prefix}wpjmpack_user_packages",
array(
'user_id' => $user_id,
'product_id' => 0, // This should be set to the ID of a package in WooCommerce if you want it to show a package name!
'package_type' => 'job_listing', // job_listing for job packages, resume for resume packages
'handler' => 'woocommerce', // !!! DO NOT CHANGE THIS VALUE !!!
// Browse is either enabled or disabled (no limits exist for browse)
'allow_browse' => 1, // 1 for enable - 0 for disable
// Apply can have a limit if specified, otherwise set to zero (0) for unlimited
'allow_apply' => 1, // 1 for enable - 0 for disable
'apply_limit' => 5, // 0 for unlimited, this example sets it to max of 5 applications before requiring new package
// View can have a limit if specified, otherwise set to zero (0) for unlimited
'allow_view' => 1, // 1 for enable - 0 for disable
'view_limit' => 0, // 0 for unlimited
)
);
}
}
/**
* Give user custom Resume Visibility Package on registration
*
*
* @since @@version
*
* @param $user_id
*/
function give_wpjmp_vis_resume_user_package_on_registration( $user_id ) {
global $wpdb;
if ( wpjmp_give_pack_check_user_role( 'candidate', $user_id ) ) {
$wpdb->insert(
"{$wpdb->prefix}wpjmpack_user_packages",
array(
'user_id' => $user_id,
'product_id' => 0, // This should be set to the ID of a package in WooCommerce if you want it to show a package name!
'package_type' => 'resume', // job_listing for job packages, resume for resume packages
'handler' => 'woocommerce', // !!! DO NOT CHANGE THIS VALUE !!!
// Browse is either enabled or disabled (no limits exist for browse)
'allow_browse' => 1, // 1 for enable - 0 for disable
// Apply can have a limit if specified, otherwise set to zero (0) for unlimited
'allow_contact' => 1, // 1 for enable - 0 for disable
'contact_limit' => 0, // 0 for unlimited
// View can have a limit if specified, otherwise set to zero (0) for unlimited
'allow_view' => 1, // 1 for enable - 0 for disable
'view_limit' => 5, // 0 for unlimited, this example sets it to max of 5 resume views before requiring new package
// View name can have a limit if specified, otherwise set to zero (0) for unlimited
'allow_view_name' => 1, // 1 for enable - 0 for disable
'view_name_limit' => 0, // 0 for unlimited
)
);
}
}
/**
* Check if user has specific role
*
*
* @since @@version
*
* @param $role
* @param null $user_id
*
* @return bool
*/
function wpjmp_give_pack_check_user_role( $role, $user_id = null ) {
if ( is_numeric( $user_id ) ) {
$user = get_userdata( $user_id );
} else {
$user = wp_get_current_user();
}
if ( empty( $user ) ) {
return false;
}
return in_array( $role, (array) $user->roles );
}

You will notice there are two actions, one for Job and one for Resumes … if you only need this for Job packages, or only for Resume packages, you can omit the add_action and the associated function (do not omit the function to check user roles).  As an example, if you only needed this for Job Packages, you would use this code instead:

<?php
add_action( 'user_register', 'give_wpjmp_vis_job_user_package_on_registration' );
/**
* Give user custom Job Visibility Package on registration
*
*
* @since @@version
*
* @param $user_id
*/
function give_wpjmp_vis_job_user_package_on_registration( $user_id ) {
global $wpdb;
// This checks that only users with a role of "employer" will get this package
if ( wpjmp_give_pack_check_user_role( 'employer', $user_id ) ) {
$wpdb->insert(
"{$wpdb->prefix}wpjmpack_user_packages",
array(
'user_id' => $user_id,
'product_id' => 0, // This should be set to the ID of a package in WooCommerce if you want it to show a package name!
'package_type' => 'job_listing', // job_listing for job packages, resume for resume packages
'handler' => 'woocommerce', // !!! DO NOT CHANGE THIS VALUE !!!
// Browse is either enabled or disabled (no limits exist for browse)
'allow_browse' => 1, // 1 for enable - 0 for disable
// Apply can have a limit if specified, otherwise set to zero (0) for unlimited
'allow_apply' => 1, // 1 for enable - 0 for disable
'apply_limit' => 5, // 0 for unlimited, this example sets it to max of 5 applications before requiring new package
// View can have a limit if specified, otherwise set to zero (0) for unlimited
'allow_view' => 1, // 1 for enable - 0 for disable
'view_limit' => 0, // 0 for unlimited
)
);
}
}
/**
* Check if user has specific role
*
*
* @since @@version
*
* @param $role
* @param null $user_id
*
* @return bool
*/
function wpjmp_give_pack_check_user_role( $role, $user_id = null ) {
if ( is_numeric( $user_id ) ) {
$user = get_userdata( $user_id );
} else {
$user = wp_get_current_user();
}
if ( empty( $user ) ) {
return false;
}
return in_array( $role, (array) $user->roles );
}

You will also notice in the code that there is a check for the User Role before assigning a package.  You can change this to any specific user role you want, but in these examples the user role of employer is used for Job Visibility packages, and candidate for Resume Visibility Packages.

Total 0 Votes

Tell us how can we improve this post?

+ = Verify Human or Spambot ?