diff --git a/lib/WebGUI/Content/Shop.pm b/lib/WebGUI/Content/Shop.pm index 7b77426f2..a254f0bc8 100644 --- a/lib/WebGUI/Content/Shop.pm +++ b/lib/WebGUI/Content/Shop.pm @@ -109,7 +109,7 @@ sub www_cart { my $session = shift; my $output = undef; my $method = "www_". ( $session->form->get("method") || "view"); - my $cart = WebGUI::Shop::Cart->create($session); + my $cart = WebGUI::Shop::Cart->getCartBySession($session); if ($cart->can($method)) { $output = $cart->$method(); } diff --git a/lib/WebGUI/Shop/Cart.pm b/lib/WebGUI/Shop/Cart.pm index 5609ff067..ffadd3f67 100644 --- a/lib/WebGUI/Shop/Cart.pm +++ b/lib/WebGUI/Shop/Cart.pm @@ -92,9 +92,7 @@ sub create { unless (defined $session && $session->isa("WebGUI::Session")) { WebGUI::Error::InvalidObject->throw(expected=>"WebGUI::Session", got=>(ref $session), error=>"Need a session."); } - my $cartId = $session->db->quickScalar("select cartId from cart where sessionId=?",[$session->getId]); - return $class->new($session, $cartId) if (defined $cartId); - $cartId = $session->id->generate; + my $cartId = $session->id->generate; $session->db->write('insert into cart (cartId, sessionId) values (?,?)', [$cartId, $session->getId]); return $class->new($session, $cartId); } @@ -183,6 +181,24 @@ sub getAddressBook { #------------------------------------------------------------------- +=head2 getCartBySession () + +Class method that figures out if the user has a cart in their session. If they do it returns it. If they don't it creates it and returns it. + +=cut + +sub getCartBySession { + my ($class, $session) = @_; + unless (defined $session && $session->isa("WebGUI::Session")) { + WebGUI::Error::InvalidObject->throw(expected=>"WebGUI::Session", got=>(ref $session), error=>"Need a session."); + } + my $cartId = $session->db->quickScalar("select cartId from cart where sessionId=?",[$session->getId]); + return $class->new($session, $cartId) if (defined $cartId); + return $class->create($session); +} + +#------------------------------------------------------------------- + =head2 getId () Returns the unique id for this cart. diff --git a/t/Shop/Cart.t b/t/Shop/Cart.t index 6fa729183..46289498a 100644 --- a/t/Shop/Cart.t +++ b/t/Shop/Cart.t @@ -35,7 +35,7 @@ plan tests => 16; # Increment this number for each test you create #---------------------------------------------------------------------------- # put your tests here -my $cart = WebGUI::Shop::Cart->create($session); +my $cart = WebGUI::Shop::Cart->getCartBySession($session); isa_ok($cart, "WebGUI::Shop::Cart"); isa_ok($cart->session, "WebGUI::Session"); diff --git a/t/Shop/Tax.t b/t/Shop/Tax.t index 0be5057ea..ddffcfec5 100644 --- a/t/Shop/Tax.t +++ b/t/Shop/Tax.t @@ -540,7 +540,7 @@ is($e->error, 'Must pass in a WebGUI::Shop::Cart object', 'calculate: error hand ##Build a cart, add some Donation SKUs to it. Set one to be taxable. -my $cart = WebGUI::Shop::Cart->create($session); +my $cart = WebGUI::Shop::Cart->getCartBySession($session); is($taxer->calculate($cart), 0, 'calculate returns 0 if there is no shippingAddressId in the cart');