Вступ
Token Loops allow you to iterate over multiple items in loopable tokens, a new type of token that contains information about one or more items. If you’re familiar with User Loops or Post Loops, Token Loops work in an almost identical way, except they loop through items in the supplied token. You can use Token Loops with any paid Automator license.
Here are a few examples of recipes you could run with token loops and a few plugins like WooCommerce, LearnDash and BuddyBoss:
- Export all orders in the past 24 hours, week, 30 days or year to a Google Sheet or Airtable.
- When a user purchases a product, loop through all of their previously purchased products and email them a special coupon code if they’ve qualified for a multi-product promotion.
- When a user purchases a product, loop through all of their previously purchased products and send out supplementary information on how to use complementary products together.
- When a user purchases a product, loop through the categories assigned to those products and award an achievement for purchases in specific categories.
- When a user purchases a product, loop through their enrolled courses and complete them or reset their progress.
- When a user submits a form, add a post to the activity stream of each of their enrolled groups.
- When a user completes a course, loop through their previously completed courses and award a certificate if they’ve completed the required courses.
- When a user subscribes to a product, loop through their active subscriptions and award store credit for specific combinations of active subscriptions.
Adding a token loop
To add a token loop to your recipe:
- Scroll to the Actions section of the recipe.
- Click Add.
- Click Token loop.
This adds an empty loop to your recipe. Next, select the loopable token that contains the desired data. Only special loopable tokens will appear in the token selector of a token loop.
Фільтри контуру
Actions will be run on every item in the loopable token that meets your loop filter. Adding loop filters makes loops run more efficiently, so if you can narrow your post targets with filters, you should. The default loop filter is that the current item is not empty. Additional filters will be added over time.
Adding actions inside a token loop
Actions inside a token loop will be run for each item that meets the loop’s filter criteria.
The type of action you want to run will depend on the items in the token. Are they products? Courses? Groups? If you are iterating over courses, you may want to run a Reset course progress action. If they’re products, maybe you want to check if the user purchased a specific product in the past and if so, send an email.
In most cases, you’ll need some information about the items in the loop. To get that information, you’ll use loop tokens.
In the Loop token category you will find key tokens related to the items you’re iterating. If you’re iterating over products, you’ll find things like the Product ID, Product Name, etc. For courses, the Course ID, Course Name, etc. Here’s what that might look like:
Suppose you have a recipe that fires when a user purchases an online Introduction to Marketing course. You could loop over the user’s previously purchased products, and if they previously purchased a complementary course, like Introduction to Sales, send them an email letting them know about a Sales and Marketing group they might want to join. Or add them to the group directly. Or, perhaps you’re running a promotion if a user purchases both of those courses, but you want to offer it even if they’re not purchased in the same transaction. Here’s what that recipe might look like:
Запуск циклу
To run the bulk actions inside a loop, you need to trigger the recipe. Most of the time, you’ll want to use a regular trigger to run the recipe containing the loop. For some semi-automated workflows, you may want to use the Run now trigger to run token loops right from inside the recipe page.
Моніторинг прогресу циклу
Once the recipe containing the token loop has been triggered, you can monitor the progress of the loop from Automator > Logs and view results in the easy-to-read, detailed logs.
Скасування циклу
Кнопка скасування дозволяє скасувати поточний цикл. Цикли обробляються пакетами (див. розділ « Що потрібно знати » нижче), розмір яких автоматично підлаштовується під апаратне забезпечення вашого сервера. Скасування поточного циклу призведе до скасування пакетів, які ще не розпочалися; проте поточний пакет продовжуватиме виконуватися, доки не буде завершено.
Перегляд результатів циклу
Once the loop has completed, you can expand the log to see the result of each action on each item in the loop. This gives you very detailed information about actions that experienced an error so you can easily trace any issues.
Важливі примітки
- There is no way to undo the actions that were executed in a loop, so be 100% sure your loop is correct before running. Consider testing on a staging site prior to running an extensive loop on your live site.
- When running loops with app actions (such as Google Sheets), you may run into rate limitations on the receiving side, which will cause actions to fail. While very rare, extremely large batches should in some cases be divided up.
Під капотом
- Loops run as background processes. Loops are designed to be as efficient as possible and should not result in a performance hit to the front end of your site.
- Оскільки цикли виконуються у фоновому режимі, дії, що не входять до циклу, зазвичай завершуються раніше за сам цикл. Якщо ви хочете, щоб дія в рецепті виконувалася після завершення циклу, єдиний спосіб зробити це – вручну вказати затримку для дії.
- Цикли виконуються пакетами . Розмір і частота цих пакетів автоматично адаптуються до можливостей вашого сервера і не можуть бути налаштовані вручну.
- Ви можете зупинити цикл, що виконується, з журналів. Однак, пакет, що виконується, не можна зупинити, і він виконуватиметься до завершення. Будь-які пакети, які ще не розпочалися, будуть скасовані.
- Скільки часу потрібно для завершення циклу, залежить від багатьох факторів, зокрема:
- The number of items the loop iterates over
- Кількість фільтрів усередині циклу
- Кількість дій усередині циклу
- Тип дій усередині циклу (деякі дії повільніші за інші)
- Можливості сервера, на якому розміщено сайт