Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Ecommerce Cart Quantity

Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, biapar, Willr, Ingo, simon_w

Page: 1
Go to End
Author Topic: 1689 Views
  • KevvyKev
    Avatar
    Community Member
    3 Posts

    Ecommerce Cart Quantity Link to this post

    I've been able to get the total quantity of my cart, but I want to have different text if there is 1 item or 2 or more items. Is there a property I am missing that would work in the <% if %> statement? I've tried Cart, Items, Items.Count, Count, and various others. My code for the cart is below.

    <% control Cart %>
       <div id="ShoppingCart">
          <% if Items %>
                <% if Count = 1 %>
                   <p>There is $Items.Count item in your cart. <a class="checkoutButton" href="checkout">Proceed to checkout</a></p>
                <% else %>
                   <p>There are $Items.Count items in your cart. <a class="checkoutButton" href="checkout">Proceed to checkout</a></p>
                <% end_if %>
          <% else %>
             <p><% _t("NOITEMS","There are no items in your cart") %>.</p>
          <% end_if %>
       </div>
    <% end_control %>

  • Willr
    Avatar
    Forum Moderator
    5464 Posts

    Re: Ecommerce Cart Quantity Link to this post

    if the if Statement you use Count = 1. where as if that variable is at $Items.Count then you would need to use

    ..
    <% if Items.Count = 1 %>
    // do stuff
    <% end_if %>

    You said you tried that so maybe that doesnt work

  • KevvyKev
    Avatar
    Community Member
    3 Posts

    Re: Ecommerce Cart Quantity Link to this post

    I did try that version. When I would go back to the site and refresh, the screen would just be empty. The site would render if the property was just one word, but as soon as I used Property.SubProperty, the site wouldn't render. It seems like Items.Count should work in that scenario, but it doesn't. Also, I can put a completely made up property into the if-statement and the site will render. For example, if I used

    ..
    <% if Sherwood = 1 %>
    // do stuff
    <% end_if %>

    The site will render, but it will use the else-statement ("...are items...") even though there is one item in the cart. If I use a made up property, should the site not render?

    Thanks!

  • Willr
    Avatar
    Forum Moderator
    5464 Posts

    Re: Ecommerce Cart Quantity Link to this post

    If I use a made up property, should the site not render?

    No because you are checking if it exists. It will use the else.. bit as if you say "if Count = 1" and Count doesnt exist then count doesnt equal 1 therefore use the else.

    This is a 'Feature' of the SSViewer so for instance you always have $Form in your Page.ss template but you might not always have a form on your page so you dont want an error - you could wrap a <% if Form %> round it and every other variable but that gets messy.

  • KevvyKev
    Avatar
    Community Member
    3 Posts

    Re: Ecommerce Cart Quantity Link to this post

    After some moving and tweaking I found the following code to accomplish what I was after. Two things that I believe to be important were: 1) moving the Cart control inside the ShoppingCart div, and 2) adding the Item control. The variables were then set to $Quantity instead of $Items.Count.

    The code acts as follows. If there are no items in the cart, display the text "There are no items in your cart." If there is one item in the cart, display the text, "The is 1 item in your cart. Proceed to checkout."If there are more than two items in the cart, display the text, "There are X items in your cart. Proceed to checkout."

    One scenario I have not checked yet is adding multiple copies of more than one item (i.e. a quantity). I may end up using a different variable to show the total cart item quantity.

    <div id="ShoppingCart">
       <% control Cart %>
          <% if Items %>
             <% control Items %>
                <% if Quantity = 1 %>
                   <p>There is $Quantity item in your cart. <a href="checkout">Proceed to checkout</a></p>
                <% else %>
                   <p>There are $Quantity items in your cart. <a href="checkout">Proceed to checkout</a></p>
                <% end_if %>
             <% end_control %>
          <% else %>
             <p><% _t("NOITEMS","There are no items in your cart") %>.</p>
          <% end_if %>
       <% end_control %>
    </div>

    1689 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.