Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Matt 186 posts 490 karma points
    Feb 23, 2019 @ 11:04
    Matt
    0

    Trying to add recently added section

    Hello all,

    So I'm making a basic website where a small shop can post there products on their site, I created sub categories for the products, but what I want to do now is have a list of recently added products on the main stock page.

    I'm really struggling to get anything to work.

    I've tried going right back to the basics but I'm just getting errors and nothing rendering?

    This is my current structure;

    enter image description here

    And this is the code I'm adding to my main "stock" template

        @{
        var selection = Model.Content.Site().FirstChild("stock").Children("products")
                            .Where(x => x.IsVisible())
                            .OrderBy("CreateDate desc");
    }
    <ul>
        @foreach(var item in selection){
            <li>
                <a href="@item.Url">@item.Name</a>
            </li>
        }
    </ul>
    

    Ooops it might help if I added the error;

    enter image description here

    Thanks in advance,

    Matt

  • Dirk De Grave 4532 posts 5976 karma points MVP 3x admin
    Feb 23, 2019 @ 11:21
    Dirk De Grave
    100

    Hi Matt,

    Regarding your error message, it says you've already defined the variable selection... obviously the code shown is not the complete template code, so it might be used/declared already elsewhere in the template.

    On to the technical part... in the assumption that "products" is the doc type alias of a product, the statement

    Model.Content.Site().FirstChild("stock").Children("products")
    

    will not work as product nodes in tree are NOT immediate children of your "Stock" node, but grand children, so you need to use

    Model.Content.Site().FirstChild("stock").Descendants("products")
    

    It'll work in your case and probably won't suffer from performance issues (if you don't have too many products/categories), but it's not the best approach, see https://our.umbraco.com/Documentation/Reference/Common-Pitfalls/#querying-with-descendants-descendantsorself

    --Dirk

  • Matt 186 posts 490 karma points
    Mar 31, 2019 @ 17:45
    Matt
    0

    Hmm I thought I had this working but seems its not quite there.

        @{
        Model.Content.Site().FirstChild("stock").Descendants("products")
                            .Where(x => x.IsVisible())
                            .OrderBy("CreateDate desc");
    }
    <ul>
        @foreach(var item in selection){
            <li>
                <a href="@item.Url">@item.Name</a>
            </li>
        }
    </ul>
    

    This is my code, but its picking up the categories e.g Lamps,Chairs,Tables rather then the products inside of those categories.

    What am I doing wrong?

    Thanks

  • Søren Kottal 409 posts 2462 karma points MVP c-trib
    Mar 31, 2019 @ 19:02
    Søren Kottal
    0

    Hi Matt

    Is the doctype for Lamps,Chairs,Tables by any chance "products"?

    You need to put your product-doctype into the Descendants call.

  • Matt 186 posts 490 karma points
    Mar 31, 2019 @ 19:29
    Matt
    0

    Hello,

    No the doctype for lamps etc is called "StockSubCat" the products doc type is called products

    Which is what I'm getting confused why it doesn't seem to work?

    Thanks

  • Chris Evans 92 posts 177 karma points c-trib
    Mar 31, 2019 @ 23:11
    Chris Evans
    0

    I think we may need to see your entire view, not just this section of it. I get the feeling you've already got a variable on the same view called selection which is what you're actually looping through in your foreach statement.

    can you try this:

        @{
        var products = Model.Content.Site().FirstChild("stock").Descendants("products")
                            .Where(x => x.IsVisible())
                            .OrderBy("CreateDate desc");
    }
    <ul>
        @foreach(var item in products){
            <li>
                <a href="@item.Url">@item.Name</a>
            </li>
        }
    </ul>
    
Please Sign in or register to post replies

Write your reply to:

Draft