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

@ -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));