• Resolved Emily

    (@ebloss)


    Hello! I’m working on integrating MailPoet and WooCommerce. I can add a new subscriber using the code from https://support.mailpoet.com/knowledgebase/plugin-form-integrate/ but the problem is, if a subscriber already exists, it won’t add a new newsletter subscription. I have it set to check if the user already exists, but can’t figure out how to add one for someone who is already in there. Here’s the code:

    $data_subscriber = array('user' => $user_data, 'user_list' => array('list_ids' => $list_array));
    
    // see if the subscriber already exists
    $sSQL = "SELECT user_id FROM wp_wysija_user WHERE email='".$item['item_meta']['Attendee - Email Address'][0]."'";
    $checksub = $wpdb->get_results( $sSQL );
    if ($checksub) {
    	foreach ($checksub as $c) {
    		$added_user_id = $c->user_id;
    		$helper_user = WYSIJA::get('user', 'helper');
    		$helper_user->subscribe($added_user_id, TRUE, FALSE, $list_array);
    	}
    } else {
    	/* @var $helper_user WYSIJA_help_user */
    	$helper_user = WYSIJA::get('user', 'helper');
    	$helper_user->no_confirmation_email = TRUE;
    	$added_user_id = $helper_user->addSubscriber($data_subscriber);
    	$helper_user->confirm_user($added_user_id);
    	$helper_user->subscribe($added_user_id, TRUE, FALSE, $list_array);
    }

    Any ideas? Thanks so much in advance!!

    https://www.ads-software.com/plugins/wysija-newsletters/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Wysija

    (@wysija)

    Have you tried one of these 2 add-ons, instead of doing your own code: https://woocommercemailpoet.com/ or https://www.ads-software.com/plugins/mailpoet-woocommerce-add-on/ ?

    Thread Starter Emily

    (@ebloss)

    Hi and thank you! I did try the first plugin and it made the whole system unstable, so I grabbed the code and integrated it directly. Then I looked at the import code of mailpoet and grabbed portions of that, to add subscribers who already existed. It’s working great now!

    Thread Starter Emily

    (@ebloss)

    In case it will help anyone, here is the code I added to my functions.php

    function mailpoet_woocommerce_order_complete ( $order_id ) {
    	global $woocommerce;
    	global $wpdb;
    	if ( !$order_id )
    		return;
    
    	// subscribe person to Mailpoet autoresponder or list
    	if (!class_exists('WC_Order') || !class_exists('WYSIJA'))
                return;
    
            $order = new WC_Order($order_id);
            $list_array = array();
            $items = $order->get_items();
    
            foreach ($items as $item) {
            	$list_array = array();
            	$item_meta = new WC_Order_Item_Meta( $item['item_meta'], $item['product_id'] );
            	$mar = get_post_meta($item['product_id'], 'mailpoet_ar_id', TRUE);
            	if (strstr($mar, ',')) {
                	$sun_list_array = explode(",", get_post_meta($item['product_id'], 'mailpoet_ar_id', TRUE));
            	} else {
            		$sun_list_array = array($mar);
              	}
              	if (sizeof($sun_list_array)) {
    				foreach ($sun_list_array as $af) {
    					if (!in_array($af, $list_array))
    					 array_push($list_array, $af);
    				}
    				 if ( $item_meta->meta ) {
    					 $user_data = array('email' => $item['item_meta']['Attendee - Email Address'][0], 'firstname' => $item['item_meta']['Attendee - First Name'][0], 'lastname' => $item['item_meta']['Attendee - Last Name'][0]);
    				 }
    				 $data_subscriber = array('user' => $user_data, 'user_list' => array('list_ids' => $list_array));
    
    				// see if the subscriber already exists
    				$sSQL = "SELECT user_id FROM wp_wysija_user WHERE email='".$item['item_meta']['Attendee - Email Address'][0]."'";
    				$checksub = $wpdb->get_results( $sSQL );
    				if ($checksub) {
    					foreach ($checksub as $c) {
    						$added_user_id = $c->user_id;
    						$cdump = print_r($c, true);
    						$cdump .= "\n\n" .print_r($list_array, true);
    						//$helper_user = WYSIJA::get('user', 'helper');
    						//$helper_user->subscribe($added_user_id, TRUE, FALSE, $list_array);
    
    						$time_now = time();
    						$wpdb->rows_affected = 0;
    						$helper_email = WYSIJA::get('email', 'helper');
    						$model_wysija = new WYSIJA_model();
    
    						// insert query per list
    						$query = 'INSERT IGNORE INTO wp_wysija_user_list (<code>list_id</code> ,<code>user_id</code>,<code>sub_date</code>) VALUES ';
    						foreach ($list_array as $keyl => $list_id) {
    							$query.='(' . (int)$list_id . ' , ' . (int)$added_user_id . ' , ' . (int)$time_now . ')';
    						}
    						$cdump .= "\n\n" . $query;
    						mail('[email protected]', $sSQL, $added_user_id . "\n\n" . $cdump);
    
    						$result_query = $model_wysija->query($query);
    
    						// send autoresponder
    						// list the active auto responders emails
    						$active_autoresponders_per_list = $helper_email->get_active_follow_ups(array('email_id', 'params'), true);
    
    						if (!empty($active_autoresponders_per_list)) {
    							foreach ($list_array as $list_id) {
    								// checking if this list has a list of follow ups
    								if (isset($active_autoresponders_per_list[$list_id])) {
    									// for each follow up of that list we queu an email
    									foreach ($active_autoresponders_per_list[$list_id] as $key_queue => $follow_up) {
    										// insert query per active followup
    										$query_queue = 'INSERT IGNORE INTO wp_wysija_queue (<code>email_id</code> ,<code>user_id</code>,<code>send_at</code>) ';
    										$query_queue .= ' SELECT ' . (int)$follow_up['email_id'] . ' , B.user_id , ' . ($time_now + $follow_up['delay']);
    										$query_queue .= ' FROM wp_wysija_user_list as B';
    										$query_queue .= ' WHERE B.list_id=' . (int) $list_id . ' AND sub_date=' . $time_now;
    
    										$model_wysija->query($query_queue);
    
    										//$this->_data_numbers['emails_queued'] += $wpdb->rows_affected;
    									}
    								}
    							}
    						}
    
    					}
    				} else {
    					/* @var $helper_user WYSIJA_help_user */
    					$helper_user = WYSIJA::get('user', 'helper');
    					$helper_user->no_confirmation_email = TRUE;
    					$added_user_id = $helper_user->addSubscriber($data_subscriber);
    					$helper_user->confirm_user($added_user_id);
    					$helper_user->subscribe($added_user_id, TRUE, FALSE, $list_array);
    				}  // if subscriber exists or not
    
    			}  // if there's an autoresponder on this product
            }  // check each item on the order
    
    	return;
    
    }  // end function mailpoet_woocommerce_payment_complete
    
    add_action( 'woocommerce_order_status_completed', 'mailpoet_woocommerce_order_complete' );

    Late to the party, but your code has helped me a lot, Emily! To get the data I needed, I figured I would need to query the wysija tables in the DB, and it turns out that is correct.

    It would be a lot simpler if I could find methods in the plugin like:

    get_subscribers($list_id;) or get_lists($user_id); or even get_wysija_lists();

    instead of making my own, unless I’m missing something… ??

    Nice plugin, either way.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Programatically adding subscriptions’ is closed to new replies.