Trigger custom email only on the first order generated from a subscription product

Top WooCommerce & WordPress Plugins Forums Custom Emails for WooCommerce Trigger custom email only on the first order generated from a subscription product

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #85977
    blankend-1818
    Guest

    Hi. 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.

    #85978
    blankTom Anbinder
    Moderator
    Plugin Support

    Hi,

    Sorry for the late reply. Let me test it and get back to you.

    #85979
    blankTom Anbinder
    Moderator
    Plugin Support

    So 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.

    #85980
    blankend-1818
    Guest

    Thanks 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!

    #85981
    blankTom Anbinder
    Moderator
    Plugin Support

    Hi,

    Happy to help 🙂 Please let me know if you’ll bump into any issues while testing.

    #121297
    blankLiviu Mateescu
    Participant

    Hi,

    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,

    #121606
    blankMoshtafizur
    Moderator
    Plugin Support

    Hi 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,
    Moshtafizur

    #121611
    blankLiviu Mateescu
    Participant

    Hi,

    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???

    #121878
    blankMoshtafizur
    Moderator
    Plugin Support

    Hi 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,
    Moshtafizur

    #122012
    blankTom Anbinder
    Moderator
    Plugin Support

    Hi, 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.

    #122033
    blankTom Anbinder
    Moderator
    Plugin Support

    Update: 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.

Viewing 11 posts - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.