added some helper methods for addressing, and got the cart closer to working

This commit is contained in:
JT Smith 2008-03-05 16:32:08 +00:00
parent 8c862439f9
commit 112834a9b7
4 changed files with 112 additions and 8 deletions

View file

@ -103,6 +103,25 @@ sub get {
#-------------------------------------------------------------------
=head2 getHtmlFormatted ()
Returns an HTML formatted address for display.
=cut
sub getHtmlFormatted {
my $self = shift;
my $address = $self->get("name") . "<br />" . $self->get("address1") . "<br />";
$address .= $self->get("address2") . "<br />" if ($self->get("address2") ne "");
$address .= $self->get("address3") . "<br />" if ($self->get("address3") ne "");
$address .= $self->get("city") . ",";
$address .= $self->get("state") . " " if ($self->get("state") ne "");
$address .= $self->get("code") if ($self->get("code") ne "");
$address .= '<br />' . $self->get("country");
}
#-------------------------------------------------------------------
=head2 getId ()
Returns the unique id of this item.

View file

@ -6,6 +6,7 @@ use Class::InsideOut qw{ :std };
use WebGUI::Asset::Template;
use WebGUI::Exception::Shop;
use WebGUI::International;
use WebGUI::Shop::AddressBook;
use WebGUI::Shop::CartItem;
# use WebGUI::Shop::Coupon;
use WebGUI::Shop::Ship;
@ -53,6 +54,26 @@ sub addItem {
return $item;
}
#-------------------------------------------------------------------
=head2 calculateSubtotal ()
Returns the subtotal of the items in the cart.
=cut
sub calculateSubtotal {
my $self = shift;
my $subtotal = 0;
foreach my $item (@{$self->getItems}) {
my $sku = $item->getSku;
$subtotal += $sku->getPrice * $item->get("quantity");
}
return $subtotal;
}
#-------------------------------------------------------------------
=head2 create ( session )
@ -110,6 +131,23 @@ sub empty {
#-------------------------------------------------------------------
=head2 formatCurrency ( amount )
Formats a number as a float with two digits after the decimal like 0.00.
=head3 amount
The number to format.
=cut
sub formatCurrency {
my ($self, $amount) = @_;
return sprintf("%.2f", $amount);
}
#-------------------------------------------------------------------
=head2 get ( [ property ] )
Returns a duplicated hash reference of this objects data.
@ -131,6 +169,19 @@ sub get {
#-------------------------------------------------------------------
=head2 getAddressBook ()
Returns a reference to the address book for the user who's cart this is.
=cut
sub getAddressBook {
my $self = shift;
return WebGUI::Shop::AddressBook->create($self->session);
}
#-------------------------------------------------------------------
=head2 getId ()
Returns the unique id for this cart.
@ -162,6 +213,19 @@ sub getItems {
#-------------------------------------------------------------------
=head2 getShippingAddress ()
Returns the WebGUI::Shop::Address object that is attached to this cart for shipping.
=cut
sub getShippingAddress {
my $self = shift;
return $self->getAddressBook->getAddress($self->get("shippingAddressId"));
}
#-------------------------------------------------------------------
=head2 new ( session, cartId )
Constructor. Instanciates a cart based upon a cartId.
@ -297,16 +361,20 @@ sub www_view {
quantityField => WebGUI::Form::integer($session, {name=>"quantity-".$item->getId, value=>$item->get("quantity")}),
isUnique => ($sku->getMaxAllowedInCart == 1),
isShippable => $sku->isShippingRequired,
extendedPrice => sprintf("%.2f", ($sku->getPrice * $item->get("quantity"))),
price => sprintf("%.2f", $sku->getPrice),
extendedPrice => $self->formatCurrency($sku->getPrice * $item->get("quantity")),
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;"|}),
shippingAddress => "todo",
shipToButton => WebGUI::Form::submit($session, {value=>$i18n->get("ship to button"),
extras=>q|onclick="this.form.shop.value='address';this.form.method.value='view';this.form.itemId.value='|.$item->getId.q|';this.form.submit;"|}),
);
my $address = eval { $item->getShippingAddress };
unless (WebGUI::Error->caught) {
$properties{shippingAddress} = $address->getHtmlFormatted;
}
push(@items, \%properties);
}
my $tax = WebGUI::Shop::Tax->new($self->session);
my %var = (
%{$self->get},
items => \@items,
@ -323,17 +391,20 @@ sub www_view {
extras=>q|onclick="this.form.method.value='continueShopping';this.form.submit;"|}),
chooseShippingButton => WebGUI::Form::submit($session, {value=>$i18n->get("choose shipping button"),
extras=>q|onclick="this.form.shop.value='address';this.form.method.value='view';this.form.submit;"|}),
shipppingAddress => "todo",
shippingOptions => "todo",
shipToButton => WebGUI::Form::submit($session, {value=>$i18n->get("ship to button"),
extras=>q|onclick="this.form.shop.value='address';this.form.method.value='view';this.form.submit;"|}),
hasShippingAddress => "todo",
hasShippingAddress => ($self->get("shippingAddressId") ne ""),
couponField => WebGUI::Form::text($session, {name=>"couponCode", value=>"", size=>20}),
couponDiscount => "todo",
totalPrice => "todo",
tax => "todo",
subtotalPrice => "todo",
tax => $self->formatCurrency($tax->calculate($self)),
subtotalPrice => $self->formatCurrency($self->calculateSubtotal()),
);
my $address = eval { $self->getShippingAddress };
unless (WebGUI::Error->caught) {
$var{shippingAddress} = $address->getHtmlFormatted;
}
my $template = WebGUI::Asset::Template->new($session, $session->setting->get("shopCartTemplateId"));
$template->prepare;
return $session->style->userStyle($template->process(\%var));

View file

@ -112,6 +112,19 @@ sub getId {
}
#-------------------------------------------------------------------
=head2 getShippingAddress ()
Returns the WebGUI::Shop::Address object that is attached to this item for shipping.
=cut
sub getShippingAddress {
my $self = shift;
return $self->cart->getAddressBook->getAddress($self->get("shippingAddressId"));
}
#-------------------------------------------------------------------
=head2 getSku ( )

View file

@ -88,7 +88,7 @@ sub add {
Calculate the tax for the contents of the cart. The tax rate is calculated off
of the shipping address stored in the cart. If an item in the cart has an alternate
address, that is used instead. Finally, if the item in the cart has a Sku with a tax
rate override, that rate overrides all.
rate override, that rate overrides all. Returns 0 if no shipping address has been attached to the cart yet.
=cut
@ -98,6 +98,7 @@ sub calculate {
WebGUI::Error::InvalidParam->throw(error => 'Must pass in a WebGUI::Shop::Cart object')
unless ref($cart) eq 'WebGUI::Shop::Cart';
my $book = WebGUI::Shop::AddressBook->create($self->session);
return 0 if $cart->get('shippingAddressId') eq "";
my $address = $book->getAddress($cart->get('shippingAddressId'));
my $tax = 0;
foreach my $item (@{ $cart->getItems }) {