Pulling custom fields from item details for ItemLabel

davidkdavidk Member Posts: 7
edited August 3 in Development

Hello,

I would like to know how to access the values in my items custom fields so that i can use them in my items label print template.

Thank you.

13 comments

  • davidkdavidk Member Posts: 7

    Hi Gregarican,

    Thank you for responding!

    I am not hitting the API though, I am creating a template through

    setting->advanced setup->print templates

  • gregaricangregarican Member Posts: 336 

    Hmmm. I know from the Lightspeed Retail web front-end, with a lot of products that have a lot of custom fields defined we experience slower performance. Not sure how that will be effected in relation to the print template. Also, I'm unsure whether or not you can access custom fields in the print templates. I'd be curious to see what Lightspeed Retail support come back with.

  • Adrian SamuelAdrian Samuel Moderator, Lightspeed Staff Posts: 573 moderator

    Hey @davidk,

    When you're customising the data to display onto a template, you are in fact using one of the DisplayTemplate API endpoints:

    In your case you're looking at the label template so you can read specifically about that template here: https://developers.lightspeedhq.com/retail/endpoints/DisplayTemplate-Label/

    Since tags aren't a feature of the Display Template endpoint you'll have to make an AJAX call to the Retail Item endpoint. This can be accomplished using Javascript

    Adrian Samuel

    API Integrations Consultant - Strategic Solutions

    Lightspeed HQ

  • davidkdavidk Member Posts: 7

    Thank you for your response @Adrian Samuel

    Can i make an AJAX call from a custom template in the Lightspeed print templates page?

    What do i use for website and redirect URI in the API signup?

    Thanks

  • Adrian SamuelAdrian Samuel Moderator, Lightspeed Staff Posts: 573 moderator

    Hey @davidk, when you're inside the templates, you don't need to manage server side authentication.

    I've created a javascript snippet for you to use to get the custom field data out. I'll leave it up to you to handle exceptions and display the HTML on the label as you like, but this should be a start:

    https://gist.github.com/Adrian-Samuel/2894d357a8f16a37676795df06f89def

    Adrian Samuel

    API Integrations Consultant - Strategic Solutions

    Lightspeed HQ

  • davidkdavidk Member Posts: 7

    Hi @Adrian Samuel , I keeping getting this response from the request builder:


    {

    "httpCode": "404",

    "httpMessage": "Not Found",

    "message": "Object of type 'Label' not found with primary ID '584'.",

    "errorClass": "Exception"

    }

  • Adrian SamuelAdrian Samuel Moderator, Lightspeed Staff Posts: 573 moderator

    @davidk, good catch!

    The template API for Label is "ItemAsLabel" instead of just "Label" which is distinctive and different from the other template endpoints which aren't prepended by anything. I've updated the snippet so you should find that it works now!

    As a result, the request builder function will only work for the Label Template for now. I might find some time to make it generally useful across the rest of the templates.

    Adrian Samuel

    API Integrations Consultant - Strategic Solutions

    Lightspeed HQ

  • davidkdavidk Member Posts: 7

    @Adrian Samuel it worked! Thank you very much.

    One last question... I have one more field i need to grab and it's the "Brand" field under Organize, how would i go about that? I've tried a few different variations with your script but haven't had any success.


  • Adrian SamuelAdrian Samuel Moderator, Lightspeed Staff Posts: 573 moderator

    @davidk, no worries! :)

    I've updated the snippet again and written a second function using the request builder to get the brand information. Hopefully it will serve as an example to get other pieces of data if you so desire it :)

    Adrian Samuel

    API Integrations Consultant - Strategic Solutions

    Lightspeed HQ

  • davidkdavidk Member Posts: 7

    @Adrian Samuel Amazing, thank you!!

    However ;)... the latest problem is that when i click 'Print Label' the print menu auto pops up too quickly and doesn't give my script time to render the fields. I have my script top of the page and added a timeout at the end of it but that didn't help.

    Thoughts?

  • Adrian SamuelAdrian Samuel Moderator, Lightspeed Staff Posts: 573 moderator
    edited August 12

    @davidk :)

    To stop the browser's auto-print prompt popping up straight away and blocking your script you'll need to amend the line in the label template code that says:

    {% extends parameters.print ? "printbase" : "base" %}

    And change it to:

    {% extends "base" %}

    You'll typically find it on line 114 on an unamended template as seen here:

    https://github.com/merchantos/PrintTemplates/blob/master/label/customizable_label.tpl#L114

    At the end of your javascript code you'll need to implement the print prompt yourself after your script does its job with a window.print()

    I hope this settles things for you

    On a side note, I've updated the request builder to work for all template endpoints (and all endpoints for that matter). I hope you enjoy using Lightspeed!

    Adrian Samuel

    API Integrations Consultant - Strategic Solutions

    Lightspeed HQ

  • davidkdavidk Member Posts: 7

    @Adrian Samuel Everything is working as required. Thank you Adrian, you have been very helpful!

Sign In or Register to comment.