Search

How to Add Subscribe/Unsubscribe to Email Notifications Option in WooCommerce

Listen to this article
email-newsletter
Add a Subscribe Option in WooCommerce

Want to keep your WooCommerce store customers interested? Send out monthly.. no..weekly… no wait! Daily newsletters. *Wide Grin* *Two Thumbs Up*.

Noooot so faasst!

Think about it. How many newsletters have you signed up for… say in the past few months? Okay and let’s consider you have signed up for a few. Do you read every newsletter which comes your way? You don’t. And neither do your store customers. Customers are interested in promotional offers, such as sales, or in new products being launched. And, these offers could be sent as occasional emails.

[space]

Email Notifications for WooCommerce

For your WooCommere store, there are several plugins available which you can use to send out such emails. You could use some standalone WordPress newsletter plugins, like MailPoet, or integration plugins, (which just connect your website to an email marketing service provider) like MailChimp. However, most of these are not integrated directly into your WooCommerce store. And usually add a subscription option only on a checkout or registration form.

In today’s article, we’ll learn how to add another subtle option, like a button or form, using which a customer can subscribe to your notification emails, using a custom plugin. You could combine this option with a newsletter plugin or to send out custom emails.

[space]

Add Subscribe/Unsubscribe Option under Customer Account

A subscription option can be shown using a button, on a customer’s account page. The customer can use this button to subscribe (or unsubscribe) to your email notifications. Sounds good? Let’s begin.

Add Subscribe/Unsubscribe Option

On your WooCommerce account page, you can add a button, say just below, ‘My Account’, using your own plugin. To display such a button you have to override the WooCommerce ‘my-account.php’ template. You can refer ‘How to Override WooCommerce Templates‘, to learn how you can change the template in your plugin. In the template file, add the following code:

<?php if(get_user_meta($current_user->id,'wdm_subscribe',true) == 'no'): ?>
<p><a href="/my-account/?wdmsubs=true" value="subscribe" name="subscribe" id="wdm_subscribe">Subscribe</a></p>
<?php else: ?>
<p><a href="/my-account/?wdmsubs=false" value="subscribe" name="subscribe" id="wdm_subscribe">Unsubscribe</a></p>
<?php endif; ?>

The above code checks if the current user has subscribed to your newsletter, if not, the button value is shown as ‘Subscribe’, else ‘Unsubscribe’ will be shown. I have used a custom meta key to track if the user has subscribed or not, and the button click is tracked using a parameter ‘wdmsubs’.

Display Acknowledgement Message

Once a user clicks on the button, and subscribes, an acknowledgment message can be shown using the below given code. Add the following code in your plugin.

add_action( 'wp_head', 'wdm_woo_subscribe' );
function wdm_woo_subscribe()
{
    if(isset($_REQUEST['wdmsubs']) && $_REQUEST['wdmsubs'] == true)
    {
     update_user_meta(get_current_user_id(),'_wdm_subscribe','yes');
     wc_add_notice( __( 'Yayy! You  have subscribed to our email notification.', 'woocommerce' ) );
    }
}

Add User to List

Upon subscription, you need to add the user to your email recipient list. You could use the above function to add the user to your list, or to a list maintained using a newsletter plugin. To add it to a newsletter plugin, you need to find a filter or hook using which you can add a user to the subscription list. For example, if you use MailPoet, you can use refer how to add subscribers using your plugin, to add the user to a MailPoet subscription list.

Unsubscribe User from Default Emails

To unsubscribe a user, we need to remove him from the list of email recipients. A similar hook or filter can be used, to remove the user from the subscribers list. However, to unsubscribe the user from the default WooCommerce emails, say for example, ‘woocommerce_email_recipient_customer_processing_order’, you can use the below code:

add_filter('woocommerce_email_recipient_customer_processing_order','wdm_woo_unsubscribe_processing_order');
function wdm_woo_unsubscribe_processing_order($recipients)
{
    $recipients = explode(',',$recipients);
    $final_recipient = array();
    foreach($recipients as $r)
    {
      $user = get_user_by_email( $r );
      $user_id = $user -> ID;
      /* check user meta, to verify if user has to be excluded from recipient list. */
      if( get_user_meta($user_id, 'wdm_subscribe', true) == 'no')
      {
       $final_recipient[] = $r;
      }
   }
   $final_recipient = implode(',',$final_recipient);
   return $final_recipient;
}

This function can also be used to add an ‘Unsubscribe’ link in WooCommerce Email footer, by overriding the ’email-footer.php’ template.

[space]

Your newsletters or notifications could be used to market your products and offers. This simple notification option under a user’s account page can be used along with your own email notification plugin, or for default WooCommere emails. What do you think? Do you think it’s a good option? Let me know your thoughts and views in the comment section below 🙂

Pooja Mistry

Pooja Mistry

Leave a Reply

Your email address will not be published. Required fields are marked *

Get The Latest Updates

Subscribe to our Newsletter

A key to unlock the world of open-source. We promise not to spam your inbox.

Suggested Reads

Join our 55,000+ Subscribers

    The Wisdm Digest delivers all the latest news, and resources from the world of open-source businesses to your inbox.

    Suggested Reads

    WordPress Tips & Tricks
    Ketan Vyawahare

    How to Make Responsive Tables using CSS without Tag Read More »