Skip to content

Instantly share code, notes, and snippets.

@seb86
Forked from woogist/gist:5692886
Created August 21, 2017 13:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seb86/a5446e0608e79e66b42b569cdcad9925 to your computer and use it in GitHub Desktop.
Save seb86/a5446e0608e79e66b42b569cdcad9925 to your computer and use it in GitHub Desktop.
Sample WooCommerce Points & Rewards 3rd Party Plugin Integration
<?php
// Add the action setting
add_filter( 'wc_points_rewards_action_settings', 'points_rewards_newsletter_action_settings' );
function points_rewards_newsletter_action_settings( $settings ) {
$settings[] = array(
'title' => __( 'Points earned for newsletter signup' ),
'desc_tip' => __( 'Enter the amount of points earned when a customer signs up for a newsletter via MailChimp.' ),
'id' => 'wc_points_rewards_mailchimp_newsletter_signup',
);
return $settings;
}
// add the event descriptions
add_filter( 'wc_points_rewards_event_description', 'add_points_rewards_newsletter_action_event_description', 10, 3 );
function add_points_rewards_newsletter_action_event_description( $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 'mailchimp-newsletter-signup': $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', 'points_rewards_newsletter_signup_action' );
function points_rewards_newsletter_signup_action( $newsletter_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( 'newsletter_id' => $newsletter_id );
WC_Points_Rewards_Manager::increase_points( get_current_user_id(), $points, 'mailchimp-newsletter-signup', $data );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment