Skip to content

Commit

Permalink
Add quantity and count methods to Nitesi::Cart.
Browse files Browse the repository at this point in the history
  • Loading branch information
racke committed Feb 1, 2012
1 parent 2bf48a5 commit ed21f2d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
32 changes: 32 additions & 0 deletions lib/Nitesi/Cart.pm
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,38 @@ sub clear {
return;
}

=head2 quantity
Returns the sum of the quantity of all items in the shopping cart,
which is commonly used as number of items.
print 'Items in your cart: ', $cart->quantity, "\n";
=cut

sub quantity {
my $self = shift;
my $qty = 0;

for my $item (@{$self->{items}}) {
$qty += $item->{quantity};
}

return $qty;
}

=head2 count
Returns the number of different items in the shopping cart.
=cut

sub count {
my $self = shift;

return scalar(@{$self->{items}});
}

=head2 apply_cost
Apply cost to cart.
Expand Down
15 changes: 14 additions & 1 deletion t/003_cart.t
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use strict;
use warnings;

use Test::More tests => 24;
use Test::More tests => 28;

use Nitesi::Cart;

Expand Down Expand Up @@ -130,6 +130,19 @@ $cart->seed([{sku => 'ABC', name => 'ABC', price => 2, quantity => 1},
$ret = $cart->items;
ok(@$ret == 2, "Items: $ret");

$ret = $cart->count;
ok($ret == 2, "Count: $ret");

$ret = $cart->quantity;
ok($ret == 3, "Quantity: $ret");

$ret = $cart->total;
ok($ret == 8, "Total: $ret");

$cart->clear;

$ret = $cart->count;
ok($ret == 0, "Count: $ret");

$ret = $cart->quantity;
ok($ret == 0, "Quantity: $ret");

0 comments on commit ed21f2d

Please sign in to comment.