The general idea behind this article is to look at the possible option on how to include a manually picked items from a catalog as a possible fallback addition into the results.
It can happen that when you request for example 6 products into the email from a reco model, not all 6 of them will make it into the template, it may be due to the strict catalog filter, or if the customer dont have enough learning events from the reco model. In this case you can choose manually which products you want to include as a refill. They may be some products that you want to boost the sales.
You will need to work with the JINJA code so lets go through the steps now action by action.
1. In the beginning you want to define the reco model itself into a variable where the results are stored. If you are editing the existing template or block you probably already have this step in the place, in that case it is important to know the variable name under which are the results stored. In this case it will be variable called "items"
{% set items = recommendations("Recommendation ID", 6, fill_with_random = false) %}
Here we requested a 6items from the recommendation model and disabled random items.
-------------------------------------------------------------------------------------------------
2. Second step would be a bit broader. First of all, we want to define a threshold of items, that means if the original results of the reco will be under this threshold we will initialize the refill from the fallback items, this should reflect the size number from recommendation, so in this case 6.
Then we need to define items you would like to manually define as a fallback into a separate list, in case you requested 6items, I recommend to define manually 6items into this list. There will be two variables, one "item_list" which will contain the ItemIDs of the fallback items.
Second variable will be called "fallback_items" and it will be defined right by the items fetched from the catalog, there is also a shuffle function to always randomize the order of the items.
In the third line of the code, please define a catalog name you want to use for fetching those fallback items.
{% set threshold = 6 %}
{% set item_list = ["ItemID 1","ItemID 2","ItemID 3","ItemID 4","ItemID 5","ItemID 6"] %}
{% set fallback_items = catalogs["Replace by catalog name"].items_by_id(item_list) |shuffle %}
-------------------------------------------------------------------------------------------------
3. In the next step we want to check whether the reco results are under the threshold amount, and if yes we will initiate filling the results with the fallback items. This will be the most complex step.
So we need to compare if "items" length is less than the "threshold" and if yes, initiate a for cycle that will iterate offer the "fallback_items" and append them to the items list. Also, if the threshold is 6 and results have only 4 items, we want to append only 2 items, so we need to treat this also with a math operations to find out how many items we need and then pull only the desired amount with indexing inside the for cycle.
{% set amount_to_refill = threshold - items |length %}
{% if items |length < threshold %}
{% for item in fallback_items[0:amount_to_refill] %}
{% append item to items %}
{%endfor%}{%endif%}
-------------------------------------------------------------------------------------------------
This code checks the difference between the amount of recommended results and threshold, store it in a separate variable, based on which we can tell how many fallback items we need to include into the recommendation results to achieve a desired amount. Then those amount of items are appended into the items list which is a final output of the recommendation that you can use in your template or block further for displaying items.
Whole code in your template will look like this -->
After that you are good to start working with the items variable and iterate over it to display the products into the HTML block, or into the template built with visual editor. It will always contains 6 products. If recommendation recommended only 3 valid products, the other 3 products remaining will be filled with the manually defined fallback automatically.
Feel free to utilize this method in your campaigns and I hope you learned something new. Happy coding !