Last active
January 23, 2021 17:56
-
-
Save woogist/5692886 to your computer and use it in GitHub Desktop.
Sample WooCommerce Points & Rewards 3rd Party Plugin Integration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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