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
    Nov 15, 2011 @ 17:55
    Bjarne Fyrstenborg
    0

    Get random product

    Hi..

    I am trying to get a random product to be shown on frontpage: http://sub.ak-security.dk/da/forside.aspx

    But I want to ensure only products with images in shown and not the variants e.g. Product A1 - Blue... just Product A1.. futhermore I have a problem with showing the price, where I have used the code from product page.

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
    <xsl:stylesheet 
      version="1.0" 
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
      xmlns:msxml="urn:schemas-microsoft-com:xslt"
      xmlns:umb="urn:umbraco.library"
      xmlns:emath="urn:Exslt.ExsltMath"
      xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets"
      xmlns:teacommerce="urn:teacommerce"
      exclude-result-prefixes="msxml umb emath umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets teacommerce">

      <xsl:output method="xml" omit-xml-declaration="yes"/>

      <xsl:param name="currentPage"/>
      <xsl:variable name="random" select="floor(emath:random() * count($currentPage/ancestor-or-self::root/descendant-or-self::Product)) + 1"/>

      <xsl:template match="/">
        <xsl:variable name="productImage" select="$currentPage/ancestor-or-self::root/descendant-or-self::Product/productImage/DAMP/mediaItem/Image"/>
        <xsl:if test="$productImage != ''">
         <xsl:apply-templates select="$currentPage/ancestor-or-self::root/descendant-or-self::Product[$random]" />
        </xsl:if>
      </xsl:template>

      <xsl:template match="Product">
        <div id="randProduct">
          <!-- Apply templates to the document properties -->
          <xsl:apply-templates select="productImage[normalize-space()]" mode="productimage"/>
          <xsl:apply-templates select="productName[normalize-space()]" mode="productname"/>
          <xsl:apply-templates select="productPrice[normalize-space()]" mode="productprice"/>
        </div>
      </xsl:template>
      
      <xsl:template match="*" mode="productimage">
          <xsl:variable name="productImage" select="$currentPage/ancestor-or-self::root/descendant-or-self::Product/productImage/DAMP/mediaItem/Image"/>

          <div>
            <img src="{concat('/ImageGen.ashx?Width=150&amp;Image=',$productImage/umbracoFile)}" id="productImage" alt="" title="" />
          </div>
      </xsl:template>
       
      <xsl:template match="*" mode="productname">
        <h2>
          <xsl:value-of select="." />
        </h2>
      </xsl:template>

      <xsl:template match="*" mode="productprice">
      <xsl:variable name="currentCurrency" select="teacommerce:GetCurrentCurrency()"/>
      <xsl:variable name="variant" select="./descendant-or-self::Product [not(child::Product)][1]"/>
        <!-- The product stock is fetched from either the product or the first variant -->
      <xsl:variable name="stock" select="teacommerce:GetStock($variant)" />
      <!-- The product prices is fetched in all currencies -->
      <xsl:variable name="prices" select="teacommerce:GetPrices($variant)" />
      <xsl:variable name="priceUnFormatted" select="$prices/* [name() = $currentCurrency/@ISOCode]/@price" />
      <!-- The product price with the current currency and formatted with the current culture -->
      <xsl:variable name="price" select="teacommerce:FormatPriceWithSpecificCulture($priceUnFormatted, $currentCurrency/@cultureName)" />

        <span class="{concat('currency', @id, $currentCurrency)}" id="productPrice" itemprop="offers" itemscope="" itemtype="http://schema.org/Offer">
              <xsl:choose>
                <xsl:when test="$stock = '' or $stock &gt; 0">
                  <meta itemprop="availability" content="http://schema.org/InStock" />
                </xsl:when>
                <xsl:otherwise>
                  <meta itemprop="availability" content="http://schema.org/OutOfStock" />
                </xsl:otherwise>
              </xsl:choose>
              
              <span itemprop="price"><xsl:value-of select="$price"/></span>
            </span>
      </xsl:template>
    </xsl:stylesheet>

    Bjarne

  • Anders Burla 2560 posts 8256 karma points
    Nov 15, 2011 @ 18:25
    Anders Burla
    1

    Hi Bjarne

    For the random products - I think I would go for a multi tree node picker from uComponents and have the shop owner select which products to get "random" from. The often know which products sell the best and which one the want to highlight - it shouldnt be true random. Now you will have a list of ids to choose from - and then its just plain old Tea Commerce as you know it. Get the product xml by id and do your magic :)

    Kind regards
    Anders

  • Bjarne Fyrstenborg 1284 posts 4038 karma points MVP 8x c-trib
    Nov 15, 2011 @ 18:48
    Bjarne Fyrstenborg
    0

    Hi Anders

    Yes, I was thinking if it should be complete random product or the shop owner should pick one.. I was exploring with the complete random product as I did with random quotes on http://oldengaard.dk ... but it would probably be better with a selected product by the shop owner, so it could be a best selling product, an offer or just a product they want to hightligt...

    If it should be really fancy the code would be more advanced and automatically select e.g. one of the top ten best selling product or one of the offers ... so the shop owner has three options: show best selling products, offers or pick a product.

    I will try with the more simple solution .. :)

    Bjarne

  • Anders Burla 2560 posts 8256 karma points
    Nov 15, 2011 @ 18:50
    Anders Burla
    0

    Yes it could be way more advanced - but true to keep it simple sometimes is enough :)

    Dint forget to mark the right answer as the solution :)

Please Sign in or register to post replies

Write your reply to:

Draft