website logo
⌘K
Introduction
PORTAL
Agency Whitelabelling
PAGES
Pages - Accessing Page Data
Pagination Layouts
Page Templates
FORMS
CATEGORIES
Filtering WebApps and Modules by Categories
Outputting Categories on WebApp / Module / eCommerce Layouts
Outputting Category Fields in any Location
Category Detail Layouts
FAQ - Categories
COMPANY INFORMATION
Company Information
SITE SEARCH
Site Search
PUBLIC API/ZAPIER
Zapier - Formatting arrays correctly
Public API/Zapier Changelog
MODULES
Module Marketplace
Building Custom Modules
Siteglide Modules
Front-end Submit Modules
DATA STRUCTURES
Creating WebApps from the CLI
Field Types
WEBAPPS
Front-end Submit WebApps
Layouts
Search and Filtering
Understanding Custom Field Names and IDs
FAQ - WebApps
CRM
User Details
User Secure Zones
Storing User's Favourite WebApp / Module Items
User's Form Submissions (Cases)
How Users Edit their Email and Password Front End
Editing a User's CRM record Front End with Custom Field Sets
CLI
Introducing Siteglide CLI
CLI Changelog
Secure Zones with CLI
Page Templates with Siteglide CLI
Pages with Siteglide CLI
Includes with Siteglide CLI
Managing Email Templates
Migrate - Manual Form setup
Migrate - Convert existing Forms
Liquid
Accessing Assets
Liquid Dot Notation
Using WebApp Collections- Tutorial
Using the current_user object with Secure Zones
Preventing Duplicate Content with Query Parameters- Canonical URL and Robots.txt
FAQ - Liquid
GraphQL
Tutorial Overview
About GraphQL
Tutorial 1- Your First Query
Tutorial 2 - Pagination
Tutorial 3 - Filtering the Results
Tutorial 3 - Answers to the First Filtering Challenge
Tutorial 4 - Advanced Filtering
Tutorial 4 - Challenge Answers
Tutorial 5 - Using Liquid to run GraphQL queries on your Site
Tutorial 6 - Variables
Tutorial 6 - Answers to the Variables Challenge
Tutorial 7 - Sorting
Tutorial 8 - Building a Liquid API GET Endpoint Page powered by GraphQL queries
Best Practice and Performance
Module/WebApp Caching
Getting Started with Liquid Caching - to Reduce Server Response time and Improve Performance
Includes
ecommerce/checkout_standard
Frequently Asked Questions
Using Liquid Logic to Check if a field exists, or is empty on the Front End
How do I learn more about Liquid?
How to setup a multi domain start page
Docs powered by archbee 

Storing User's Favourite WebApp / Module Items

21min

Users can "add" or "remove" their favourite WebApp and Module items. You can display them to the User in Lists.

Document image


Prerequisites

  • You have installed the Secure Zones Module
  • You have added a User Sign Up Form
  • A User has logged in

Introduction

We've added the ability for User's to store their favourite Module items within a "favourite_items" array, they'll be able to "add" and "remove" items from the list which will be stored alongside the rest of the CRM data available to that User in user_details. Possible Use Cases for this feature include:

  • A wishlist of Products
  • Bookmarks of favourite Pages
  • Giving feedback on parts of a Site the User found helpful.

Syntax - Including the User Favourites Toggle Button Layout

This Layout will allow the User to add or remove an Item from their favourites.

Note

The User Favourites Toggle Layout will only work within an item.liquid file (for Modules) or a WebApp Layout file that has access to {{this.id}}.

Use this Liquid to include your User Favourites Layout, the only parameter that'll differ for this is the "layout", here you can specify a custom layout: {% include 'user_favourites_toggle', layout: 'default' -%} is_favourite - you can use this within user_favourites_toggle layouts to determine if the Module/ Webapp item is already added to the Users favourites like so:

HTML
|
{% if is_favourite != true  %}
    <!-- output add to favourites button -->
{% else %}
   <!--  output remove from favourites button -->
{% endif %}


Arguments

Our default Layout contains two buttons which we'd recommend you use for reference when setting this up. Let's have a look at these buttons and what they're doing:

Add to Favourites Button

JS
|
<button class="btn btn-success"
 onClick="user_add_to_favourites({{this.favourite_item}},added_success,favourite_toggle_failed)">
 Add to favourites
 </button>


Remove from Favourites Button

JS
|
<button class="btn btn-danger" 
onClick="user_remove_from_favourites({{this.favourite_item}},removed_success,favourite_toggle_failed)">
Remove from favourites
</button>


Required Arguments for both Buttons

To keep things simple, we've bundled the arguments you need to pass into the function into a single Liquid variable containing a JavaScript array {{this.favourite_item}}.

Advanced- modifying the arguments

If you need to modify them, you can split this into following the 4 items in the JavaScript array:

JS
|
[
  "{{this.id}}",  //contains the ID of the Module item being added/ removed.
  "{{this.name}}", //contains the name of Module item being added/ removed.
  "{{context.current_user.id}}", // ID of the User adding/ remove the Favourite item.
  "{{context.authenticity_token}}" // token given to the User, this is required.
]


A note on security

Even if a malicious User submits a fraudulent User ID argument, we will double check on the server-side, so it won't be possible for them to change the favourites of other Users.

Custom Callback Functions

You can also pass success/ failure functions to the add/ remove buttons.

Step 1) Define your functions

JS
|
<script>
  function custom_success_function(type, name, button_element) {
   typeWord = type == 'add' ? "added" : "removed";
   alert('Success! You have ' + typeWord + ' ' + name + 'to favourites'  );
  }
  function custom_error_function(error) {
    alert('error');
  }
</script>


Available arguments for success callback functions:

  • type - add/ remove - whether item was added or removed from favourites
  • name - name of item added/ removed
  • button_element - element that fired toggle function Available arguments for error callback functions:
  • error - error message detailing why function failed

Step 2) Add the name of your success function as the second argument to the toggle button's function, and your error function as the third.

If you add an error function, you must add a custom success function. Note it's important to only add the names of functions at this step- this means without the usual round brackets which would contain arguments- that's because we only want to store the functions now and call them later.

JS
|
<button class="btn btn-success" 
onClick="user_add_to_favourites({{this.favourite_item}},custom_success_function,custom_error_function)">
Add to favourites
</button>


Hiding add/ remove buttons if User isn't logged into Secure Zone

You may want to hide the add/ remove buttons from the User if they aren't logged into a Secure Zone you could do so by wrapping your user_favourites Layout within this IF statement: {% if context.current_user %} {% endif %}

Syntax - Outputting User's Favourite Module/ Webapp items

Now let's look at how we can output all of the User's Favourite items. The favourite_items object is stored within the User Details Layout.

You can include the user_details Layout like so: {% include 'user_details', layout: 'my_layout' -%} Layouts for user_details are stored under the following path: site Manager/code Editor/layouts/modules>module_5/user_details/my_layout.liquid Next within the "user_details" Layout, you can use the following Liquid variables:

  • favourite_items - an array of IDs of favourite WebApp and Module Items you can loop over. This is mostly for use in your logic.
  • favourite_items_string - a comma-separated String of favourite WebApp and Module Items. This is formatted ready to be passed into an item_ids parameter for filtering WebApp and Module List views. See below.

Outputting Webapp favourite items

{%- include 'webapp', id: '1', layout: 'my_layout', item_ids: favourite_items_string -%}

Outputting favourite Products

{%- include 'ecommerce/products', layout: 'default', item_ids: favourite_items_string -%}

Outputting favourite Blog Posts (Modules)

{%- include 'module', id: '3', layout: 'my_layout', item_ids: favourite_items_string -%}

Updated 11 Apr 2023
Did this page help you?
Yes
No
PREVIOUS
User Secure Zones
NEXT
User's Form Submissions (Cases)
Docs powered by archbee 
TABLE OF CONTENTS
Prerequisites
Introduction
Syntax - Including the User Favourites Toggle Button Layout
Note
Arguments
Add to Favourites Button
Remove from Favourites Button
Required Arguments for both Buttons
Advanced- modifying the arguments
A note on security
Custom Callback Functions
Step 1) Define your functions
Step 2) Add the name of your success function as the second argument to the toggle button's function, and your error function as the third.
Hiding add/ remove buttons if User isn't logged into Secure Zone
Syntax - Outputting User's Favourite Module/ Webapp items
Outputting Webapp favourite items
Outputting favourite Products
Outputting favourite Blog Posts (Modules)