Copied to clipboard

Flag this post as spam?

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


  • Dinovo 40 posts 59 karma points
    Dec 13, 2009 @ 10:15
    Dinovo
    0

    Remove value in string/cookie based on position()?

    I am building a simple eComerce solution where the basket is populated via nodeId's in a cookie (comma seperated).

    So i could have a value of a cookie looking like this; "1112,1113,1112".

    In the cart there is an option for deleting an item/node. But how do i do this? The note needs to be deleted based on the position() and not the actual @id as there could more than one with the same @id.

    The code looks like this;

    <xsl:template match="/">
    <xsl:variable name="addNodeId" select="umbraco.library:RequestQueryString('add')"/>
    <xsl:variable name="removePosId" select="umbraco.library:RequestQueryString('remove')"/>
    <xsl:variable name="currentCookie" select="umbraco.library:RequestCookies('basket')"/>
    <xsl:variable name="basketC"><xsl:call-template name="basketcontent"/></xsl:variable>

    <xsl:if test="$addNodeId != ''">
    <xsl:value-of select="umbraco.library:setCookie('basket', concat($currentCookie,$addNodeId,','))"/>
    </xsl:if>

    <xsl:if test="$removePosId != ''">
    // What to do here?
    </xsl:if>

    <xsl:for-each select="msxml:node-set($basketC)/node">
    <ul class="prolist">
    <li class="proname"><xsl:value-of select="./@nodeName"/></li>
    <li class="pricehead"><xsl:value-of select="./data [@alias = 'itemNo']"/></li>
    <li class="total"><xsl:value-of select="./data [@alias = 'price']"/><span><a href="?remove={position()}"><img src="images/cross.gif" alt=""/></a></span></li>
    </ul>
    </xsl:for-each>

    <div class="toalprice">[How to get the total price?]</div>
    </xsl:template>

    <xsl:template name="basketcontent">
    <xsl:for-each select="umbraco.library:Split(umbraco.library:RequestCookies('basket'),',')/value">
    <xsl:if test=".!=''">
    <xsl:copy-of select="umbraco.library:GetXmlNodeById(.)"/>
    </xsl:if>
    </xsl:for-each>
    </xsl:template>

    Also; how can i sum up the total price of all the selected nodes/price and use it in the bottom sum outside the loop?

    :-)

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Dec 13, 2009 @ 12:05
    Dirk De Grave
    0

    Hm, maybe not an answer to your question, but why don't you use a user control to handle all this stuff. I see far too many things xslt isn't designed for in your script (I'd only focus on presenting data instead of manipulating stuff through xslt - although personal preference...).

     

    Hope this helps.

    Regards,

    /Dirk

  • Dinovo 40 posts 59 karma points
    Dec 13, 2009 @ 14:09
    Dinovo
    0

    I am aware that a usercontrol maybe would be better in some aspects - but this is alos sort of an experiment and a learning project, so i would like to do it in xslt :)

  • Morten Bock 1867 posts 2140 karma points MVP 2x admin c-trib
    Dec 13, 2009 @ 18:37
    Morten Bock
    0

    Variables in XSLT are immutable, so the thing to do would probably be something like this:

        <xsl:if test="$removePosId != ''">
          <xsl:variable name="newBasketString">
            <xsl:for-each select="msxml:node-set($basketC)/node">
              <xsl:choose>
                <xsl:when test="$removePosId != last()">
                  <xsl:if test="position() != $removePosId">
                    <xsl:value-of select="@id"/> <!--Or whatever value you use-->
                    <xsl:if test="position() != last()">
                      <xsl:text>,</xsl:text>
                    </xsl:if>
                  </xsl:if>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:if test="position() != $removePosId">
                    <xsl:value-of select="@id"/>
                    <!--Or whatever value you use-->
                    <xsl:if test="position() != last() - 1">
                      <xsl:text>,</xsl:text>
                    </xsl:if>
                  </xsl:if>
                </xsl:otherwise>
              </xsl:choose>
            </xsl:for-each>
          </xsl:variable>
          <xsl:value-of select="umbraco.library:setCookie('basket', $newBasketString)"/>
        </xsl:if>
    

  • Dinovo 40 posts 59 karma points
    Dec 13, 2009 @ 20:13
    Dinovo
    0

    Works like a charm - thanks :)

    The final thing is to sum the price of every line. I have tried with the following but cannot get it to work;

    <xsl:value-of select="sum(msxml:node-set($basketC)/node/data [@alias = 'price'] )" />

    The above line just returns NaN - am i totally off course in the assumption that something along the lines of the above should work?

  • Morten Bock 1867 posts 2140 karma points MVP 2x admin c-trib
    Dec 14, 2009 @ 13:59
    Morten Bock
    0

    Is your price formatted with comma or periods? The parser might not be able to figure out that the string is a number.

  • dandrayne 1138 posts 2262 karma points
    Dec 14, 2009 @ 14:25
    dandrayne
    0

    You could also try number()

    <xsl:value-of select="number('1010')"/>

    Dan

  • Dinovo 40 posts 59 karma points
    Dec 15, 2009 @ 00:10
    Dinovo
    0

    The 'price' fields contains comma values. How would i incorporate the Number() function in my one-line statement?

  • Dinovo 40 posts 59 karma points
    Dec 15, 2009 @ 23:37
    Dinovo
    0

    I have now figured out that if i instead of passing values with comma (,) pass values with periods (.) the sum function i stated above works as intended.

Please Sign in or register to post replies

Write your reply to:

Draft