Yith Points and Rewards send email

Send an email with accumelated points from Yith Points and rewards plugin.

add to functions.php:

add_action( 'show_user_profile', 'extra_user_profile_fields' );
add_action( 'edit_user_profile', 'extra_user_profile_fields' );

function extra_user_profile_fields( $user ) { 
    $checked = get_user_meta($user->ID, 'spaarpunten', true);
    
    ?>
    <h3>Extra profile information</h3>
    <table class="form-table">
    <tr>
        <th><label for="premium">Spaarpunten</label></th>
        <td>
            <input type="checkbox" id="premium" name="premium"
            <?php if($checked){ ?> 
                    checked="checked" 
            <?php } ?>
            >
            <span class="description">Vink aan om spaarpuntenmail te versturen.</span>
        </td>
    </tr>
    </table>
<?php 
}

add_action( 'personal_options_update', 'save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_user_profile_fields' );

function save_extra_user_profile_fields( $user_id ) {
    if ( !current_user_can( 'edit_user', $user_id ) ) { 
        return false; 
    }
    if (isset($_POST['premium'])){
        update_user_meta( $user_id, 'spaarpunten', 1 );
    }
    else {
        update_user_meta( $user_id, 'spaarpunten', 0 );
        
    }
}

add_action( 'user_register', 'save_extra_profile_fields', 10, 1 );
function save_extra_profile_fields(){
  update_user_meta($user_id, 'spaarpunten',1);
}

Create a new post or page, add content to be sent to the user. You can use {punten} for points {voornaam} for Firstname and {korting} for discount in Euro.

Create a new php file in the root of the domain, for example “puntenmail.php” and add the following code, change from to your own needs and replace page ID with your page ID created in the previous step.

<?php
include('wp-load.php');
if ($_GET['key']=='kjsdvajkfsljoidfkjfdskldfjkfdhuiaioasjdiajdjakjhiuvbsdnak[ask8547ahjsdjasko'){
 user_role_check();
}
elseif ($_GET['action']=='uitschrijven' && !empty($_GET['email'])){
  
  $emailhash = hash('ripemd160', $_GET['email']);
  if ($_GET['key']==$emailhash){
    
  	$user = get_user_by('email', $_GET['email']);
  	if($user){
      
		echo '<h1><center>'.$_GET['email'].' ontvangt geen puntennotificaties meer</center></h1>';
      update_user_meta( $user->ID, 'spaarpunten', 0 );   	
  	}
    else{
    	echo 'De user is niet gevonden, neem contact op met info@annadee.nl om je uit te schrijven';
    }
  }
  else{
   	echo 'Deze actie is niet mogelijk, neem contact op met info@annadee.nl om je uit te schrijven'; 
  }
  
}
else{
  	echo "NO KEY";
	die;
}


function mail_to_subscribe($punten,$email,$Sitivoornaam){
$admin_email = get_option( 'admin_email' );
  	$to = $email;
    $prijs = wc_price($punten);
    if($punten > 1){
        $strpunt = "punten"; 
        $is_ww = 'zijn';
    }else{ 
        $strpunt = "punt"; 
        $is_ww = 'is';
    }
    $subject = strip_tags('Beste '.$Sitivoornaam. ', deze €'.$prijs .' is voor jou!');
    $puntenstring = $punten ." ". $strpunt;

    $headers = array('Content-Type: text/html; charset=UTF-8');
  	// change FROM to your own needs
    $headers[] = 'From: '.get_bloginfo( 'name' ).' '.$admin_email;
    ob_start();
    wc_get_template( '../../plugins/woocommerce/templates/emails/email-header.php', array() );

  	// Create a post or page, change the ID to this post id.
    $finished = get_post_field('post_content', 34286);
    $finished = str_replace ('{Voornaam}', $Sitivoornaam , $finished );
    $finished = str_replace ('{Punten}', $puntenstring, $finished);
    $finished = str_replace ('{Korting}', $prijs, $finished);
    echo $finished;
	echo '<a style="font-size:9px;color:grey;" href="'.get_site_url().'/puntenmail.php?action=uitschrijven&key='.hash('ripemd160', $email).'&email='.$email.'">Klik hier indien je geen puntennotificatie meer wilt ontvangen</a>';
    $message = ob_get_clean();
    wp_mail( $to, $subject, $message, $headers );
}

add_filter( 'woocommerce_form_field' , 'remove_optional_fields_label', 10, 4 );
function remove_optional_fields_label( $field, $key, $args, $value ) {
    if( 'checkbox' === $key ) {
        $optional = ' <span class="optional">(' . esc_html__( 'optional', 'woocommerce' ) . ')</span>';
        $field = str_replace( $optional, '', $field );
    }
    return $field;
}

add_action( 'woocommerce_edit_account_form', 'display_edit_account_checkbox_field' );
function display_edit_account_checkbox_field() {
    woocommerce_form_field( 'spaarpunten', array(
        'type'  => 'checkbox',
        'class' => array('form-row-wide'),
        'label' => __( 'Houd mij maandelijks op de hoogte van mijn spaarpunten!', 'woocommerce' ),
        'clear' => true,
    ), get_user_meta(get_current_user_id(), 'spaarpunten', true ) );

}

add_action( 'woocommerce_save_account_details', 'save_checkbox_value_to_account_details', 10, 1 );
function save_checkbox_value_to_account_details( $user_id ){
    $value = isset( $_POST['spaarpunten'] ) ? '1' : '0';
    update_user_meta( $user_id, 'spaarpunten', $value );
}
//add_filter( 'the_content', 'user_role_check');

function user_role_check(){	
    $users = get_users();

    $users = array();
    $args = array(
        'number' => 10,
        'meta_query' => array(
            'relation' => 'AND',
                array(
                    'key'     => 'timestamp',
                    'value'   => strtotime('-1 month'),
                     'compare' => '<'
                ),
                array(
                    'key'     => 'spaarpunten',
                    'value'   => true,
                    'compare' => '='
                )
        )
    );
    
    // The Query
    $user_query = new WP_User_Query( $args );
    $authors = $user_query->get_results();

    if (!empty($authors)) {

        // loop through each author
        foreach ($authors as $author)
        {
            $user = get_userdata($author->ID);
            $email_adres = $user->user_email;

            $oneMonthAgo = strtotime('-1 month');
            $timestamp = time();
            $lastEmail = get_user_meta($user->id, 'timestamp', true);
            if($lastEmail < $oneMonthAgo){
            update_user_meta($user->id, 'timestamp', $timestamp);
            $puntenaantal = sitiweb_puntenaantal($user->ID);
            $Sitivoornaam = $user->first_name;
            $is_subscribed = get_user_meta($user->id, $key = 'spaarpunten', true);
            
                if($puntenaantal > 0 && $is_subscribed){
                    mail_to_subscribe($puntenaantal, $email_adres, $Sitivoornaam);
                    
                    echo 'Mail verstuurd naar '. $email_adres .' Klant heeft '. $puntenaantal .' punten!<br>';
                }
                else {
                    echo 'Mail niet verstuurd naar '. $email_adres .' Klant heeft '. $puntenaantal .' punten!<br>';
                }
            }
        }
    } else {
        echo 'No authors found';
    }

    
} 
function sitiweb_puntenaantal($user_id)
{
		global $wpdb;

		$content = get_user_meta($user_id, '_ywpar_user_total_points',true);
		
		return $content;
}


?>

Like this article?

Share on Facebook
Share on Twitter
Share on Linkdin
Share on Pinterest
Author picture

SitiWeb is de expert op het gebied van WordPress. gespecialiseerd in het leveren van maatwerk.

Leave a comment

Geef een reactie

Je e-mailadres wordt niet gepubliceerd. Vereiste velden zijn gemarkeerd met *