Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Archive /

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

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

Ecommerce Cart Quantity


Go to End


5 Posts   2206 Views

Avatar
KevvyKev

Community Member, 3 Posts

28 June 2008 at 5:24pm

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 %>

Avatar
Willr

Forum Moderator, 5523 Posts

28 June 2008 at 7:32pm

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 :(

Avatar
KevvyKev

Community Member, 3 Posts

29 June 2008 at 2:09am

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!

Avatar
Willr

Forum Moderator, 5523 Posts

29 June 2008 at 10:53am

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.

Avatar
KevvyKev

Community Member, 3 Posts

2 July 2008 at 5:29pm

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>