Scripts
The Script resource allows an app to register a custom Javascript file to be loaded and executed in the Merchant's storefront page. More precisely, on products (store) and payment (checkout) pages.
This is necessary when apps, besides its page on the Merchant's admin, must also impact the merchant store web page in some manner. For example, an App that recoveries user's carts might want to display a dialog component in which the user can see previous carts and choose which one to recover.
The script is inserted in the HTML code of the Merchant's webpage in a <script> html tag the decision process of loading a custom script or not is based on the script configuration that will be soon explained.
Apps that require a script to work fully should be registered in the Partner's portal with the scripts scope permission assigned. This permission will be added to the apps scopes and guarantees that every time a merchant decides to install the app, they will be required to accept a term of use that informs them that an additional script will be run in their storefront. Apps without this permission won't be able to have scripts being associated.
Important
- It's the Partner's responsibility to guarantee that the script doesn't generate errors in the Merchant's page
- Once an App is uninstalled, the script won't be loaded anymore
- The script should not depend on any JavaScript available in the store's theme. Not even jQuery.
- Other Apps might also include their own scripts
- Tiendanube layouts have HTML selectors so modifications can be made by scripts while the original theme code stays the same
- The script is injected in the merchant storefront and a store (id) parameter is sent along with it
<script type="text/javascript" src="https://apps-scripts.tiendanube.com/app-handle/script-handle/2.js?versionId=c1285a470f0fc8f14f54851c5d8eb32f&store=1234"></script>- Observation: the
versionIdis an internal Tiendanube's ID that can be ignored. The important information in this example is the parameterstore=1234
- Observation: the
Script code
The script code will be loaded in the Merchant's storefront and executed in the client's browsers. It can modify the HTML of the page using HTML selectors or execute AJAX calls.
Considerations
Clojure
Ideally, your javascript should be inside a closure to avoid any external conflicts:
(function () {
// Your JavaScript
})();
AJAX
Use AJAX to load store configurations by accessing a URL with the store ID. The app will return store settings in JSON format for use in your JavaScript.
JQuery
In case jQuery is necessary, it should be accessed by the useJquery method. Some stores already have jQuery in their themes, but it's not necessarily the latest version.
Some store's themes includes their own version of JQuery, which might vary. If the store theme doesn't use it, then a default recent version will be provided (3.6)
useJquery is a Promise and resolves once jQuery is loaded.
useJquery().then( (jq) => {
console.log(`I'm using jQuery version ${jq().jquery}`)
// MyApp.init(jq);
});
Important: This function ensures jQuery is available, but it doesn't ensure its version.
Variables
A Javascript object (called LS) with some common variables is available in the Script context.
The object contains general Tiendanube's domain information that might be relevant for the script implementation.
Each storefront page has its own LS object definition
Store Page
var LS = {
store : {
id : /* Store's id */,
url : /* Store's URL */
},
cart : {
subtotal : /* Cart's subtotal in cents */,
items : [
/* For every cart item we have */
{
id: /* Product Variant's id */,
name: /* Product Variant's name */,
unit_price: /* Product Variant's price in cents */,
quantity: /* Quantity to be purchased */,
requires_shipping: /* True if product requires physical shipping */
}
],
has_shippable_products : /* True if at least one product requires physical shipping */,
has_non_shippable_products : /* True if at least one product doesn`t require physical shipping */
},
lang : /* Current language's code (e.g. pt_BR) */,
currency : {
code: /* Current currency in ISO 4217 format */,
display_short: /* Currency format string when the currency is not specified.*/,
display_long: /* Currency format string when the currency is specified.*/,
cents_separator: /* Symbol used for separating cents */,
thousands_separator: /* Symbol used for separating thousands (could be blank) */
},
country : /* Current currency in ISO 3166-1 format */,
customer : /* Current customer id or null if there is no logged-in customer */,
theme : {
code: /* Current theme's code */,
name: /* Current theme's name */
}
}
Product Page
LS.product = {
id : /* Product's id */,
name : /* Product's name */,
tags : /* Array of product's tags */,
requires_shipping : /* True if product requires physical shipping */
};
LS.variants = /* JSON encoded representation of the product variants */;
Category Page
LS.category = {
id : /* Category's id */,
name : /* Category's name */
};
Checkout Page
Important: Mandatory migration for Checkout scripts by 10/30. If your application uses scripts in the Checkout, you must migrate them to the new SDK to ensure they continue working after that date. Learn more about NubeSDK
var LS = {
store : {
id : /* Store's id */,
url : /* Store's URL */
},
cart : {
subtotal : /* Cart's subtotal in cents */,
items : [
/* For every cart item we have */
{
id: /* Product Variant's id */,
name: /* Product Variant's name */,
unit_price: /* Product Variant's price in cents */,
quantity: /* Quantity to be purchased */,
}
]
},
customer : /* Current customer id or null if there is no logged-in customer */,
lang : /* Current language's code (e.g. pt_BR) */,
currency : /* Current currency in ISO 4217 format */
}
Note: You cannot access this variable from the JavaScript file where developers can create their own Payment Options.
Thank you page
LS.order = {
id : /* Order's id */,
number : /* Order's number */,
hash : /* Order's hash */,
created_at : /* Order's creation date */,
coupon : /* Array of coupon codes that apply to this order */,
discount : /* Order's discount in cents */,
total : /* Order's total in cents */,
total_in_usd : /* Order's total in USD in cents */,
gateway : /* Payment Gateway's code */
};