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:
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) ?
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 <--
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. -->
<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. -->
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.
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
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 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 :)
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:
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
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:
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
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
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:
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
I just needed to update the jQuery carousel inside the Tea Commerce updateProduct function in javascript:
It now works as intended :)
Bjarne
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:
when I use:
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
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:
-Tom
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
Ok, I think a slight modification will fix that:
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.
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 :)
It then uses Product B1 and not the variant (child - size 30) ..
When using this it almost seems to be correct:
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 :)
I think this will work once and for all :)
Though it's messy I think it should work. Maybe Chriztian knows how to clean it up :)
-Tom
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
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 :)
Okay.. so you would do something like this?
<a href="{$relatedProductLink}">
...
</a>
That fixed it :)
Bjarne
is working on a reply...