Add a billing address to the cart. Add form processing for an address form to the AddressBook. Support for choosing shipping methods without an address.
This commit is contained in:
parent
8504a34c65
commit
9838d20458
6 changed files with 134 additions and 59 deletions
Binary file not shown.
|
|
@ -36,6 +36,7 @@ my $session = start(); # this line required
|
|||
addWikiSubKeywords($session);
|
||||
addSynopsistoEachWikiPage($session);
|
||||
dropVisitorAddressBooks($session);
|
||||
alterCartTable($session);
|
||||
alterAddressBookTable($session);
|
||||
addWizardHandler( $session );
|
||||
|
||||
|
|
@ -125,6 +126,15 @@ sub alterAddressBookTable {
|
|||
print "DONE!\n" unless $quiet;
|
||||
}
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
sub alterCartTable {
|
||||
my $session = shift;
|
||||
print "\tAdd billing address column to the Cart table... " unless $quiet;
|
||||
# and here's our code
|
||||
$session->db->write("ALTER TABLE cart ADD COLUMN billingAddressId CHAR(22)");
|
||||
print "DONE!\n" unless $quiet;
|
||||
}
|
||||
|
||||
# -------------- DO NOT EDIT BELOW THIS LINE --------------------------------
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
|
|
|
|||
|
|
@ -367,6 +367,51 @@ sub newByUserId {
|
|||
}
|
||||
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 processAddressForm ( $prefix )
|
||||
|
||||
Process the current set of form variables for any belonging to the address book. Returns
|
||||
a hash ref of address information.
|
||||
|
||||
=head3 $prefix
|
||||
|
||||
An optional prefix to be added to each form variable.
|
||||
|
||||
=cut
|
||||
|
||||
sub processAddressForm {
|
||||
my ($self, $prefix) = @_;
|
||||
$prefix ||= '';
|
||||
my $form = $self->session->form;
|
||||
my %addressData = (
|
||||
label => $form->get($prefix . "label"),
|
||||
firstName => $form->get($prefix . "firstName"),
|
||||
lastName => $form->get($prefix . "lastName"),
|
||||
address1 => $form->get($prefix . "address1"),
|
||||
address2 => $form->get($prefix . "address2"),
|
||||
address3 => $form->get($prefix . "address3"),
|
||||
city => $form->get($prefix . "city"),
|
||||
state => $form->get($prefix . "state"),
|
||||
code => $form->get($prefix . "code", "zipcode"),
|
||||
country => $form->get($prefix . "country", "country"),
|
||||
phoneNumber => $form->get($prefix . "phoneNumber", "phone"),
|
||||
email => $form->get($prefix . "email", "email"),
|
||||
organization => $form->get($prefix . "organization"),
|
||||
);
|
||||
my $i18n = WebGUI::International->new($self->session, "Shop");
|
||||
foreach my $field (qw/label firstName lastName address1 city code country phoneNumber/) {
|
||||
my $label = $field eq 'address1' ? 'address'
|
||||
: $field eq 'phoneNumber' ? 'phone number'
|
||||
: $field
|
||||
;
|
||||
if ($addressData{$field} eq "") {
|
||||
$addressData{error} = sprintf($i18n->get('is a required field'), $i18n->get($label));
|
||||
}
|
||||
}
|
||||
return %addressData;
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 update ( properties )
|
||||
|
|
@ -540,46 +585,10 @@ Saves the address. If there is a problem generates www_editAddress() with an err
|
|||
sub www_editAddressSave {
|
||||
my $self = shift;
|
||||
my $form = $self->session->form;
|
||||
my $i18n = WebGUI::International->new($self->session,"Shop");
|
||||
if ($form->get("label") eq "") {
|
||||
return $self->www_editAddress(sprintf($i18n->get('is a required field'), $i18n->get('label')));
|
||||
}
|
||||
if ($form->get("firstName") eq "") {
|
||||
return $self->www_editAddress(sprintf($i18n->get('is a required field'), $i18n->get('firstName')));
|
||||
}
|
||||
if ($form->get("lastName") eq "") {
|
||||
return $self->www_editAddress(sprintf($i18n->get('is a required field'), $i18n->get('lastName')));
|
||||
}
|
||||
if ($form->get("address1") eq "") {
|
||||
return $self->www_editAddress(sprintf($i18n->get('is a required field'), $i18n->get('address')));
|
||||
}
|
||||
if ($form->get("city") eq "") {
|
||||
return $self->www_editAddress(sprintf($i18n->get('is a required field'), $i18n->get('city')));
|
||||
}
|
||||
if ($form->get("code") eq "") {
|
||||
return $self->www_editAddress(sprintf($i18n->get('is a required field'), $i18n->get('code')));
|
||||
}
|
||||
if ($form->get("country") eq "") {
|
||||
return $self->www_editAddress(sprintf($i18n->get('is a required field'), $i18n->get('country')));
|
||||
}
|
||||
if ($form->get("phoneNumber") eq "") {
|
||||
return $self->www_editAddress(sprintf($i18n->get('is a required field'), $i18n->get('phone number')));
|
||||
}
|
||||
my %addressData = (
|
||||
label => $form->get("label"),
|
||||
firstName => $form->get("firstName"),
|
||||
lastName => $form->get("lastName"),
|
||||
address1 => $form->get("address1"),
|
||||
address2 => $form->get("address2"),
|
||||
address3 => $form->get("address3"),
|
||||
city => $form->get("city"),
|
||||
state => $form->get("state"),
|
||||
code => $form->get("code","zipcode"),
|
||||
country => $form->get("country","country"),
|
||||
phoneNumber => $form->get("phoneNumber","phone"),
|
||||
email => $form->get("email","email"),
|
||||
organization => $form->get("organization"),
|
||||
);
|
||||
my %addressData = $self->processAddressForm();
|
||||
if (exists $addressData{error}) {
|
||||
return $self->www_editAddress($addressData{error});
|
||||
}
|
||||
if ($form->get('addressId') eq '') {
|
||||
$self->addAddress(\%addressData);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -565,6 +565,10 @@ A hash reference that contains one of the following:
|
|||
|
||||
The unique id for a shipping address attached to this cart.
|
||||
|
||||
=head4 billingAddressId
|
||||
|
||||
The unique id for a billing address attached to this cart.
|
||||
|
||||
=head4 shipperId
|
||||
|
||||
The unique id of the configured shipping driver that will be used to ship these goods.
|
||||
|
|
@ -585,7 +589,7 @@ sub update {
|
|||
WebGUI::Error::InvalidParam->throw(error=>"Need a properties hash ref.");
|
||||
}
|
||||
my $id = id $self;
|
||||
foreach my $field (qw(shippingAddressId posUserId shipperId creationDate)) {
|
||||
foreach my $field (qw(billingAddressId shippingAddressId posUserId shipperId creationDate)) {
|
||||
$properties{$id}{$field} = (exists $newProperties->{$field}) ? $newProperties->{$field} : $properties{$id}{$field};
|
||||
}
|
||||
$self->session->db->setRow("cart","cartId",$properties{$id});
|
||||
|
|
@ -595,7 +599,7 @@ sub update {
|
|||
|
||||
=head2 updateFromForm ( )
|
||||
|
||||
Updates the cart totals.
|
||||
Updates the cart totals from form data.
|
||||
|
||||
=cut
|
||||
|
||||
|
|
@ -618,8 +622,10 @@ sub updateFromForm {
|
|||
my $i18n = WebGUI::International->new($self->session, "Shop");
|
||||
$error{id $self} = $i18n->get('mixed items warning');
|
||||
}
|
||||
my $book = $self->getAddressBook;
|
||||
#$book->processAddressForm()
|
||||
my $cartProperties = {};
|
||||
$cartProperties->{ shipperId } = $form->process( 'shipperId' ) if $form->process( 'shipperId' );
|
||||
$cartProperties->{ shipperId } = $form->process( 'shipperId' ) if $form->process( 'shipperId' );
|
||||
$self->update( $cartProperties );
|
||||
}
|
||||
|
||||
|
|
@ -698,6 +704,22 @@ sub www_removeItem {
|
|||
return $self->www_view;
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 www_setBillingAddress ()
|
||||
|
||||
Sets the billing address for the cart.
|
||||
|
||||
=cut
|
||||
|
||||
sub www_setBillingAddress {
|
||||
my $self = shift;
|
||||
my $form = $self->session->form;
|
||||
$self->update({billingAddressId=>$form->get('billingAddressId')});
|
||||
return $self->www_view;
|
||||
}
|
||||
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 www_setShippingAddress ()
|
||||
|
|
@ -710,10 +732,10 @@ sub www_setShippingAddress {
|
|||
my $self = shift;
|
||||
my $form = $self->session->form;
|
||||
if ($form->get("itemId") ne "") {
|
||||
$self->getItem($form->get("itemId"))->update({shippingAddressId=>$form->get('addressId')});
|
||||
$self->getItem($form->get("itemId"))->update({shippingAddressId=>$form->get('shippingAddressId')});
|
||||
}
|
||||
else {
|
||||
$self->update({shippingAddressId=>$form->get('addressId')});
|
||||
$self->update({shippingAddressId=>$form->get('shippingAddressId')});
|
||||
}
|
||||
return $self->www_view;
|
||||
}
|
||||
|
|
@ -742,11 +764,12 @@ Displays the shopping cart.
|
|||
=cut
|
||||
|
||||
sub www_view {
|
||||
my $self = shift;
|
||||
my $self = shift;
|
||||
my $session = $self->session;
|
||||
my $url = $session->url;
|
||||
my $i18n = WebGUI::International->new($session, "Shop");
|
||||
my @items = ();
|
||||
my $url = $session->url;
|
||||
my $form = $session->form;
|
||||
my $i18n = WebGUI::International->new($session, "Shop");
|
||||
my @items = ();
|
||||
my $taxDriver = WebGUI::Shop::Tax->getDriver( $session );
|
||||
|
||||
if($url->forceSecureConnection()){
|
||||
|
|
@ -888,8 +911,6 @@ sub www_view {
|
|||
#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'})
|
||||
|
|
@ -906,6 +927,7 @@ sub www_view {
|
|||
my $addressBook = $self->getAddressBook;
|
||||
$addressBook->appendAddressFormVars(\%var, 'shipping_', {});
|
||||
$addressBook->appendAddressFormVars(\%var, 'billing_', {});
|
||||
$var{sameShippingAsBilling} = WebGUI::Form::yesNo($session, {name => 'sameShippingAsBilling', value => $form->get('sameShippingAsBilling','yesNo')});
|
||||
}
|
||||
|
||||
# POS variables
|
||||
|
|
|
|||
|
|
@ -84,7 +84,14 @@ sub getDrivers {
|
|||
=head2 getOptions ( $cart )
|
||||
|
||||
Returns a list of options for the user to ship, along with the cost of using each one. It is a hash of hashrefs,
|
||||
with the key of the primary hash being the shipperId of the driver, and sub keys of label and price.
|
||||
with the key of the primary hash being the shipperId of the driver, and sub keys of label, price, and whether the
|
||||
price actually exists, to tell the difference between 0 and unknown.
|
||||
|
||||
{
|
||||
label => 'ShipDriver label',
|
||||
price => \d+,
|
||||
hasPrice => 1 || 0,
|
||||
}
|
||||
|
||||
=head3 $cart
|
||||
|
||||
|
|
@ -99,15 +106,24 @@ sub getOptions {
|
|||
my %options = ();
|
||||
SHIPPER: foreach my $shipper (@{$self->getShippers()}) {
|
||||
next SHIPPER unless $shipper->get('enabled');
|
||||
my $price = eval { $shipper->calculate($cart) };
|
||||
if (my $e = WebGUI::Error->caught()) {
|
||||
$self->session->log->warn($e->error);
|
||||
next SHIPPER;
|
||||
}
|
||||
next SHIPPER unless $shipper->canUse;
|
||||
my ($price, $hasPrice);
|
||||
if ($cart->get('shippingAddressId')) {
|
||||
my $price = eval { $shipper->calculate($cart) };
|
||||
if (my $e = WebGUI::Error->caught()) {
|
||||
$self->session->log->warn($e->error);
|
||||
next SHIPPER;
|
||||
}
|
||||
$hasPrice = 1;
|
||||
}
|
||||
else {
|
||||
$price = 0;
|
||||
$hasPrice = 0;
|
||||
}
|
||||
$options{$shipper->getId} = {
|
||||
label => $shipper->get("label"),
|
||||
price => $price,
|
||||
label => $shipper->get("label"),
|
||||
price => $price,
|
||||
hasPrice => 0,
|
||||
};
|
||||
}
|
||||
return \%options;
|
||||
|
|
|
|||
|
|
@ -1731,6 +1731,24 @@ our $I18N = {
|
|||
context => q|commerce setting help|
|
||||
},
|
||||
|
||||
'Billing Address' => {
|
||||
message => q|Billing Address|,
|
||||
lastUpdated => 0,
|
||||
context => q|template label for the cart|
|
||||
},
|
||||
|
||||
'Shipping Address' => {
|
||||
message => q|Shipping Address|,
|
||||
lastUpdated => 0,
|
||||
context => q|template label for the cart|
|
||||
},
|
||||
|
||||
'use same shipping as billing' => {
|
||||
message => q|Use the same shipping address as billing address.|,
|
||||
lastUpdated => 0,
|
||||
context => q|template label for the cart|
|
||||
},
|
||||
|
||||
};
|
||||
|
||||
1;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue