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 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #85977
    end-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
    Tom Anbinder
    Moderator
    Plugin Support

    Hi,

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

    #85979
    Tom 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
    end-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
    Tom Anbinder
    Moderator
    Plugin Support

    Hi,

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

    #121297
    Liviu 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
    Moshtafizur
    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
    Liviu 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
    Moshtafizur
    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
    Tom 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
    Tom 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.

    #135501
    Liviu Mateescu
    Participant

    Hi,

    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,

    #136028
    Tom Anbinder
    Moderator
    Plugin Support

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

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