Macro to get document data from shopping cart

Jarrod Lawson asked on August 16, 2022 06:17

I have a custom field that is on the document (that is also binding to an SKU) called VideoURL.

I can get this value on it's own with {% Documents["/Store/Webinars/Product-A"].VideoURL%}

However when I try to access it by joining from the shopping cart to document, it doesn't work

  {% foreach(item in ContentTable) { %}
  {% foreach (cmsdocument in Documents) { %} 
  {% if(item.SKUID == cmsdocument.NodeSKUID) { %} 
      <td>{% cmsdocument.DocumentName %}</td> <!-- this works I.e. Advanced Family Law -->                      
      <td>{% cmsdocument.VideoURL %}<td> <!-- this doesnt work should be https://www... -->
  {% } #%}
  {% } #%}
  {% } #%}

Any Suggestions as to why it's not working in this way? and any better advice to get from the shopping cart context (invoice) to get document data out

Also to add - the VideoURL field exists as a property on all the items, however in some cases it's empty with no data

thanks Jarrod

Correct Answer

Jarrod Lawson answered on August 17, 2022 01:50

Kentico Support was able to assist -

When you get a document by its Node Alias Path, as in the first example you shared, the macro engine can automatically resolve the type of the page, and include pagetype-specific (coupled) data, because there's only one page to worry about. When you're using the full Documents set, however, it contains all of the documents, spanning several types, so by default it does not include the coupled data. You can work around this by calling the .WithAllData extension. However, there's a drawback to that— if you're resolving all of the documents this way, before looping through them, for every single item in the content table, it will be pretty slow and take a lot of resources. I'd recommend filtering the Documents call beforehand, with the Where extension method. It should look something like this:

Documents.Where("NodeSKUID = " + item.SKUID).WithAllData.FirstItem.VideoURL

The Where method tells the macro engine to only pull the data of documents that meet that condition. The WithAllData extension tells it to pull pagetype-specific data as well as generic document data. The FirstItem extension tells it to pick the first one, in case multiple documents are associated with the same SKU. This also allows you to avoid the nested for loop. If you want to access multiple properties without resolving the document each time, you can set it to a variable before accessing VideoUrl.

e.g. {%myDoc = Documents.Where("NodeSKUID = " + item.SKUID).WithAllData.FirstItem %} then later {%myDoc.VideoURL%}

0 votesVote for this answer Unmark Correct answer

   Please, sign in to be able to submit a new answer.