lisaleague
9/10/2015 - 3:11 AM

Attempt to connect Completing a Sensei LESSON to points and rewards

Attempt to connect Completing a Sensei LESSON to points and rewards

<?php
// THIS IS THE FILE FROM https://gist.github.com/woogist/5692886#file-gistfile1-php//
//  ...and, believe it or not, I understand about 50% of this.//
//  While that's better than the average Joe, it' still not enough to actually CONNECT the points //
// Anyone want to jump in here? //

// Add the action setting
add_filter( 'wc_points_rewards_action_settings', 'bkg_sensei_points' );

function points_rewards_newsletter_action_settings( $settings ) {
  
	$settings[] = array(
		'title'    => __( 'Points earned for completing lesson' ),
		'desc_tip' => __( 'Enter the amount of points earned when a customer finishes a Sensei Lesson.' ),
		'id'       => 'bkg_sensei_points',
	);

	return $settings;
}

// add the event descriptions
add_filter( 'wc_points_rewards_event_description', 'one_of_those_magical_hook_things_goes_here', 10, 3 );

function one_of_those_magical_hook_things_goes_here( $event_description, $event_type, $event ) {

	$points_label = get_option( 'wc_points_rewards_points_label' );

	// set the description if we know the type
	switch ( $event_type ) {
		case 'magical_underlined_thing': $event_description = sprintf( __( '%s earned for newsletter signup' ), $points_label ); break;
	}

	return $event_description;
}

// perform the event (of course this depends on your particular plugin/action)
add_action( 'wp_mailchimp_new_newsletter_signup', 'yet_another_underscore_thingy' );

function yet_another_underscore_thingy( $lesson_id ) {

	// can't give points to a user who isn't logged in
	if ( ! is_user_logged_in() )
		return;

	// get the points configured for this custom action
	$points = get_option( 'wc_points_rewards_mailchimp_newsletter_signup' );

	if ( ! empty( $points ) ) {
		// arbitrary data can be passed in with the points change, this will be persisted to the points event log
		$data = array( 'lesson_id' => $lesson_id );
		  
		WC_Points_Rewards_Manager::increase_points( get_current_user_id(), $points, 'magical_underlined_thing', $data );
	}
}