Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Programmer095/2cd1618687e002ef8a6a1b4debab53ec to your computer and use it in GitHub Desktop.
Save Programmer095/2cd1618687e002ef8a6a1b4debab53ec to your computer and use it in GitHub Desktop.
Import myCRED Points to users with WP All Import

This code and procedure are provided in the hope that they're useful, but without support and without any warranty expressed or implied

The code below needs to go in something like https://wordpress.org/plugins/code-snippets/ or your theme's functions.php file ( example code, adjust if needed ):

add_filter( 'mycred_setup_hooks', 'mycredpro_adjust_hooks', 10, 2 );
function mycredpro_adjust_hooks( $installed, $point_type ) {

	// Add a custom hook
	$installed['wpai'] = array(
		'title'        => 'WP All Import Import',
		'description'  => 'Add points as specified when importing',
		'callback'     => array( 'wpai_myCRED_Hook' )
	);

	return $installed;

}

add_action( 'mycred_load_hooks', 'mycredpro_load_custom_hook' );
function mycredpro_load_custom_hook() {

	// Require file containing the class or
	// define the class in this function
	class wpai_myCRED_Hook extends myCRED_Hook {

		/**
	 	* Construct
	 	*/
		function __construct( $hook_prefs, $type ) {
			parent::__construct( array(
				'id'       => 'wpai',
				'defaults' => array(
					'creds'   => 1,
					'log'     => '%plural% added via WP All Import'
				)
			), $hook_prefs, $type );
		}

		/**
		 * Hook into WordPress
		 */
		public function run() {
			// Since we are running a single instance, we do not need to check
			// if points are set to zero (disable). myCRED will check if this
			// hook has been enabled before calling this method so no need to check
			// that either.
			//add_action( 'personal_options_update',  array( $this, 'profile_update' ) );
			//add_action( 'edit_user_profile_update', array( $this, 'profile_update' ) );

			add_action('pmxi_saved_post', array( $this, 'profile_update') );

		}

		/**
	 	* Check if the user qualifies for points
	 	*/
		public function profile_update( $user_id ) {
			// Check if user is excluded (required)
			if ( $this->core->exclude_user( $user_id ) ) return;
			
			$creds = get_user_meta( $user_id, '_my_creds', true);
			
			if( $creds ){
				// Execute
				$this->core->add_creds(
					'wpai',
					$user_id,
					$creds,
					$this->prefs['log'],
					0,
					'',
					$m
				);
			}

			// Check to see if user has filled in their first and last name
			//if ( empty( $_POST['first_name'] ) || empty( $_POST['last_name'] ) ) return;

			// Make sure this is a unique event
			//if ( $this->has_entry( 'completing_profile', '', $user_id ) ) return;

			
		}
	}

}

Then you need to activate the new hook under Points->hooks: https://d.pr/i/CWCfSe

Next, prepare a new 'Existing Items' import for either Users or Customers ( as appropriate ): https://d.pr/i/eOMbOK

On Step 3, enter placeholder data for username and email if you're not updating it also: https://d.pr/i/kGZLsl

Also on Step 3, add a Custom Field called '_my_creds' and set the value to the number of points/creds to add: https://d.pr/i/Vzz2lg

On Step 4, tell WP All Import how to match your existing users/customers and limit what WP All Import updates: https://d.pr/i/bwe0ad

Now run the import and you'll see that points have been added to each user/customer that WP All Import was able to update.

Note: if you run the same import again the points will be added to every user again.

References

https://codex.mycred.me/chapter-v/hook-api/

https://www.mycred.me/tutorials/how-to-make-your-custom-hook/

@dlupus10
Copy link

How can I make it add different log entries from the csv?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment