Taking information from an array

Using the get order function from the thank you page https://www.wix.com/corvid/reference/$w.ThankYouPage.html#getOrder I managed to get the order details into another database. but the line items of the order are in an array like below.

[
  {
    "weight": 1.42,
    "name": "A great product",
    "quantity": 1,
    "sku": "sku",
    "lineItemType": "",
    "tax": 5,
    "price": 5,
    "translatedName": "a",
    "taxGroupId": "4",
    "totalPrice": 6,
    "priceData": {
      "totalPrice": 6,
      "price": 5,
      "taxIncludedInPrice": false
    },
    "options": [
      {
        "selection": "value",
        "option": "title"
      }
    ],
    "customTextFields": [
      {
        "value": "value",
        "title": "title"
      }
    ],
    "productId": "",
    "notes": "some notes",
    "mediaItem": {
      "altText": "a",
      "externalImageUrl": "http://static.wixstatic.com/media/289e7e_4d7e6ed33f324a0aa3e1821ee6142781~mv2.jpeg/v1/fill/w_50,h_50,al_c,q_90/file.jpg",
      "src": "wix:image://v1/289e7e_4d7e6ed33f324a0aa3e1821ee6142781~mv2.jpeg/file.jpg#originWidth=50&originHeight=50",
      "mediaId": "1",
      "type": "image"
    },
    "discount": 5,
    "index": 1
  }
]

Was wondering how to split the array to get all the information into separate items in a database? And corresponding to the order id also.

Hi,

What is the structure of the collection where you want to store this information. It would be helpful if you can post a screen shot of the collection. If the field names are exactly the same as this lineItems array, it is more straightforward. Otherwise, it will entail more coding. I take it that you can have multiple line items, right?

Hi @tony-brunsman currently the product item array shows like the screenshots attached.

And yes the original post above was just for a test order. Multiple line items will be in the order like the screenshot above. Currently multiple line items looks like the array code below. I would like to split this array to individual line items bought in another database corresponding to the order id.

[{"variantId":"00000000-0000-0000-0000-000000000000","weight":0,"name":"Glass","quantity":3,"sku":"364215375135191","lineItemType":"PHYSICAL","tax":0,"price":20,"translatedName":"Glass","totalPrice":60,"priceData":{"totalPrice":60,"price":20,"taxIncludedInPrice":false},"options":[],"customTextFields":[],"taxIncludedInPrice":false,"productId":"cd59cd36-b6d2-2cf3-9d48-81793a7bdbbd","mediaItem":{"mediaId":"a9ff3b_1eec973ae9eb43f4bca9876e5d90f6fa.jpg","src":"wix:image://v1/a9ff3b_1eec973ae9eb43f4bca9876e5d90f6fa.jpg/file.jpg#originWidth=1000&originHeight=1000","type":"IMAGE"},"discount":0,"index":1},
{"variantId":"00000000-0000-0000-0000-000000000000","weight":0,"name":"Watch","quantity":1,"sku":"364115376135191","lineItemType":"PHYSICAL","tax":0,"price":10,"translatedName":"Watch","totalPrice":10,"priceData":{"totalPrice":10,"price":10,"taxIncludedInPrice":false},"options":[{"selection":"White","option":"Color"}],"customTextFields":[],"taxIncludedInPrice":false,"productId":"c8539b66-7a44-fe18-affc-afec4be8562a","mediaItem":{"mediaId":"a9ff3b_119100d8c0144221b2f6733f4d205d2e.jpg","src":"wix:image://v1/a9ff3b_119100d8c0144221b2f6733f4d205d2e.jpg/file.jpg#originWidth=1000&originHeight=1000","type":"IMAGE"},"discount":0,"index":2},
{"variantId":"00000000-0000-0000-0000-000000000000","weight":0,"name":"Shoe","quantity":6,"sku":"364215376135191","lineItemType":"PHYSICAL","tax":0,"price":85,"translatedName":"Shoe","totalPrice":510,"priceData":{"totalPrice":510,"price":85,"taxIncludedInPrice":false},"options":[{"selection":"Brown","option":"Color"}],"customTextFields":[{"value":"","title":"Date"}],"taxIncludedInPrice":false,"productId":"df19c1f7-07d8-a265-42f8-e8dfa824cc6e","mediaItem":{"mediaId":"a9ff3b_9928686dcfa740bd802821d0b6f4ac03.jpg","src":"wix:image://v1/a9ff3b_9928686dcfa740bd802821d0b6f4ac03.jpg/file.jpg#originWidth=1000&originHeight=1000","type":"IMAGE"},"discount":0,"index":3}]

@team69619 Here is some code that will give you an idea of how to go about it. The idea is to create an array with the actual field names to store the values of the line items by looping through the order’s lineItems array. Once that is in place, then insert into the collection.

$w('#ThankYouPage').getOrder()
.then( (order) => {
    let orderId = order._id;
    let email = order.buyerInfo.email;
    let firstname = order.buyerInfo.firstName;
    let OrderIDItems = [], OrderIDItem = {}, lineItem = {};
    let lineItems = order.lineItems;
    for (var i = 0; i < lineItems.length; i++) {
        lineItem = lineItems[i];
        OrderIDItem = {"ordernum": orderId,
            "email": email, 
            "firstname":firstname,
            "productName": lineItem.name,
            "productQty": lineItem.quantity, 
            "productTotalPrice": lineItem.totalPrice};
        // Now, add the lineItem to the array
        OrderIDItems.push(OrderIDItem);
    }
    // Finally, put the records into the 
    let options = {"suppressAuth": true};
    wixData.bulkInsert("orderID", OrderIDItems, options)
    .then((bulkResult) => {
        console.log(bulkResult);
    })
})


1 Like

@tony-brunsman Thanks. This seems to have worked for me. Will update in the future if any issue.