Problems, need help? Have a tip or advice? Post it here.
3 posts Page 1 of 1
Hi, I need to ask for some help. I'm modifying a Couch site to use https://snipcart.com/, they seem to work well together. However, I'm getting a bit out of my depth with webhooks and JSON. I have belatedly discovered that my client uses a a fulfilment partner, and they need to be able to access the snipcart data. So I have set up a webhook, which seems to work. I can receive the JSON, and have managed to store it in Couch.
My problem is that I have only ever used JSON in a very limited way. I can access some of the data, but I am struggling the deeper I get. This post has been helpful viewtopic.php?f=5&t=10892#p27841

Here is the JSON (don't worry about the formatting - it's fine, I have deleted a lot for demonstration purposes)
Code: Select all
{
"eventName":"order.completed",
    "mode":"Test",
    "createdOn":"2021-03-15T16:36:30.7898192Z",
"content":{
"discounts":[],
"items":[
    {
    "name":"Product 1",
    "price":55,
    "quantity":1,
    "id":"387",
    "customFields":[]
    },
   
    {
    "name":"Product 2",
    "price":30,
    "quantity":1,
    "id":"403",
    "customFields":[
    {
    "name":"Size",
    "placeholder":"",
    "displayValue":"Large",
    "type":"dropdown",
    "options":"Small|Medium|Large",
    "required":false,
    "value":"Large",
    "operation":null,
    "optionsArray":["Small","Medium","Large"]}],
    "unitPrice":30,
    "hasDimensions":false,"hasTaxesIncluded":false,
    "totalPriceWithoutDiscountsAndTaxes":30}
    ]
    }
    }


And here is my code:

Code: Select all
<cms:capture into='orders' is_json='1' ><cms:show test_field /></cms:capture>

    <cms:each orders.content.items>
    <cms:if "<cms:is_array item />">
        <cms:each item>
            <cms:if key="name">Item: <cms:show item/><br></cms:if>
            <cms:if key="description">Desc: <cms:show item/><br></cms:if>
            <cms:if key="price">Price: <cms:show item/><br></cms:if>
            <cms:if key="quantity">Quantity: <cms:show item/><br></cms:if>
           
               
            <cms:if (key="customFields") && ("<cms:is_array item />")>
               
                <cms:each item>
                <cms:if key="name">Options: <cms:show item/><br></cms:if> //Nothing shows here
                -- <cms:show key /> (<cms:array_count item />)<br> //Shows -- 0 (9) (Why is key 0? This is maybe where my problem is)
 
               </cms:each>   
                   
            </cms:if>
           
        </cms:each>
       
    </cms:if> 
        <hr>
</cms:each>


Getting and displaying Name, description, price, quantity is fine. My problem is - I can't figure out how to extract the sub-array stored in Custom Fields. I'm sure it's not a big deal, I'm just not great with dealing with data like this - it makes my head hurt... Any assistance would be great!
Hi @ewanmc!

SnipCart is great! :) And Couch arrays are one of my favorite features here.

I think your error is in losing track of how, where, and what you’re iterating, and there are maybe some unnecessary if statements that - if removed - could help focus the intention.

Naming your items explicitly during the each loops may also help keep track of where you are in the scope of things. You might try something closer to this:

Code: Select all

<cms:each orders.content.items as="item">

  Item: <cms:show item.name /><br />
  Desc: <cms:show item.description /><br />
  Price: <cms:show item.price /><br />
  Quantity: <cms:show item.quantity /><br />

  Options:<br />
  <cms:each item.customFields as="custom_field">
    <cms:show custom_field.name /><br />
    <cms:each custom_field.options as="option">
      -- <cms:show option /><br />
    </cms:each>
  </cms:each>

  <hr>

</cms:each>



It looks like you’re also trying to show the count of something, but I’m not clear on what that is. Maybe you can clarify? <cms:array_count /> is probably the way to go, assuming you’re in the right scope.

Hope that helps,

Matthew
Wow, that's exactly what I needed! I just don't have the experience, with JSON, but mainly with Couch arrays! This will get me exactly what I need to store the orders in Couch properly. Focus was definitely my problem, I knew the if statements were not the optimal way to get what I wanted, I just didn't know a better way to do it. (The count was just there to show that the data was there, it was just that I didn't know how to access it...)
Thanks so much Martin. I know who to turn to next time I'm struggling with Snipcart integration ;)
3 posts Page 1 of 1