website logo
Product FeedCommunity Forum
API Docs
Template JS
Headless SDK
Navigate through spaces
⌘K
Introduction
Getting Started
Quickstart : Smart Search
Quickstart : Smart Category Merchandising
Quickstart : Smart Recommendations
Quickstart : Moi
Apps/Plug-ins
Guide
Structure
Configuration
JS Library
Versioning
Template Reference
Build UI
Custom Templates
Modify Request
Modify Response
Modify Recommendations
Styling CSS
Analytics/Tracking
How To
Add Filters To URL
Add Pagination To URL
Add-To-Cart Function
Customer Groups
Facet Option Order
Facet Show/Hide
Infinite Scroll Results
Items Per Page
Magento B2B MSI
Multi-Currency
Ratings / Reviews
Recs Fallback Query
SortBy Options
Voice Search
Docs powered by
Archbee
How To

Recs Fallback Query

2min



In some cases, it can be desirable to apply a "fallback" query to Smart Recommendation banners where the applied logic might equate to no results found.

For example: when first installing Smart Recommendations "Checkout/Cart" logic for "Also bought Together" banners, there may not be enough initial data to qualify a result.

Any of the API options for Smart Recommendations can be applied to the fallback query.



Example

In the example below, a "Trending Products" (RECS_TRENDING) query replaces the target Recommendations banner if no results are found.

Note: Apply the target klevu-recs banner id to the klevu_recsFallBackId variable.

Note: Also adjust the templateData.metadata.title to match the intent of the fallback query.

JS
|
var klevu_recsFallbackId = "< ADD YOUR TARGET KLEVU-RECS ID HERE >";
var klevu_recsFallback = false;

/* Observer for record queries */
function klevu_processRECSRecordQueries(recordQueries, recsKey)
{
    var recsQuery = recordQueries[0];

    /* apply only to the specified banner recsKey */
    if (recsKey == klevu_recsFallbackId) {
    
        klevu.setObjectPath(recsQuery, "settings.fallbackQueryId", "myFallbackQuery");
        
        var myFallbackQuery = {
                      "id": "myFallbackQuery",
                      "isFallbackQuery": true,
                      "fallbackWhenCountLessThan": 1,
                      "typeOfRequest":"RECS_TRENDING",
                      "settings":{"typeOfRecords":["KLEVU_PRODUCT"],"limit":10}
        }
        
        recordQueries[1] = myFallbackQuery;
      
    }
}


/* Observer for query results */
function klevu_processRECSQueryResults(queryResults, recsKey) {
    
    /* apply only to the specified banner recsKey */
    if (recsKey == klevu_recsFallbackId) {
                    
        if (queryResults[0].meta.totalResultsFound == 0) {
            
          klevu_recsFallback = true;
            
          /* replace main results with fallback results */
          queryResults[0] = queryResults[1];
          queryResults[0].id = "klevuRECSItemList";
          
        }
    }
}

/* Observer to process template data */
function klevu_processRECSTemplateData(templateData, recsKey) {
        
    /* apply only to the specified banner recsKey */
    if (recsKey == klevu_recsFallbackId && klevu_recsFallback) {
                
        /* replace the title as desired (based on Fallback intent) */
        templateData.metadata.title = 'Trending Products (as Replacement)';
  
    }
}


See : Overview for more details on the functions used in the above example.

Updated 11 Jul 2023
Did this page help you?
PREVIOUS
Ratings / Reviews
NEXT
SortBy Options
Docs powered by
Archbee
TABLE OF CONTENTS
Example
Docs powered by
Archbee