PO Receipt - Adding a condition

weemoweemo Member Posts: 8
edited February 7 in Tips, Tricks & How-to’s

Hi there. Is it possible to add a condition that would change the Receipt H2? Like this:


{% if OrderLine.Quantity = 1 %}

<h2 class="u-margin-Vn">

SAMPLE REQUEST<small> #{{ Order.orderID }}</small>

</h2>

{% else %}

<h2 class="u-margin-Vn">

Purchase Order<small> #{{ Order.orderID }}</small>

</h2>

{% endif %}


by the way, the code as it is does not work. But thats what I am trying to accomplish. Thnks

5 comments

  • Adrian SamuelAdrian Samuel Moderator, Lightspeed Staff Posts: 565 moderator
    edited February 7

    @weemo much is possible.

    Might be easier to do in Javascript, but twig is also acceptable.

    My question is, when you write

    {% if OrderLine.Quantity %}

    Do you mean the total quantity of all lines on the Order?

    Adrian Samuel

    API Integrations Consultant - Strategic Solutions

    Lightspeed HQ

  • weemoweemo Member Posts: 8

    Yes so if there are more than one product, quantity has to be 1 as well since we can only order 1 sample per item. Thanks

  • weemoweemo Member Posts: 8

    sorry, each line has to be qty 1 otherwise it goes back to Purchase Order as opposed to Sample

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

    Hey @weemo, thank you for clarifying! You have two options to explore to accomplish this:

    Option 1: Twig - Uses the Display Template API to access data (slightly faster)

    <h2 class="u-margin-Vn">
    {% set greater_than_one = false %}
    {% for OrderLine in Order.OrderLines.OrderLine %}
    {% if (OrderLine.quantity|number_format) > 1 %}
    {% set greater_than_one = true %}
    {% endif %}
    {% endfor %}
    {% if greater_than_one  == true %}
    Purchase Order 
    {% else %}
    Sample Request
    {% endif %}
    <br>
     <small>#{{ Order.orderID }}</small>
    </h2>
    

    Here we initialise a variable to a boolean value of false.

    Then we loop through all the lines and check if the quantity ordered is greater than one.

    If any one of those Order Lines has an ordered quantity greater than one then we set the initialised "greater_than_one" variable to true.

    We then use that conditional to then specify whether we print our "Purchase Order", otherwise we print Sample request because not one of those lines ordered was greater than one.

    This code sits between the <h2> tag as you can see.


    Option 2: Javascript - Uses the Rendered DOM to access data (slightly slower)

    document.addEventListener('DOMContentLoaded', () => {
        const titleChange = [...document.querySelectorAll('body > table:nth-child(8) > tbody > tr > td:nth-child(4)')]
            .every(x => x.innerText == 1);
        const header = document.querySelector('body > header > h2');
        const purchaseOrderNumber = header.innerText.split(' ').slice(-1)
        if (titleChange) header.innerHTML = `Sample Request <br> <small>${purchaseOrderNumber}</small>`;
    })
    

    This code can be dropped into the template under the following element

    <header class="col-spacerL-md6 col-md6 u-margin-Bxl">
    

    This code inserts an event listener on the body and waits for the HTML to load before it then makes the changes. It then searches every field in the quantity table data cells and checks if every value is equal to 1.

    If it does, it then inserts Sample request, otherwise it won't do anything and it will leave the default Purchase Order string there.


    Choose whichever option that suits you, I hope you have a great day!

    Adrian Samuel

    API Integrations Consultant - Strategic Solutions

    Lightspeed HQ

  • weemoweemo Member Posts: 8

    Thanks Adrian!

Sign In or Register to comment.