diff --git a/lib/WebGUI/Shop/Cart.pm b/lib/WebGUI/Shop/Cart.pm index e0463d92e..eb9a705a5 100644 --- a/lib/WebGUI/Shop/Cart.pm +++ b/lib/WebGUI/Shop/Cart.pm @@ -82,6 +82,8 @@ sub delete { my ($self) = @_; $self->empty; $self->session->db->write("delete from cart where cartId=?",[$self->getId]); + undef $self; + return undef; } #------------------------------------------------------------------- @@ -144,9 +146,9 @@ Returns an array reference of WebGUI::Asset::Sku objects that are in the cart. sub getItems { my ($self) = @_; my @itemsObjects = (); - my $items = $self->session->db->read("select assetId from cartItems where cartId=?",[$self->getId]); - while (my ($assetId) = $items->array) { - push(@itemsObjects, WebGUI::Shop::CartItems->new($self->session, $assetId)); + my $items = $self->session->db->read("select itemId from cartItems where cartId=?",[$self->getId]); + while (my ($itemId) = $items->array) { + push(@itemsObjects, WebGUI::Shop::CartItem->new($self, $itemId)); } return \@itemsObjects; } diff --git a/lib/WebGUI/Shop/CartItem.pm b/lib/WebGUI/Shop/CartItem.pm index 69b128417..38c0de7dc 100644 --- a/lib/WebGUI/Shop/CartItem.pm +++ b/lib/WebGUI/Shop/CartItem.pm @@ -1,7 +1,6 @@ package WebGUI::Shop::CartItem; use strict; - use Class::InsideOut qw{ :std }; use Carp qw(croak); use JSON; @@ -81,7 +80,13 @@ sub get { my ($self, $name) = @_; if (defined $name) { if ($name eq "options") { - return JSON::from_json($properties{id $self}{$name}); + my $options = $properties{id $self}{$name}; + if ($options eq "") { + return {}; + } + else { + return JSON::from_json($properties{id $self}{$name}); + } } return $properties{id $self}{$name}; } @@ -89,6 +94,20 @@ sub get { return \%copyOfHashRef; } +#------------------------------------------------------------------- + +=head2 getId () + +Returns the unique id of this item. + +=cut + +sub getId { + my $self = shift; + return $self->get("itemId"); +} + + #------------------------------------------------------------------- =head2 getSku ( ) @@ -99,7 +118,7 @@ Returns an instanciated WebGUI::Asset::Sku object for this cart item. sub getSku { my ($self) = @_; - my $asset = WebGUI::Asset->newByDynamicClass($self->session, $self->get("assetId")); + my $asset = WebGUI::Asset->newByDynamicClass($self->cart->session, $self->get("assetId")); $asset->applyOptions($self->get("options")); return $asset; } @@ -109,11 +128,11 @@ sub getSku { =head2 incrementQuantity ( [ quantity ] ) -Increments quantity of item by one. +Increments quantity of item by one. Returns the quantity of this item in the cart. =head3 quantity -If specified may increment quantity by more than one. +If specified may increment quantity by more than one. Specify a negative number to decrement quantity. If the quantity ever reaches 0 or lower, the item will be removed from the cart. =cut @@ -124,8 +143,12 @@ sub incrementQuantity { if ($self->get("quantity") + $quantity > $self->getSku->getMaxAllowedInCart) { croak "Cannot have that many in cart."; } + if ($self->get("quantity") + $quantity <= 0) { + return $self->remove; + } $properties{$id}{quantity} += $quantity; - $self->session->db->setRow("cartItems","itemId", $properties{$id}); + $self->cart->session->db->setRow("cartItems","itemId", $properties{$id}); + return $properties{$id}{quantity}; } @@ -149,7 +172,7 @@ sub new { my ($class, $cart, $itemId) = @_; croak "Need a cart" unless (defined $cart && $cart->isa("WebGUI::Shop::Cart")); croak "Need an itemId" unless defined $itemId; - my $item = $cart->session->db->quickHashRef('select * from cart where itemId=?', [$itemId]); + my $item = $cart->session->db->quickHashRef('select * from cartItems where itemId=?', [$itemId]); croak "No item with id of $itemId" if ($item->{itemId} eq ""); croak "Item $itemId is not in this cart." if ($item->{cartId} ne $cart->getId); my $self = register $class; @@ -159,6 +182,22 @@ sub new { return $self; } +#------------------------------------------------------------------- + +=head2 remove ( ) + +Removes this item from the cart. + +=cut + +sub remove { + my $self = shift; + $self->cart->session->db->deleteRow("cartItems","itemId",$self->getId); + undef $self; + return undef; +} + + #------------------------------------------------------------------- =head2 update ( properties ) @@ -199,7 +238,7 @@ sub update { $properties{$id}{options} = JSON::to_json($newProperties->{options}); } $properties{$id}{shippingAddressId} = $newProperties->{shippingAddressId} || $properties{$id}{shippingAddressId}; - $self->session->db->setRow("cart","cartId",$properties{$id}); + $self->cart->session->db->setRow("cartItems","cartId",$properties{$id}); } diff --git a/t/Shop/Cart.t b/t/Shop/Cart.t index 1495ad5ee..6fa729183 100644 --- a/t/Shop/Cart.t +++ b/t/Shop/Cart.t @@ -30,7 +30,7 @@ my $session = WebGUI::Test->session; #---------------------------------------------------------------------------- # Tests -plan tests => 14; # Increment this number for each test you create +plan tests => 16; # Increment this number for each test you create #---------------------------------------------------------------------------- # put your tests here @@ -48,26 +48,30 @@ my $product = $root->addChild({ my $item = $cart->addItem($product); isa_ok($item, "WebGUI::Shop::CartItem"); -isa_ok($item->cart, "WebGUI::Cart", "Does the item have a cart?"); +isa_ok($item->cart, "WebGUI::Shop::Cart", "Does the item have a cart?"); is(ref($item->get), "HASH", "Do we have a hash of properties?"); -is($item->get("quantity"), 2, "Should have 1 of these in the cart."); -$item->incrementQuantity(2); +is($item->get("quantity"), 1, "Should have 1 of these in the cart."); +is($item->incrementQuantity(2), 3, "incrementQuantity() should tell us how many items of this type are in the cart"); is($item->get("quantity"), 3, "Should have 3 of these in the cart."); -is(scalar(@{$cart->getItems}), 1, "Should have 3 of these in the cart."); +is(scalar(@{$cart->getItems}), 1, "Should have 1 item type in cart regardless of quanity."); + +$item->update({shippingAddressId => "XXXX"}); +is($item->get("shippingAddressId"), "XXXX", "Can set values to the cart item properties."); like($cart->getId, qr/[A-Za-z0-9\_\-]{22}/, "Id looks like a guid."); is(ref($cart->get), "HASH", "Cart properties are a hash reference."); is($cart->get("sessionId"), $session->getId, "Can retrieve a value from the cart properties."); -$cart->set({shippingAddressId => "XXXX"}); + +$cart->update({shippingAddressId => "XXXX"}); is($cart->get("shippingAddressId"), "XXXX", "Can set values to the cart properties."); $cart->empty; is($session->db->quickScalar("select count(*) from cartItems where cartId=?",[$cart->getId]), 0, "Items are removed from cart."); $cart->delete; -is($cart, undef, "Can destroy cart."); +is($cart->delete, undef, "Can destroy cart."); $product->purge;