CAll Us: +1 888-999-8231 Submit Ticket
Adding Custom Triggers to AutomateWoo

Adding Custom Triggers to AutomateWoo

Email marketing is key to running an effective eCommerce site. While there are many good email marketing platforms out there, many don’t have very deep interactions with WooCommerce. If you start looking at all the plugins available for WooCommerce and their capabilities, there are almost no email marketing platforms that interact with many of the different events you’ll want to use.

This is where AutomateWoo fills a huge gap. AutomateWoo is built specifically for WooCommerce; to automate your customer interactions completely inside your WordPress admin interface. This means that it has fairly deep hooks into WooCommerce that you can leverage. This also means that it’s built using WordPress best practices for plugin development and doesn’t need to interact with a 3rd party server.

Today we’re going to take a look at what AutomateWoo can do for you out of the box. Then I’m going to walk you through the process of creating a custom trigger for those few times that AutomateWoo doesn’t cover one of your use cases.

AutomateWoo Trigger Basics

AutomateWoo works by detecting actions that users take. Once an action is recognized by the plugin, it allows you to trigger events. If that sounds a bit confusing, let’s use AutomateWoo to send a thank you email to a customer once they have spent over $200 in our store.

To start go to AutomateWoo -> Workflows and then click Add Workflow at the top of the page.

Next, create a descriptive title for your Workflow. I titled mine Customer Spend Reaches

Now we need to choose the trigger we want to use. Click on the select box and choose Customer Total Spend Reaches from the list of available triggers and then set the total spend value to $200.

Now we need to choose an action to take when a customer spends more than $200 in our store. Click the blue + Add Action button and choose Send Email as the action we want to take.

As you fill in what you want to say to a customer remember that you can use any of the variables that are listed on the right-hand side of the screen. To use a variable you need to use double curly braces around the variable name. That means to use the customer’s first name (if they have one in your system) you’ll use {{customer.first_name}} in your text where you want that information to show up.

Once you’ve got the email content you want, scroll back to the top of the page and click the blue Save button so that your Workflow is saved and ready to go. If you’re working on a Workflow, you can change the status of it to Disabled and then click Save. Marking a Workflow as Disabled will ensure it doesn’t run while you’re working on it.

Do pay attention to the status of a Workflow. I’ve had workflows running that I didn’t want running, and also thought I had a workflow active that was disabled. Always double-check the status of your workflow once you’ve finished working with it for the day.

One feature of AutomateWoo we haven’t touched on yet is their rules. Rules allow you to filter which type of customer a rule works for. You could use two different rules to reward the repeat customer above based on where they are. Maybe you can mail customers in your country a card and you use a rule to send yourself an email with their address when they reach the threshold. For users outside your country, you could send the email above instead of the card.

Create a Custom AutomateWoo Trigger for WooCommerce Teams

Despite the deep hooks that AutomateWoo has in WooCommerce, it doesn’t cover every case for every plugin. For a client I worked with recently, we wanted to send each new team member an email as they were added to a team inside Teams for WooCommerce Memberships. AutomateWoo doesn’t provide a trigger for this out of the box, but it is possible to add your own custom triggers to AutomateWoo.

To start we need a base plugin, which you can see below.

<script src=”https://gist.github.com/curtismchale/6d795fca5159fb152f925c5d53fccb9f.js”></script>

While it’s possible to have a single plugin file and include all the custom triggers you may want, I don’t like doing that unless I’m 100% sure that I’m only adding a single trigger. If you end up adding more than one or two triggers you end up with a huge plugin file with functions spread all over and it becomes hard to manage which trigger your dealing with at any one time. Instead, I like to keep each trigger in its own file and include them in the main plugin. This makes it easy to pick out which trigger you’re working on.

We will need to include the main trigger registration in the plugin though, so let’s do that and include the file that will contain our trigger. Inside our init function add the trigger filter.

add_filter( 'automatewoo/triggers', array( $this, 'add_to_team_trigger' ), 10, 1 );

That’s a filter hook in WordPress. Filters allow you to change data as it passes through the code. In this case, we’re going to add to an array in the next block of code.

This in turn calls a function called add_to_team_trigger which will register our new trigger for AutomateWoo. Just below our init function add the following code, which should make your base plugin look like this.

public static function add_to_team_trigger( $triggers ){

    require_once( 'trigger-add-to-team.php' );

    $triggers['nexcess_add_to_team'] = 'Nexcess_Add_To_Team_Trigger';

    return $triggers;

}

Note that just before we adding to the $triggers array, we required a file called trigger-add-to-team.php. Now we need to create that file so that we have a trigger in AutomateWoo. Create a file called trigger-add-to-team.php in your plugin folder and then paste the code below into it.

<script src="https://gist.github.com/curtismchale/4face7b92f5fcab140267e65c3540528.js"></script>

Now, let’s walk through what the code is doing. Lines 3 – 5 are about security. They stop anyone up to no good from directly accessing the file itself. Next, we define a class called Nexcess_Add_To_Team_Trigger which extends the base functionality of AutomateWoo’s Trigger class. Line 14 defines the data that is passed to our trigger.

In our init function we define two things. First, we define the name of our hook and second, we say what group it should go in. You can use the existing groups in AutomateWoo or you can define a custom group. In this case, we used the Team group because the trigger is related to WooCommerce Teams. Any future team trigger would go in the same group.

If our trigger needed to load any custom UI elements we’d use the load_fields function. We don’t have any (and I’ve never needed any) so we’ll just leave that alone.

The register_hooks function is what catches what’s happening in WooCommerce teams so that we can do something when a new team member is added. If we go to the Team.php file in WooCommerce Memberships for Teams and go to the end of our add_member function we’ll see that once a team member has been added successfully it calls the hook named wc_memberships_for_teams_add_team_member.

Note that wc_memberships_for_teams_add_team_member passes the Member object and not a user_id which is what AutomateWoo expects to get on line 47. That means we need to take the Member object and get the user_id from it on lines 43 and 44 so that we can pass the user_id to AutomateWoo.

wc_memberships_for_teams_add_team_member is an action hook. This allows you to detect when an event happens in WordPress so that you can do something else based on the occurrence of the event. You can read more about WordPress Hooks in the documentation.

By adding to this action hook we run our trigger once a new member has been added to a team.

Next our catch_hooks function is called by the register_hooks function, specifically it’s called here when the wc_memberships_for_teams_add_team_member action is run. This is going to pass the user_id to the rest of AutomateWoo so that we can send a user an email.

The result of all this code is that if we create a new Workflow in AutomateWoo we see that we have a new trigger which lets us run a Workflow when a new user is added to a team. Make sure your custom plugin is activated and then create a new workflow using our new trigger.

That’s it, we now have our custom trigger working for AutomateWoo. You can see the completed plugin here

To add custom triggers for other events on your site you’ll need to look for other action hooks so you can detect different events as they happen. Using pretty much the same code above I could also email a user when they are removed from a team by using the wc_memberships_for_teams_after_remove_team_member hook in the same Teams.php file.

Source link

About the Author

Leave a Reply