Forum Replies Created

Viewing 1 post (of 1 total)
  • Author
    Posts
  • in reply to: Adding GiveWP donors to a mailing list #293070
    Craig Haller
    Participant

    Add a radio with Yes and No and call the meta _give_want_newsletter then put this code in a snippet or functions.php

    /**
    See give/includes/payments/functions.php
    * Fires while inserting payments.
    * @param int $payment_id The payment ID.
    * @param array $payment_data Arguments passed.
    */

    function bdn_give_insert_payment( $payment_ID, $payment_data ) {

    if ( get_post_meta( $payment_ID, '_give_want_newsletter', true ) != 'Yes' ) return;

    // https://github.com/TheNewsletterPlugin/newsletter-codesamples/blob/master/subscribe.php
    // Please, refer to the TNP_Subscription and TNP_Subscription_Data classes of Newsletter.
    // 'email' => NULL,'name' => NULL,'surname' => NULL,'sex' => NULL,'language' => '','referrer' => NULL,'http_referrer' => NULL,
    // 'ip' => NULL,'country' => NULL,'region' => NULL,'city' => NULL,'lists' =>

    // Ok, at least Newsletter should be there
    if (!class_exists('newsletter')) return;
    // Optinally a version check (you should use version_compare())
    if (NEWSLETTER_VERSION < '7.4.0') return;
    // This is the object representing a subscription (data and properties) to be
    // sent to Newsletter. The default one is prefilled with some attributes reflecting
    // the plugin settings (opt-mode, pre-assigned lists, ...).
    $subscription = NewsletterSubscription::instance()->get_default_subscription();
    // Start filling in the subscriber data.
    $subscription->data->name = get_post_meta( $payment_ID, '_give_donor_billing_first_name', true );
    $subscription->data->surname = get_post_meta( $payment_ID, '_give_donor_billing_last_name', true );
    $subscription->data->email = get_post_meta( $payment_ID, '_give_payment_donor_email', true ); // Of course, mandatory.

    // Lists: you integration could optionally add the subscriber to specific list. This is
    // useful to trigger other actions in Newsletter, for example starting an autoresponder series.
    // You should create a configuration side for your plugin where the administrator can select which

    // lists to activate (from 1 to 40).
    $subscription->data->lists['1'] = 1; // Activate list 1.
    // $subscription->data->lists['2'] = 0; // Deactivate list 2.

    // Subscription options
    $subscription->optin = 'single'; // Or 'double'. Preset following the Newsletter settings, you can comment it out.
    $subscription->send_emails = true; // Welcome or activaton emails, should be sent?
    $subscription->spamcheck = true; // Perform spam check following the rules set on Newsletter Security page?

    // Can return a WP_Error or true.
    $result = NewsletterSubscription::instance()->subscribe2($subscription);
    if (is_wp_error($result)) {
    // You can deal with the error here, but this is a in-process function, do not break the
    // flow with a die(). Do not output messages as well. Best practice is to manage the error
    // from the caller, not here.
    }

    }
    add_action( 'give_insert_payment', 'bdn_give_insert_payment', 10, 2 );

Viewing 1 post (of 1 total)