Free Website Tips

I read a lot of website advice every day. The best of that advice I wish everyone knew. Brainstorm! Let's put the greatest advice in quick tips. Usually I see a really good tip every few weeks. Remember to act on the tips - they are short so it should be easy.

Recent Twitter Posts

    Clients Include

    United Nations

    Center for Interpersonal Effectiveness


    Coram Deo Consulting Group


    Benchmarx

    Use Ubercart + Rules to send an email 30 days after checkout

    See video

    Transcript

    So we're sending out an email 30 days after a person checks out on the site and just asking them, "Hey, do you want to come back? Here is a coupon." This is for a dermatology clinic. And so, maybe they want to reorder a product, like a month later, they might have used it up. That's kind of the use case here.

    Now, I want to disclaimer this. I’m not a rules expert. I have a fair amount of experience with Ubercart, but Ubercart plus Rules is a whole new world. And I mainly got lucky making this setup work. And so, I wanted to document this for my own benefit as well as for others who might need to do the same thing.

    I’m not going to do a really in-depth tutorial, but I just wanted to kind of show you what I had. And then hopefully you’ll be able to use that on your own site or I’ll be able to use this in the future for other sites as well. Let’s take a look.

    I’ve got Rules installed. I’ve got the Ubercart Rules module also installed. And we basically need to setup two different things: a rule set and a trigger rule. There is a tutorial on the 'Resources Section' link at the bottom of this blog post that was the foundation for setting these up. And then, I took them from there and customized them for our particular purpose.

    Here is the rule set and that’s the "Email customer with coupon" rule set created with standard things like "Label" and "Machine readable name." And then the important part was after I created, clicking at "Add a new rule" to creating a new active rule called the "Send email rule" and that has just normal settings. You choose the "rule set," there is only one option there, and that’s the rule set you had created. Make sure it’s active in it, it cause you some problems when you're testing.

    And then, the main thing is this rule element, "Send an HTML mail to the user who has ordered email address," a little awkward to read it. At 11:00 p.m. at night, "Send an HTML mail to the user who ordered's email address," there we go.

    And here is the magic sauce and we’re going to talk a little bit more about this as we look at the trigger rule. But this pulls through the customer who’s ordered e-mail, customer who has ordered his email address. So, you can use it to send to them and you’ll notice the very little pulling off of is actually the same as the machine name you used for this rule set and that’s really important.

    We just have our typical subject, HTML message. Then, if you’re creating it, you can click "save" at the bottom. And you notice this email customer coupon correlates back to the actual rule set itself which is named "Email customer with coupon." And that pulls it in from whatever rule triggered this rule set. So, let’s take a look at what that rule is.

    Under "Triggered rules," we have one rule here, the "email 30 days after order." And it’s triggered by the Event, "order checkout, complete," which installing the Ubercart rules module gives you that option. And you can see that Event. Here is the label. Here is the machine readable name. Choose the event there from the Ubercart rules options. Make sure it’s active again. And here is where the magic happens.

    First, and if you have a user rules match, this is a big gotcha that happens a lot with rules, is we do the "Load customer by order." And that is when we need to be able to actually load the object, the user object so we can then access the email address. And this is just something that rules kind of handles and does for you, you create a new line here the customer by order, give it a label, user loaded, really simple stuff. But if you don't do it, you’re never going to be able to get the email address.

    And then after that, it needs to occur afterwards, because as you first loaded the customer, we have the schedule "Email customer with coupon." And email customer with coupon is the rule set into this action is scheduling for this to happen. And here is where we pull in that user and the arguments configuration.

    We pick that the user is going to be the loaded user, not user which is I think the current user, you’ll have to talk to somebody who knows the real better than I do, but we don't want that one we want the loaded user, right?

    And then, this is simply an identifier so that you can track it in the rule set schedule tasks which we look at after we test this out. And the important thing here is and note says it's "Existing task for this rule set with the same identifier will be replaced."

    So, if you just say, send email to customer what’s going to happen is after the first customer orders; it will schedule it to be sent 30 days later. And then the second customer orders and it overwrites the first customer’s scheduled task. And so, only the last customer is ever going to get the email if you happen to have 30 days with no orders.

    So, you need to make that unique and that’s fairly easy to do, we can just use the email address, but note that the variable we use. It’s different for echoing out the user-loaded mail, and that user-loaded correlates to the action that you’re just looking at. The "Load customer by order" action. The machine readable name is user loaded.

    So that’s what we then use here is user loaded. Then grab the email address. Maybe the schedule for 30 days for testing. Let’s go ahead and switch it to 5 minutes.

    All right. So, with all that in place we’re going to make an order. And before I do that, let me quickly show you one more page here, rules, rule sets, scheduling. Right now, there is no task scheduled. What’s going to happen after we make an order our tasks to send the email is going to get setup.

    Order a product. A further step on this might be to restrict it so that it only happens with certain products. We don't need that for our particular information. So, I haven’t explored it, but it theoretically should be possible. Somebody else with a little more experience than I, probably needs to give it a shot at that. Submit the order and our order is complete. Wonderful!

    So, we did this, at 17 after, and we can now look at the scheduling page, refresh that. And there we have it, is rule set name, rules, email, customer with coupon. The schedule date for when it’s going to happen or it should be 22 minutes after. So, five minutes later, wonderful. And then, the unique identifier, so, it doesn’t get overwritten. Send email to radiantcomptest@covenantdesign.com

    One caveat here would be if the user then came in 15 days later, if we saw this after 30 days, made another order, it would overwrite it, and then send an email until 30 days after that second order. In our case, that’s fine, because if they’ve already come back and made an order, we're great, no need to send them another email.

    In other users that might not be the case and so you need to add a unique identifier there. You might be able to use the task ID, I’m not sure. You would have to play around that a bit.

    And let’s pause here for a few minutes and I’ll pop you back in so you can see the email that got sent out. All right. We’ll wait five minutes here. And cron is set around less frequently, the five minutes. So let’s manually kick off for cron. And then head back. Task isn’t there anymore. So, hopefully that means it sent as an email.

    I’ve got two emails. First the order one, and second, viola, ready for a refill. Sent 0 minutes ago right when cron ran and that’s how it’s done.

    A couple of quick resources, like I mentioned will be linked, that just you know what to look for. This tutorial was just one of the standard rules tutorials including the rules documentation was a great first step to do something in a time manner using rule set and then a triggered rule and then customizing that to send our email, this little documentation LIP was very helpful.

    And then, just in case, modules we’re talking about here. This is the rules module, pretty major Drupal module. This is the Ubercart module, even more major Drupal module and finally you see rules, Ubercart rules which brings the two of those together. So, we can do magic things like this.

    Hope it’s helpful to you and possibly to me!