Copied to clipboard

Flag this post as spam?

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


  • Bjarne Fyrstenborg 1284 posts 4038 karma points MVP 8x c-trib
    Jan 06, 2012 @ 19:17
    Bjarne Fyrstenborg
    0

    Get children of a parent node

    Hi..

    I'm trying to get children of a parent node (ProductCategory) and show the related products (Product) on a product page.. but I also need to get the same parent when the child is on a deeper level.

    In my content structure I have this:

    I get the related products this way for e.g. Product B1:

    <xsl:variable name="relatedProducts" select="$currentPage/parent::ProductCategory/Product [@id != $currentPage/@id]"/>
    

    So on product page I get the Product B2 and Product B3 shown at bottom of the page:
    http://sub.ak-security.dk/da/shop/category-b/product-b1.aspx

    The problem is when changing the product variant, it is using a child Product node (Size 30, Size 40, Size 50) and on these pages the related products isn't showed:
    http://sub.ak-security.dk/da/shop/category-b/product-b1/size-30.aspx

    The relatedProducts variable probably only go one level up I think.. 

    How can I get the product nodes (B1, B2, B3) whether the currentpage is B1 product node or a child for the sizes (both Product and variant node use the Product doc type) ?

    Bjarne

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Jan 06, 2012 @ 20:11
    Tom Fulton
    1

    Hi Bjarne,

    The problem is your xpath is using the parent axis which always gets the first parent.  Instead you should use ancestor, which will get all ancestors:

    <xsl:variablename="relatedProducts"select="$currentPage/ancestor::ProductCategory/Product [@id != $currentPage/@id]"/>

    Or if this is being used on the category page also you could use ancestor-or-self instead.  Check out Chriztian's axes visualizer for more info

    -Tom

  • Bjarne Fyrstenborg 1284 posts 4038 karma points MVP 8x c-trib
    Jan 06, 2012 @ 21:23
    Bjarne Fyrstenborg
    0

    Hi Tom

    If I use ancestor instead of parent I get to related products here: http://sub.ak-security.dk/da/shop/category-b/product-b1.aspx on the product itself.. but on the variant page I also get the product itself .. perhaps because it is ancestor of variant node: http://sub.ak-security.dk/da/shop/category-b/product-b1/size-30.aspx where I don't want to product itself (Product B1) in the related products..

    So when I change the variant here: http://sub.ak-security.dk/da/shop/category-b/product-b1.aspx I get three instead of two products..

    Thanks for the link to the visualizer, it makes it more clear :)

    Bjarne 

  • Bjarne Fyrstenborg 1284 posts 4038 karma points MVP 8x c-trib
    Jan 06, 2012 @ 21:58
    Bjarne Fyrstenborg
    0

    I think it also took the variant nodes, which use the same Product doctype.. and it was the three variants I get..

    I have done this instead:

    <xsl:variable name="relatedProducts" select="$currentPage/ancestor::ProductCategory/Product [not(child::Product)] [@id != $currentPage/@id]"/>
    

    So now I only get a the first level of Products.. not children of the Products..

    I now get the same products here: http://sub.ak-security.dk/da/shop/category-b/product-b1.aspx and here: 

    http://sub.ak-security.dk/da/shop/category-b/product-b1/size-30.aspx
    http://sub.ak-security.dk/da/shop/category-b/product-b1/size-40.aspx
    http://sub.ak-security.dk/da/shop/category-b/product-b1/size-50.aspx 

    The only thing is that I get the arrows when I change the variants, but I think it's a javascript problem when updating the variant..

    Thanks for your help :)

    Bjarne

     

  • Bjarne Fyrstenborg 1284 posts 4038 karma points MVP 8x c-trib
    Jan 06, 2012 @ 22:25
    Bjarne Fyrstenborg
    0

    I just needed to update the jQuery carousel inside the Tea Commerce updateProduct function in javascript:

    $('#carousel ul').carouFredSel({
              prev'#prev',
              next'#next',
              pagination"#pager",
              autofalse,
              pauseOnHovertrue,
              items {
                visible4,
              },
              scroll {
                items1,
                duration1000,
              }
    });

    It now works as intended :)

    Bjarne

  • Bjarne Fyrstenborg 1284 posts 4038 karma points MVP 8x c-trib
    Jan 07, 2012 @ 23:10
    Bjarne Fyrstenborg
    0

    I was a bit too fast..

    with this I get the related products.. but on the product without variants, it doesn't show to product with variants:

     

    <xsl:variable name="relatedProducts" select="$currentPage/ancestor::ProductCategory/Product [not(child::Product)] [@id != $currentPage/@id]"/>

     

    when I use:

    <xsl:variablename="relatedProducts"select="$currentPage/ancestor::ProductCategory/Product [@id != $currentPage/@id]"/>

    I get the variant product when I view products without variants, but when I change the variant the product itself is displayed: http://sub.ak-security.dk/da/shop/category-b/product-b1.aspx

    I think it is because the product without variants just use the product node.. where the variant product uses the child (variant)..

    Category B
    - Product B1
      - Size 30    <-- view this as default product and is used in related products
      - Size 40
      - Size 50
    - Product B2 <--
    - Product B3 <--

    Bjarne

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Jan 08, 2012 @ 20:08
    Tom Fulton
    0

    Hi Bjarne,

    Ok, so in the example above, you just want to return "Size 30", "Product B2", and "Product B3"?  I think this may work for you:

    <xsl:variable name="possibleProducts" select="$currentPage/ancestor::ProductCategory/Product"/> <!-- just a variable with all the products to make the below more readable -->
    <xsl:variable name="relatedProducts" select="$possibleProducts [not(child::Product)][@id != $currentPage/@id] | $possibleProducts [child::Product]/Product [@id != $currentPage/@id][1]"/> <!-- all products that don't have a child product, and the first child of products who do have child products. -->

    -Tom

  • Bjarne Fyrstenborg 1284 posts 4038 karma points MVP 8x c-trib
    Jan 08, 2012 @ 20:44
    Bjarne Fyrstenborg
    0

    Hi Tom

    Yes exactly.. one small thing is that, I only want to the the first variant in the related products.

    Here I don't want to show other variants of Product B1 in the related products: http://sub.ak-security.dk/da/shop/category-b/product-b1.aspx

    Bjarne

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Jan 08, 2012 @ 20:48
    Tom Fulton
    0

    Ok, I think a slight modification will fix that:

    <xsl:variable name="possibleProducts" select="$currentPage/ancestor::ProductCategory/Product"/> <!-- just a variable with all the products to make the below more readable -->
    <xsl:variable name="relatedProducts" select="$possibleProducts [not(child::Product)][@id != $currentPage/@id] | $possibleProducts [child::Product][@id != $currentPage/@id]/Product [@id != $currentPage/@id][1]"/> <!-- all products that don't have a child product, and the first child of products who do have child products. -->

     

  • Bjarne Fyrstenborg 1284 posts 4038 karma points MVP 8x c-trib
    Jan 08, 2012 @ 20:55
    Bjarne Fyrstenborg
    0

    Almost :)

    It seems to be fine here: http://sub.ak-security.dk/da/shop/category-b/product-b1.aspx

    but when I change the variant the extra prouduct is added in the related products, cause when changing a variant it uses the child node.
    So I need to only display the two other products here: http://sub.ak-security.dk/da/shop/category-b/product-b1/size-30.aspx where it shouldn't display Product B1. 

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Jan 08, 2012 @ 21:07
    Tom Fulton
    0

    Ah I see...I'm mobile now so can't really type, but what you should do is adjust the predicate in the second statement (after the |) to check the parent product as well. Something like

    ,/ancestor-or-self::Product/@id !=$currentPage/@id

    If not I can take a look when back at a real pc :)

  • Bjarne Fyrstenborg 1284 posts 4038 karma points MVP 8x c-trib
    Jan 08, 2012 @ 23:11
    Bjarne Fyrstenborg
    0

    It then uses Product B1 and not the variant (child - size 30) ..

    When using this it almost seems to be correct:

    <xsl:variable name="possibleProducts" select="$currentPage/ancestor::ProductCategory/Product"/>
    <xsl:variable name="relatedProducts" select="$possibleProducts [not(child::Product)][@id != $currentPage/@id] | $possibleProducts [child::Product] [@id != $currentPage/@id]/Product [@id != $currentPage/@id][1]"/>

    Just on the variant page: http://sub.ak-security.dk/da/shop/category-b/product-b1/size-30.aspx it uses the second child node (size 40) and on the other two variants: http://sub.ak-security.dk/da/shop/category-b/product-b1/size-40.aspx and http://sub.ak-security.dk/da/shop/category-b/product-b1/size-50.aspx they display first child node (variant 30)

    So on these three pages, child nodes, it shouldn't display Product B1 :)

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Jan 09, 2012 @ 14:51
    Tom Fulton
    1

    I think this will work once and for all :)

      <xsl:variable name="possibleProducts" select="$currentPage/ancestor::ProductCategory/Product"/>
      <xsl:variable name="relatedProducts" select="
        $possibleProducts
        [not(child::Product)]
        [@id != $currentPage/@id]
        |
        $possibleProducts
        [child::Product]
        [@id != $currentPage/@id]
        /Product
          [@id != $currentPage/@id]
          [($currentPage/parent::Product and parent::Product/@id != $currentPage/parent::Product/@id) or (not($currentPage/parent::Product))]
          [1]"/>

    Though it's messy I think it should work.  Maybe Chriztian knows how to clean it up :)

    -Tom

  • Bjarne Fyrstenborg 1284 posts 4038 karma points MVP 8x c-trib
    Jan 09, 2012 @ 15:05
    Bjarne Fyrstenborg
    0

    Thanks Tom.. it works :) ... it was kindy tricky.. but also because Product and variant use the same doctype.. it would probably be easily if the variants had it's own doc type..

    I will try to change the link from Product B1 - size 30 http://sub.ak-security.dk/da/shop/category-b/product-b3.aspx to the Product B1 .. like the link is to the product in product list: http://sub.ak-security.dk/da/shop/category-b.aspx

    I'm glad you would take your time to help me :)

    Bjarne

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Jan 09, 2012 @ 15:07
    Tom Fulton
    0

    Not a problem - glad it's working!

    I think the easiest way to handle that would be inside your loop, check if it's a variant (./parent::Product means it's a variant) and adjust your links accordingly :)

  • Bjarne Fyrstenborg 1284 posts 4038 karma points MVP 8x c-trib
    Jan 09, 2012 @ 15:37
    Bjarne Fyrstenborg
    1

    Okay.. so you would do something like this?

    <xsl:variable name="relatedProductLink">
      <xsl:choose>
        <xsl:when test="./parent::Product/@id">
           <xsl:value-of select="umbraco.library:NiceUrl(./parent::Product/@id)" disable-output-escaping="yes"/>
        </xsl:when>
        <xsl:otherwise>
           <xsl:value-of select="umbraco.library:NiceUrl(@id)" disable-output-escaping="yes"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <href="{$relatedProductLink}">
    ...
    </a>

    That fixed it :)

    Bjarne

Please Sign in or register to post replies

Write your reply to:

Draft