Forums › Custom Emails for WooCommerce › Trigger custom email only on the first order generated from a subscription product
- Support forum for the WooCommerce Additional Custom Emails & Recipients.
- This topic has 12 replies, 4 voices, and was last updated 1 year, 2 months ago by
Tom Anbinder.
-
AuthorPosts
-
June 27, 2022 at 10:16 am #85977
end-1818
GuestHi. Is it possible for the custom email to only be triggered on the first order generated from a subscription product? We wouldn’t want to send the custom email when the repeat subscription triggers every month.
June 30, 2022 at 1:16 am #85978Hi,
Sorry for the late reply. Let me test it and get back to you.
June 30, 2022 at 10:12 pm #85979So I’ve checked, and from my tests, it looks like if you set the custom email’s “Trigger(s)” option to a new order trigger, e.g., “New order (Any status)”, then the email is sent only once, on the initial order. I was testing with the official WooCommerce Subscriptions plugin.
If you are using some other plugin for subscriptions, it may behave differently, and in this case – I’ve just released a new plugin version (v1.5.2), where I’ve added the
alg_wc_custom_emails_is_enabled
filter, so now we can prevent email from being sent with a PHP snippet. For example, we could check if it’s a WooCommerce Subscriptions renewal order with:add_filter( 'alg_wc_custom_emails_is_enabled', function ( $is_enabled, $email, $object_id ) { if ( 'alg_wc_custom' === $email->id && // it's custom email #1 ( $order = wc_get_order( $object_id ) ) && is_a( $order, 'WC_Order' ) && // it's an order ( $subscription_id = get_post_meta( $object_id, '_subscription_renewal', true ) ) // it's a renewal ) { // Do NOT send email return false; } return $is_enabled; }, 10, 3 );
I hope this helps. Please let me know if you have any questions.
July 1, 2022 at 7:47 am #85980end-1818
GuestThanks loads for your time on this. Yes, we’re using the official WooCommerce Subscriptions plugin, so we’ll do some testing with this. Thanks again!
July 1, 2022 at 11:45 pm #85981Hi,
Happy to help 🙂 Please let me know if you’ll bump into any issues while testing.
August 3, 2023 at 12:22 pm #121297Liviu Mateescu
ParticipantHi,
Is it possible to use the filter above or some other filter to limit sending of the email only once per custommer (email address).
I only want to send a custom email on the first order with a promo code he can use on the next order. On the second order he should not receive the email a second time.
Thanks in advance for your help,
August 9, 2023 at 11:32 am #121606Moshtafizur
ParticipantHi Liviu,
Sorry for the late reply.
Could you please provide more details about the specific scenario you are referring to? In order to address your concern effectively, we need to know whether you are using any subscription plugin or if this is a general situation that you are encountering.
Once we have a clearer understanding of your setup and requirements, we will be able to offer you tailored suggestions.
Kind regards,
MoshtafizurAugust 9, 2023 at 1:25 pm #121611Liviu Mateescu
ParticipantHi,
No, I am not using any subscription plugin. Just regular woocommerce with simple and variable products. I have several follow-up emails setup wich work very well but one of them contains a promotional code with a discount on the next order. When the user makes his next order I don’t want him to receive this email again because the promo code works only onece per customer. This email is setup to be sent to the customer 3 days after the order is finalized.
So I am looking for a way to stop triggering this email if the user (email adress) allready received this email on a previous order. Is there any way of doing this???
August 14, 2023 at 2:04 pm #121878Moshtafizur
ParticipantHi there,
Sorry for the late reply.
I have escalated this with our development team. They will get back to you as soon as they can.
Kind regards,
MoshtafizurAugust 17, 2023 at 10:32 am #122012Hi, Liviu,
Please try this:
add_filter( 'alg_wc_custom_emails_is_enabled', function ( $is_enabled, $email, $object_id ) { if ( 'alg_wc_custom' === $email->id && // it's custom email #1 ( $order = wc_get_order( $object_id ) ) && // it's an order email ( $customer_id = $order->get_customer_id() ) && // not guest order ( $customer = new WC_Customer( $customer_id ) ) && // get customer object $customer->get_order_count() > 1 // not first order ) { // Do NOT send email return false; } return $is_enabled; }, 10, 3 );
Let me know if this helps.
August 17, 2023 at 12:16 pm #122033Update: I’ve just realized we may have a problem with this snippet. Let’s say the customer makes two orders on the same day. Even though the first email will be scheduled, and the second will not (so all ok for now), when three days pass, and the scheduled email is triggered – it won’t be sent because, at this point, the customer already has more than one order. To fix this, please update the plugin to the latest v2.2.8, and change our snippet to:
add_filter( 'alg_wc_custom_emails_is_enabled', function ( $is_enabled, $email, $object_id, $do_force_send ) { if ( ! $do_force_send && // not "force send", e.g., scheduled 'alg_wc_custom' === $email->id && // it's custom email #1 ( $order = wc_get_order( $object_id ) ) && // it's an order email ( $customer_id = $order->get_customer_id() ) && // not guest order ( $customer = new WC_Customer( $customer_id ) ) && // get customer object $customer->get_order_count() > 1 // not first order ) { // Do NOT send email return false; } return $is_enabled; }, 10, 4 );
Please give it a try and let me know what you think.
February 22, 2024 at 8:41 am #135501Liviu Mateescu
ParticipantHi,
I’ve been testing different versions of this code but I can’t get it to work asa I would expect.
First of all many of my customers chekout using guest mode. So I need a way to explude these from receiving the email on every other order.
I think I have been going abut this the wrong way.
I see there is a EXCULDE RECIPIENTS section in the email setting.
Is there a way to add the email to that list automaticly after the first email was sent.So basicly I am trying to exclude all email adresses right after the email is sent assuring that the client only receives the email once.
Keep in mind that all the emails I am sending are scheduled emails that go out 3 or 10 days after the order is placed
Thanks,
February 29, 2024 at 10:25 am #136028Hi, Liviu,
Please update the plugin to the latest v2.9.1 and add this snippet to your site:
add_action( 'alg_wc_custom_emails_email_sent', function ( $email ) { global $wpdb; $option_name = "woocommerce_{$email->id}_settings"; // Start MySQL transaction $wpdb->query( 'START TRANSACTION' ); // Get current settings $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM {$wpdb->options} WHERE option_name = %s LIMIT 1", $option_name ) ); // Exclude recipients $settings = ( isset( $row->option_value ) && '' !== $row->option_value ? unserialize( $row->option_value ) : array() ); $settings['exclude_recipients'] = $email->get_recipient() . ',' . ( $settings['exclude_recipients'] ?? '' ); $settings = serialize( $settings ); // Update (or Insert) if ( 0 === ( $result = $wpdb->update( $wpdb->options, array( 'option_value' => $settings, 'autoload' => 'yes' ), array( 'option_name' => $option_name ) ) ) ) { $result = $wpdb->insert( $wpdb->options, array( 'option_value' => $settings, 'autoload' => 'yes', 'option_name' => $option_name ) ); } // Commit (or Rollback) MySQL transaction $wpdb->query( ( $result ? 'COMMIT' : 'ROLLBACK' ) ); } );
This snippet will automatically append the current recipient to the “Exclude recipients” option (in “WooCommerce > Settings > Emails > Custom email #X > Advanced Options”). This way, you can manually edit it if needed. However, the option may become too long if you send a lot of emails.
P.S. Generally, the snippet does this:
add_action( 'alg_wc_custom_emails_email_sent', function ( $email ) { $email->update_option( 'exclude_recipients', $email->get_recipient() . ',' . $email->get_option( 'exclude_recipients', '' ) ); } );
However, I think this shorter version has a problem – it may skip adding a recipient if two different custom emails are sent at (almost) the same time. To solve this, in the first snippet we are using MySQL transactions and executing DB queries directly.
Please give it a try and let me know what you think.
-
AuthorPosts
- You must be logged in to reply to this topic.