seb86 / MailPoet-WooCommerce-Add-on

Let your customers subscribe to your newsletters as they checkout with their purchase.
https://wordpress.org/plugins/mailpoet-woocommerce-add-on/
7 stars 3 forks source link

Hide checkbox on checkout if logged user is already a subscriber #9

Closed Januszpl closed 8 years ago

Januszpl commented 10 years ago

It would be great to hide checkbox field when logged user visit checkout page and he is aleardy a subscriber.

For non logged user it would great if after filling e-mail it would check if it is already in newsletter database. If not then it would show checkbox field to sing up for newsletter.

seb86 commented 10 years ago

@Wysija What do you guys think of this?

Januszpl commented 10 years ago

I changed on_checkout_page function in mailpoet-woocommerce-addon.php

function on_checkout_page($checkout){ // Checks if subscribe on checkout is enabled. $enable_checkout = get_option('mailpoet_woocommerce_enable_checkout'); $checkout_label = get_option('mailpoet_woocommerce_checkout_label');

    // modified
    // if user logged check if his email is already in newsletter database
    if (is_user_logged_in() ) {

        $user_id = get_current_user_id();
        $user_email = get_user_meta( $user_id, 'billing_email', true ); 
        global $wpdb;

                   // look up for email in wp_3_wysija_user
        $added = $wpdb->query("SELECT * FROM wp_3_wysija_user WHERE email LIKE '$dodany_email' AND status = '1' ;");
        $il=count($added);

    } else {
        $added=0;
        // if not logged it would be great to check via JS/PHP after user filled email to check if he has been already a subscriber - if not then show checkbox field or if yes then hide checkbox field
    }

            // added extra condition && $added!=1 to show field only for new subscribers
    if(($enable_checkout == 'yes') && ($added!=1)){
        echo '<div id="mailpoet_checkout_field">';
        //echo apply_filters('mailpoet_woocommerce_subscribe_checkout_title', '<h3>'.__('Subscribe to Newsletter', 'mailpoet_woocommerce').'</h3>');
        woocommerce_form_field('mailpoet_checkout_subscribe', array( 
            'type'          => 'checkbox', 
            'class'         => array('mailpoet-checkout-class form-row-wide'), 
            'label'         => htmlspecialchars(stripslashes($checkout_label)), 
        ), $checkout->get_value('mailpoet_checkout_subscribe'));
        echo '</div>';
    }
}