From e71946fb946ff822039faef6930033dc26bd36b6 Mon Sep 17 00:00:00 2001 From: JT Smith Date: Wed, 21 May 2008 20:14:46 +0000 Subject: [PATCH] check for mixed recurring and non-recurring items in the cart --- lib/WebGUI/Asset/Sku.pm | 4 ++-- lib/WebGUI/Shop/Cart.pm | 32 +++++++++++++++++++++++++++++++- lib/WebGUI/i18n/English/Shop.pm | 6 ++++++ 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/lib/WebGUI/Asset/Sku.pm b/lib/WebGUI/Asset/Sku.pm index 4932697a5..c49b2ed08 100644 --- a/lib/WebGUI/Asset/Sku.pm +++ b/lib/WebGUI/Asset/Sku.pm @@ -218,13 +218,13 @@ sub getOptions { =head2 getMaxAllowedInCart ( ) -Returns getQuantityAvailable(). Should be overriden by subclasses that have a specific value. Subclasses that are unique should return 1. Subclasses that have an inventory count should return the amount in inventory. +Returns getQuantityAvailable() or 1 if isRecurring() return 1. Should be overriden by subclasses that have a specific value. Subclasses that are unique should return 1. Subclasses that have an inventory count should return the amount in inventory. =cut sub getMaxAllowedInCart { my $self = shift; - return $self->getQuantityAvailable; + return $self->isRecurring || $self->getQuantityAvailable; } #------------------------------------------------------------------- diff --git a/lib/WebGUI/Shop/Cart.pm b/lib/WebGUI/Shop/Cart.pm index 3e47ba4e3..aff32a7db 100644 --- a/lib/WebGUI/Shop/Cart.pm +++ b/lib/WebGUI/Shop/Cart.pm @@ -398,6 +398,30 @@ sub getShippingAddress { return $self->getAddressBook->getAddress($self->get("shippingAddressId")); } +#------------------------------------------------------------------- + +=head2 hasMixedItems () + +Returns 1 if there are too many recurring items, or there are mixed recurring and non-recurring items in the cart. + +=cut + +sub hasMixedItems { + my $self = shift; + my $recurring = 0; + my $nonrecurring = 0; + foreach my $item (@{$self->getItems}) { + if ($item->getSku->isRecurring) { + $recurring += $item->get('quantity'); + } + else { + $nonrecurring += $item->get('quantity'); + } + return 1 if ($recurring > 0 && $nonrecurring > 0); + return 1 if ($recurring > 1); + } + return 0; +} #------------------------------------------------------------------- @@ -471,6 +495,9 @@ sub readyForCheckout { # Check if the cart has items return 0 unless scalar @{ $self->getItems }; + + # fail if there are multiple recurring items or if + return 0 if ($self->hasMixedItems); # All checks passed so return true return 1; @@ -551,7 +578,10 @@ sub updateFromForm { } } } - + if ($self->hasMixedItems) { + my $i18n = WebGUI::International->new($self->session, "Shop"); + $error{id $self} = $i18n->get('mixed items warning'); + } my $cartProperties = {}; $cartProperties->{ shipperId } = $form->process( 'shipperId' ) if $form->process( 'shipperId' ); $self->update( $cartProperties ); diff --git a/lib/WebGUI/i18n/English/Shop.pm b/lib/WebGUI/i18n/English/Shop.pm index 22ef2f66c..1bd2a07ee 100644 --- a/lib/WebGUI/i18n/English/Shop.pm +++ b/lib/WebGUI/i18n/English/Shop.pm @@ -3,6 +3,12 @@ package WebGUI::i18n::English::Shop; use strict; our $I18N = { + 'mixed items warning' => { + message => q|You are not able to check out with both recurring and non-recurring items in your cart. You may have either one recurring item, or as many non-recurring items as you want in your cart at checkout time. If you need to purchase both, then please purchase them under separate transactions.|, + lastUpdated => 0, + context => q|a warning message displayed in the cart|, + }, + 'print' => { message => q|Print|, lastUpdated => 0,