How to Set Up WooCommerce Subscribe or Unsubscribe To Our Newsletter

WooCommerce Subscribe or Unsubscribe To Our NewsletterAre you looking for a way to keep your store customers interested? In today’s post, we will show you how you can allow customers to subscribe to your monthly, weekly, or daily newsletters. It is worth mentioning that we will be using custom code snippets to achieve this.

Customers are always interested in promotional offers like sales or if a new product is being launched. It is important to note that these offers cannot be sent out as occasional emails.

In addition, there are several plugins available for sending out emails. For example, you can use a standalone WordPress newsletter plugin, like MailPoet. Alternatively, you can also use an integration plugin like MailChimp.

However, these plugins are not integrated directly into your WooCommerce store. They usually add a subscription option only on a checkout or registration form. This is why we created this guide to help you add another subtle option.

WooCommerce Subscribe or Unsubscribe To Our Newsletter

By the end of today’s brief tutorial, you will be able to add a subscribe and unsubscribe option like a button on a customer’s account page. The customer can use this button to subscribe (or unsubscribe) to your email notifications. This is a great way to improve the user experience in your WooCommerce store.

It is worth mentioning that we will be editing some of WooCommerce’s core files. This means that you need to install or create a child theme to ensure your changes are not lost when updating your theme.

Let us get right into it.

Steps to Add Subscribe or Unsubscribe Option in WooCommerce

Here are the simple steps you need to follow:

  1. The first step is to display the Subscribe/Unsubscribe Option by overriding the WooCommerce ‘my-account.php’ template. To do this, add the following code to this the ‘my-account.php’ template file:
[php] <?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; ?>
[/php]
  1. After the user clicks on the button, and subscribes, we need to display an acknowledgment message by adding the following code to the functions.php file of your child theme:
[php] add_action( ‘wp_head’, ‘njengah_woo_subscribe’ );
function njengah_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’ ) );
}
}
[/php]
  1. If you want to add the email recipient list to a newsletter plugin, you need to find a filter or hook to add it. For example, if you use MailPoet, you can use refer to how you can add subscribers here.
  2. Finally, we need to add an ‘Unsubscribe’ link in WooCommerce Email footer, by overriding the ’email-footer.php’ template. Add the following code:
[php] add_filter(‘woocommerce_email_recipient_customer_processing_order’,’njengah_woo_unsubscribe_processing_order’);
function njengah_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;
}

[/php]
  1. This is the outcome after a user subscribes:unsubscribe

Wrapping Up

In summary, we have shared how you can allow customers to subscribe or unsubscribe to your newsletter. You can use this opportunity to market your products and offers.

It is important to back up your site before making any changes. This will ensure that you can revert to the previous version if you make a mistake.

We hope that this post provided a solution to your problem.

Similar Articles

  1. 30+Best WordPress Membership Plugins Collection