Top WooCommerce & WordPress Plugins › Forums › Custom Emails for WooCommerce › Trigger custom email only on the first order generated from a subscription product
- Support forum for the Custom Emails for WooCommerce.
- This topic has 10 replies, 4 voices, and was last updated 1 month, 1 week 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 #121606Hi 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 #121878Hi 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.
- AuthorPosts
- You must be logged in to reply to this topic.