Require that the user be logged in before entering in address info. Refactor out address form into a method that can called multiple times.

This commit is contained in:
Colin Kuskie 2010-04-22 20:02:19 -07:00
parent 747fa8c922
commit fbefeaf621
5 changed files with 131 additions and 85 deletions

View file

@ -143,11 +143,6 @@ our $HELP = {
description => "formFooter help",
required => 1,
},
{
name => "checkoutButton",
description => "checkoutButton help",
required => 1,
},
{
name => "updateButton",
description => "updateButton help",
@ -157,15 +152,6 @@ our $HELP = {
name => "continueShoppingButton",
description => "continueShoppingButton help",
},
{
name => "chooseShippingButton",
description => "chooseShippingButton help",
required => 1,
},
{
name => "shipToButton",
description => "shipToButton help",
},
{
name => "subtotalPrice",
description => "subtotalPrice help",

View file

@ -185,6 +185,58 @@ sub getAddresses {
#-------------------------------------------------------------------
=head2 getAddressFormVars ( $properties, $prefix )
Return a hashref of template variables for building a form to edit an address.
=head3 $properties
A hash ref of properties to assign to as default to the form variables.
=head3 $prefix
An optional prefix to add to each variable name, and form name.
=cut
sub getAddressFormVars {
my ($self, $prefix, $properties ) = @_;
my $session = $self->session;
$properties ||= {};
$prefix ||= '';
my $var = {};
for ( qw{ address1 address2 address3 label firstName lastName city state organization } ) {
$var->{ $prefix . $_ . 'Field' } = WebGUI::Form::text( $session, {
name => $prefix . $_,
maxlength => 35,
defaultValue => $properties->{ $_ }
} );
}
$var->{ $prefix . 'countryField' } =
WebGUI::Form::country( $session,{
name => $prefix . 'country',
defaultValue => $properties->{ country }
} );
$var->{ $prefix . 'codeField' } =
WebGUI::Form::zipcode( $session, {
name => $prefix . 'code',
defaultValue => $properties->{ code }
} );
$var->{ $prefix . 'phoneNumberField' } =
WebGUI::Form::phone( $session, {
name => $prefix . 'phoneNumber',
defaultValue => $properties->{ phoneNumber }
} );
$var->{ $prefix . 'emailField' } =
WebGUI::Form::email( $session, {
name => $prefix . 'email',
defaultValue => $properties->{ email }
} );
return $var;
}
#-------------------------------------------------------------------
=head2 getDefaultAddress ()
Returns the default address for this address book if there is one. Otherwise throws a WebGUI::Error::ObjectNotFound exception.

View file

@ -798,9 +798,8 @@ sub www_view {
price => $self->formatCurrency($sku->getPrice),
removeButton => WebGUI::Form::submit($session, {value=>$i18n->get("remove button"),
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("ship to button"),
extras=>q|onclick="setCallbackForAddressChooser(this.form,'|.$item->getId.q|');"|}),
);
shipToButton => WebGUI::Form::submit($session, {value=>$i18n->get("Special shipping"), }),
);
my $itemAddress = eval {$item->getShippingAddress};
if ((!WebGUI::Error->caught) && $itemAddress && $address && $itemAddress->getId ne $address->getId) {
$properties{shippingAddress} = $itemAddress->getHtmlFormatted;
@ -823,14 +822,12 @@ sub www_view {
. WebGUI::Form::hidden($session, {name=>"callback", value=>""}),
formFooter => WebGUI::Form::formFooter($session),
updateButton => WebGUI::Form::submit($session, {value=>$i18n->get("update cart button"), extras=>q|id="updateCartButton"|}),
checkoutButton => WebGUI::Form::submit($session, {value=>$i18n->get("checkout button"),
extras=>q|onclick="this.form.method.value='checkout';this.form.submit;" id="checkoutButton"|}),
continueShoppingButton => WebGUI::Form::submit($session, {value=>$i18n->get("continue shopping button"),
extras=>q|onclick="this.form.method.value='continueShopping';this.form.submit;" id="continueShoppingButton"|}),
chooseShippingButton => WebGUI::Form::submit($session, {value=>$i18n->get("choose shipping button"),
extras=>q|onclick="setCallbackForAddressChooser(this.form);" id="chooseAddressButton"|}),
shipToButton => WebGUI::Form::submit($session, {value=>$i18n->get("ship to button"),
extras=>q|onclick="setCallbackForAddressChooser(this.form);"|}),
# chooseShippingButton => WebGUI::Form::submit($session, {value=>$i18n->get("choose shipping button"),
# extras=>q|onclick="setCallbackForAddressChooser(this.form);" id="chooseAddressButton"|}),
# shipToButton => WebGUI::Form::submit($session, {value=>$i18n->get("ship to button"),
# extras=>q|onclick="setCallbackForAddressChooser(this.form);"|}),
subtotalPrice => $self->formatCurrency($self->calculateSubtotal()),
minimumCartAmount => $session->setting->get( 'shopCartCheckoutMinimum' ) > 0
? sprintf( '%.2f', $session->setting->get( 'shopCartCheckoutMinimum' ) )
@ -839,51 +836,74 @@ sub www_view {
);
# if there is no shipping address we can't check out
if (WebGUI::Error->caught) {
$var{shippingPrice} = $var{tax} = $self->formatCurrency(0);
}
# if (WebGUI::Error->caught) {
# $var{shippingPrice} = $var{tax} = $self->formatCurrency(0);
# }
#
# # if there is a shipping address calculate tax and shipping options
# if ($address) {
# $var{hasShippingAddress} = 1;
# $var{shippingAddress} = $address->getHtmlFormatted;
# my $ship = WebGUI::Shop::Ship->new($self->session);
# my $options = $ship->getOptions($self);
# my $numberOfOptions = scalar keys %{ $options };
# if ($numberOfOptions < 1) {
# $var{shippingOptions} = '';
# $var{shippingPrice} = 0;
# $error{id $self} = $i18n->get("No shipping plugins configured");
# }
# elsif ($numberOfOptions == 1) {
# my ($option) = keys %{ $options };
# $self->update({ shipperId => $option });
# $var{shippingPrice} = $options->{$self->get("shipperId")}->{price};
# $var{shippingPrice} = $self->formatCurrency($var{shippingPrice});
# }
# else {
# tie my %formOptions, 'Tie::IxHash';
# $formOptions{''} = $i18n->get('Choose a shipping method');
# foreach my $option (keys %{$options}) {
# $formOptions{$option} = $options->{$option}{label}." (".$self->formatCurrency($options->{$option}{price}).")";
# }
# $var{shippingOptions} = WebGUI::Form::selectBox($session, {name=>"shipperId", options=>\%formOptions, value=>$self->get("shipperId")});
# if (!exists $options->{$self->get('shipperId')}) {
# $self->update({shipperId => ''});
# }
# if (my $shipperId = $self->get('shipperId')) {
# $var{shippingPrice} = $options->{$shipperId}->{price};
# }
# else {
# $var{shippingPrice} = 0;
# $error{id $self} = ($i18n->get('Choose a shipping method and update the cart to checkout'));
# }
# $var{shippingPrice} = $self->formatCurrency($var{shippingPrice});
# }
# }
#
# # Tax variables
# $var{tax} = $self->calculateTaxes;
# if there is a shipping address calculate tax and shipping options
if ($address) {
$var{hasShippingAddress} = 1;
$var{shippingAddress} = $address->getHtmlFormatted;
my $ship = WebGUI::Shop::Ship->new($self->session);
my $options = $ship->getOptions($self);
my $numberOfOptions = scalar keys %{ $options };
if ($numberOfOptions < 1) {
$var{shippingOptions} = '';
$var{shippingPrice} = 0;
$error{id $self} = $i18n->get("No shipping plugins configured");
}
elsif ($numberOfOptions == 1) {
my ($option) = keys %{ $options };
$self->update({ shipperId => $option });
$var{shippingPrice} = $options->{$self->get("shipperId")}->{price};
$var{shippingPrice} = $self->formatCurrency($var{shippingPrice});
}
else {
tie my %formOptions, 'Tie::IxHash';
$formOptions{''} = $i18n->get('Choose a shipping method');
foreach my $option (keys %{$options}) {
$formOptions{$option} = $options->{$option}{label}." (".$self->formatCurrency($options->{$option}{price}).")";
}
$var{shippingOptions} = WebGUI::Form::selectBox($session, {name=>"shipperId", options=>\%formOptions, value=>$self->get("shipperId")});
if (!exists $options->{$self->get('shipperId')}) {
$self->update({shipperId => ''});
}
if (my $shipperId = $self->get('shipperId')) {
$var{shippingPrice} = $options->{$shipperId}->{price};
}
else {
$var{shippingPrice} = 0;
$error{id $self} = ($i18n->get('Choose a shipping method and update the cart to checkout'));
}
$var{shippingPrice} = $self->formatCurrency($var{shippingPrice});
}
#Address form variables
$var{userIsVisitor} = $session->user->isVisitor;
if ($var{userIsVisitor}) {
##Make login form
#Form variable returnUrl
$var{loginFormHeader} = WebGUI::Form::formHeader($session, {action => $session->url->page})
. WebGUI::Form::hidden($session,{ name => 'op', value => 'auth'})
. WebGUI::Form::hidden($session,{ name => 'method', value => 'login'})
;
$var{loginFormUsername} = WebGUI::Form::text($session, { name => 'username', size => 12, });
$var{loginFormPassword} = WebGUI::Form::password($session, { name => 'identifier', size => 12, });
$var{loginFormButton} = WebGUI::Form::submit($session, { value => $i18n->get(52,'WebGUI'), });
$var{registerLink} = $session->url->page('op=auth;method=createAccount');
$session->scratch->set('redirectAfterLogin', $session->url->page('shop=cart'));
$var{loginFormFooter} = WebGUI::Form::formFooter($session)
}
else {
##Address form variables
my $addressBook = $self->getAddressBook;
%var = (%var, $addressBook->getAddressFormVars('shipping', {}));
%var = (%var, $addressBook->getAddressFormVars('billing', {}));
}
# Tax variables
$var{tax} = $self->calculateTaxes;
# POS variables
$var{isCashier} = WebGUI::Shop::Admin->new($session)->isCashier;
@ -895,7 +915,7 @@ sub www_view {
$var{posUserId} = $posUser->userId;
# calculate price adjusted for in-store credit
$var{totalPrice} = $var{subtotalPrice} + $var{shippingPrice} + $var{tax};
$var{totalPrice} = $var{subtotalPrice}; # + $var{shippingPrice} + $var{tax};
my $credit = WebGUI::Shop::Credit->new($session, $posUser->userId);
$var{ inShopCreditAvailable } = $credit->getSum;
$var{ inShopCreditDeduction } = $credit->calculateDeduction($var{totalPrice});

View file

@ -201,12 +201,6 @@ our $I18N = {
context => q|a help description|,
},
'checkoutButton help' => {
message => q|The button the user pushes to choose a payment method.|,
lastUpdated => 0,
context => q|a help description|,
},
'continueShoppingButton help' => {
message => q|Clicking this button will take the user back to the site.|,
lastUpdated => 0,
@ -219,18 +213,6 @@ our $I18N = {
context => q|a help description|,
},
'chooseShippingButton help' => {
message => q|Clicking this button will let the user pick a shipping address from the address book.|,
lastUpdated => 0,
context => q|a help description|,
},
'shipToButton help' => {
message => q|Does the same as the chooseShippingButton.|,
lastUpdated => 0,
context => q|a help description|,
},
'subtotalPrice help' => {
message => q|The price of all the items in the cart.|,
lastUpdated => 0,
@ -976,6 +958,12 @@ our $I18N = {
context => q|a button the user clicks on to set shipping information|
},
'Special shipping' => {
message => q|Special shipping|,
lastUpdated => 0,
context => q|a button the user clicks on to set shipping information on an item|
},
'shipping address' => {
message => q|Shipping Address|,
lastUpdated => 0,