Error promoted to a full cart method. Beginning to process the address forms.

This commit is contained in:
Colin Kuskie 2010-04-27 17:18:35 -07:00
parent 9365a55477
commit 822cac8dec
2 changed files with 105 additions and 47 deletions

View file

@ -9,6 +9,7 @@ use WebGUI::Exception::Shop;
use WebGUI::Form; use WebGUI::Form;
use WebGUI::International; use WebGUI::International;
use WebGUI::Shop::Address; use WebGUI::Shop::Address;
use Scalar::Util qw/blessed/;
=head1 NAME =head1 NAME
@ -282,6 +283,36 @@ sub getId {
#------------------------------------------------------------------- #-------------------------------------------------------------------
=head2 missingFields ( $address )
Returns a list of missing, required fields in this address.
=head3 $address
An address. If it's an WebGUI::Shop::Address object, it will use the data
from it. Otherwise, it will assume that $address is just a hashref.
=cut
sub missingFields {
my $self = shift;
my $address = shift;
my $addressData;
if (blessed $address && $address->isa('WebGUI::Shop::Address')) {
$addressData = $address->get();
}
else {
$addressData = $address;
}
my @missingFields = ();
FIELD: foreach my $field (qw/label firstName lastName address1 city code country phoneNumber/) {
push @missingFields, $field if $addressData->{$field} eq '';
}
return @missingFields;
}
#-------------------------------------------------------------------
=head2 new ( session, addressBookId ) =head2 new ( session, addressBookId )
Constructor. Instanciates an addressBook based upon a addressBookId. Constructor. Instanciates an addressBook based upon a addressBookId.
@ -400,17 +431,11 @@ sub processAddressForm {
email => $form->get($prefix . "email", "email"), email => $form->get($prefix . "email", "email"),
organization => $form->get($prefix . "organization"), organization => $form->get($prefix . "organization"),
); );
my $i18n = WebGUI::International->new($self->session, "Shop"); #my $label = $field eq 'address1' ? 'address'
FIELD: foreach my $field (qw/label firstName lastName address1 city code country phoneNumber/) { # : $field eq 'phoneNumber' ? 'phone number'
my $label = $field eq 'address1' ? 'address' # : $field
: $field eq 'phoneNumber' ? 'phone number' # ;
: $field
;
if ($addressData{$field} eq "") {
$addressData{error} = sprintf($i18n->get('is a required field'), $i18n->get($label));
last FIELD;
}
}
return %addressData; return %addressData;
} }
@ -588,8 +613,9 @@ sub www_editAddressSave {
my $self = shift; my $self = shift;
my $form = $self->session->form; my $form = $self->session->form;
my %addressData = $self->processAddressForm(); my %addressData = $self->processAddressForm();
if (exists $addressData{error}) { my @missingFields = $self->missingFields(\%addressData);
return $self->www_editAddress($addressData{error}); if (@missingFields) {
return $self->www_editAddress(pop @missingFields);
} }
if ($form->get('addressId') eq '') { if ($form->get('addressId') eq '') {
$self->addAddress(\%addressData); $self->addAddress(\%addressData);

View file

@ -40,7 +40,7 @@ These subroutines are available from this package:
readonly session => my %session; readonly session => my %session;
private properties => my %properties; private properties => my %properties;
private error => my %error; public error => my %error;
private addressBookCache => my %addressBookCache; private addressBookCache => my %addressBookCache;
#------------------------------------------------------------------- #-------------------------------------------------------------------
@ -521,8 +521,12 @@ Returns whether all the required properties of the the cart are set.
sub readyForCheckout { sub readyForCheckout {
my $self = shift; my $self = shift;
# Check if the billing address is set and correct
my $address = eval{$self->getBillingAddress};
return 0 if WebGUI::Error->caught;
# Check if the shipping address is set and correct # Check if the shipping address is set and correct
my $address = eval{$self->getShippingAddress}; my $shipAddress = eval{$self->getShippingAddress};
return 0 if WebGUI::Error->caught; return 0 if WebGUI::Error->caught;
# Check if the cart has items # Check if the cart has items
@ -534,7 +538,7 @@ sub readyForCheckout {
# Check minimum cart checkout requirement # Check minimum cart checkout requirement
my $total = eval { $self->calculateTotal }; my $total = eval { $self->calculateTotal };
if (my $e = WebGUI::Error->caught) { if (my $e = WebGUI::Error->caught) {
$error{id $self} = $e->error; $self->error($e->error);
return 0; return 0;
} }
my $requiredAmount = $self->session->setting->get( 'shopCartCheckoutMinimum' ); my $requiredAmount = $self->session->setting->get( 'shopCartCheckoutMinimum' );
@ -574,6 +578,26 @@ sub requiresRecurringPayment {
#------------------------------------------------------------------- #-------------------------------------------------------------------
=head2 requiresShipping ( )
Returns whether any item in this cart requires shipping.
=cut
sub requiresShipping {
my $self = shift;
# Look for recurring items in the cart
foreach my $item (@{ $self->getItems }) {
return 1 if $item->getSku->isShippingRequired;
}
# No recurring items in cart so return false
return 0;
}
#-------------------------------------------------------------------
=head2 update ( properties ) =head2 update ( properties )
Sets properties in the cart. Sets properties in the cart.
@ -620,7 +644,7 @@ sub update {
=head2 updateFromForm ( ) =head2 updateFromForm ( )
Updates the cart totals from form data. Updates the cart totals, the address fields and the shipping and billing options from form data.
=cut =cut
@ -648,13 +672,17 @@ sub updateFromForm {
my $cartProperties = {}; my $cartProperties = {};
my %billingData = $book->processAddressForm('billing_'); my %billingData = $book->processAddressForm('billing_');
my @missingBillingFields = $book->missingFields(\%billingData);
if (@missingBillingFields) {
$self->error('missing billing '.$missingBillingFields[0]);
}
my $billingAddressId = $form->process('billingAddressId'); my $billingAddressId = $form->process('billingAddressId');
if ($billingAddressId eq 'new_address' && ! exists $billingData{'error'}) { if ($billingAddressId eq 'new_address' && ! @missingBillingFields) {
##Add a new address ##Add a new address
my $newAddress = $book->addAddress(\%billingData); my $newAddress = $book->addAddress(\%billingData);
$cartProperties->{billingAddressId} = $newAddress->get('addressId'); $cartProperties->{billingAddressId} = $newAddress->get('addressId');
} }
elsif ($billingAddressId eq 'update_address' && $self->get('billingAddressId')) { elsif ($billingAddressId eq 'update_address' && $self->get('billingAddressId') && ! @missingBillingFields) {
##User changed the address selector ##User changed the address selector
my $address = $self->getBillingAddress(); my $address = $self->getBillingAddress();
$address->update(\%billingData); $address->update(\%billingData);
@ -663,29 +691,35 @@ sub updateFromForm {
$cartProperties->{billingAddressId} = $billingAddressId; $cartProperties->{billingAddressId} = $billingAddressId;
} }
else { else {
$self->session->log->warn('billing address: something else: '. $billingData{error}); $self->session->log->warn('billing address: something else: ');
} }
my %shippingData = $book->processAddressForm('shipping_'); if ($self->requiresShipping) {
my $shippingAddressId = $form->process('shippingAddressId'); my %shippingData = $book->processAddressForm('shipping_');
if ($form->process('sameShippingAsBilling', 'yesNo')) { my @missingShippingFields = $book->missingFields(\%shippingData);
$cartProperties->{shippingAddressId} = $self->get('billingAddressId'); if (@missingShippingFields) {
} $self->error('missing shipping '.$missingShippingFields[0]);
elsif ($shippingAddressId eq 'new_address' && ! exists $shippingData{'error'}) { }
##Add a new address my $shippingAddressId = $form->process('shippingAddressId');
my $newAddress = $book->addAddress(\%shippingData); if ($form->process('sameShippingAsBilling', 'yesNo')) {
$cartProperties->{shippingAddressId} = $newAddress->get('addressId'); $cartProperties->{shippingAddressId} = $self->get('billingAddressId');
} }
elsif ($shippingAddressId eq 'update_address' && $self->get('shippingAddressId')) { elsif ($shippingAddressId eq 'new_address' && ! @missingShippingFields) {
##User changed the address selector ##Add a new address
my $address = $self->getBillingAddress(); my $newAddress = $book->addAddress(\%shippingData);
$address->update(\%shippingData); $cartProperties->{shippingAddressId} = $newAddress->get('addressId');
} }
elsif ($shippingAddressId ne 'new_address' && $shippingAddressId) { elsif ($shippingAddressId eq 'update_address' && $self->get('shippingAddressId') && ! @missingShippingFields) {
$cartProperties->{shippingAddressId} = $shippingAddressId; ##User changed the address selector
} my $address = $self->getBillingAddress();
else { $address->update(\%shippingData);
$self->session->log->warn('shipping address: something else: '. $shippingData{error}); }
elsif ($shippingAddressId ne 'new_address' && $shippingAddressId) {
$cartProperties->{shippingAddressId} = $shippingAddressId;
}
else {
$self->session->log->warn('shipping address: something else: ');
}
} }
$cartProperties->{ shipperId } = $form->process( 'shipperId' ) if $form->process( 'shipperId' ); $cartProperties->{ shipperId } = $form->process( 'shipperId' ) if $form->process( 'shipperId' );
@ -860,7 +894,6 @@ sub www_view {
} }
# generate template variables for the items in the cart # generate template variables for the items in the cart
my $shippableItemsInCart = 0;
foreach my $item (@cartItems) { foreach my $item (@cartItems) {
my $sku = $item->getSku; my $sku = $item->getSku;
$sku->applyOptions($item->get("options")); $sku->applyOptions($item->get("options"));
@ -876,7 +909,6 @@ sub www_view {
extras=>q|onclick="this.form.method.value='removeItem';this.form.itemId.value='|.$item->getId.q|';this.form.submit;"|}), extras=>q|onclick="this.form.method.value='removeItem';this.form.itemId.value='|.$item->getId.q|';this.form.submit;"|}),
shipToButton => WebGUI::Form::submit($session, {value=>$i18n->get("Special shipping"), }), shipToButton => WebGUI::Form::submit($session, {value=>$i18n->get("Special shipping"), }),
); );
$shippableItemsInCart ||= $properties{isShippable};
my $itemAddress = eval {$item->getShippingAddress}; my $itemAddress = eval {$item->getShippingAddress};
if ((!WebGUI::Error->caught) && $itemAddress && $address && $itemAddress->getId ne $address->getId) { if ((!WebGUI::Error->caught) && $itemAddress && $address && $itemAddress->getId ne $address->getId) {
$properties{shippingAddress} = $itemAddress->getHtmlFormatted; $properties{shippingAddress} = $itemAddress->getHtmlFormatted;
@ -909,7 +941,7 @@ sub www_view {
? sprintf( '%.2f', $session->setting->get( 'shopCartCheckoutMinimum' ) ) ? sprintf( '%.2f', $session->setting->get( 'shopCartCheckoutMinimum' ) )
: 0 : 0
, ,
shippableItemsInCart => $shippableItemsInCart, shippableItemsInCart => $self->requiresShipping,
); );
$session->log->warn('below var block'); $session->log->warn('below var block');
@ -923,7 +955,7 @@ sub www_view {
if ($numberOfOptions < 1) { if ($numberOfOptions < 1) {
$var{shippingOptions} = ''; $var{shippingOptions} = '';
$var{shippingPrice} = 0; $var{shippingPrice} = 0;
$error{id $self} = $i18n->get("No shipping plugins configured"); $self->error($i18n->get("No shipping plugins configured"));
} }
elsif ($numberOfOptions == 1) { elsif ($numberOfOptions == 1) {
my ($option) = keys %{ $options }; my ($option) = keys %{ $options };
@ -951,7 +983,7 @@ sub www_view {
} }
else { else {
$var{shippingPrice} = 0; $var{shippingPrice} = 0;
$error{id $self} = ($i18n->get('Choose a shipping method and update the cart to checkout')); $self->error($i18n->get('Choose a shipping method and update the cart to checkout'));
} }
$var{shippingPrice} = $shipperId && $options->{$shipperId}->{hasPrice} ? $self->formatCurrency($var{shippingPrice}) : ''; $var{shippingPrice} = $shipperId && $options->{$shipperId}->{hasPrice} ? $self->formatCurrency($var{shippingPrice}) : '';
$var{tax} = $self->calculateTaxes; $var{tax} = $self->calculateTaxes;
@ -1052,8 +1084,8 @@ sub www_view {
$var{ inShopCreditAvailable } = $credit->getSum; $var{ inShopCreditAvailable } = $credit->getSum;
$var{ inShopCreditDeduction } = $credit->calculateDeduction($var{totalPrice}); $var{ inShopCreditDeduction } = $credit->calculateDeduction($var{totalPrice});
$var{ totalPrice } = $self->formatCurrency($var{totalPrice} + $var{inShopCreditDeduction}); $var{ totalPrice } = $self->formatCurrency($var{totalPrice} + $var{inShopCreditDeduction});
$var{ readyForCheckout } = $self->readyForCheckout; #$var{ readyForCheckout } = $self->readyForCheckout;
$var{ error } = $error{id $self}; $var{ error } = $self->error;
# render the cart # render the cart
my $template = WebGUI::Asset::Template->new($session, $session->setting->get("shopCartTemplateId")); my $template = WebGUI::Asset::Template->new($session, $session->setting->get("shopCartTemplateId"));