Mia
Mia

Reputation: 41

How to add a variable to a native tpl file?

For example, in the product_details_full.tpl file, i want to add a variable whose value comes from item. How should you implement it? enter image description here In addition, I found a pageHeader variable in the tpl file, but I didn't see this variable in the Field Sets of the website. Why? Aren't the variables in the Field Sets and the variables in the tpl file one-to-one corresponding?

I know i can expose information from the item records in NetSuite to my website by defining field sets.But I don't know how to use the field set and in which tpl file it will be used

Upvotes: 2

Views: 90

Answers (1)

SCA Dev
SCA Dev

Reputation: 26

i want to add a variable whose value comes from item. How should you implement it?

First you add the value of the item field on the fieldset you're going to use. In this case based on your question, I'm assuming you're using it on the Product Details Page. In that case add the value on the "details" fieldset and you need to add the value to the view, in the "getContext" method.

Passing that value to the view will depend on which version of SuiteCommerce you're on, and what information you have available on the view already.

The product details page has the following variables available:

  • model
  • pageHeader
  • itemUrl
  • isItemProperlyConfigured
  • isPriceEnabled

This is the getContext method for that view:

    getContext: function() {
        const item_model = this.model.get('item');

        // @class ProductDetails.Base.View.Context
        return {
            // @property {Transaction.Line.Model} model
            model: this.model,
            // @property {String} pageHeader
            pageHeader: this.page_header,
            // @property {String} itemUrl
            itemUrl: item_model.get('_url') + this.model.getQuery(),
            // @property {Boolean} isItemProperlyConfigured
            isItemProperlyConfigured: item_model.isProperlyConfigured(),
            // @property {Boolean} isPriceEnabled
            isPriceEnabled: !ProfileModel.getInstance().hidePrices()
        };
        // @class ProductDetails.Base.View
    }

Since you're in the Product Details page, you may have the field available under the model variable already. Meaning if you add {{model.the-id-of-your-field}} in the template, the value you want could be already available for use (depends on the type of field and value). If this does not work you'll need to create an extension.

If you're using version 18.2 or newer, you can create an extension and use the "addToViewContextDefinition" method (how the "addToViewContextDefinition" works). If you've never used extensions, check how to develop your first extension.

If you're using an older version (Denali, Mont-Blanc, Elbrus, etc.), then you'll need to extend the View's getContext method. See how to develop your first SCA customization

I found a pageHeader variable in the tpl file, but I didn't see this variable in the Field Sets of the website. Why? Aren't the variables in the Field Sets and the variables in the tpl file one-to-one corresponding?

No, they do not correspond 1-1. The values specified in the fieldsets indicate which data is going to be passed on by Netsuite's API to SuiteCommerce, but they still might need to be added to the view's context to make them available to the template as explained above.

Upvotes: 1

Related Questions