diff --git a/docs/upgrades/upgrade_7.5.2-7.5.3.pl b/docs/upgrades/upgrade_7.5.2-7.5.3.pl index ce391c423..000c8ba2a 100644 --- a/docs/upgrades/upgrade_7.5.2-7.5.3.pl +++ b/docs/upgrades/upgrade_7.5.2-7.5.3.pl @@ -125,6 +125,7 @@ sub migrateToNewCart { cartId varchar(22) binary not null primary key, sessionId varchar(22) binary not null, shippingAddressId varchar(22) binary, + shipperId varchar(22) binary, couponId varchar(22) binary, index sessionId (sessionId) )"); diff --git a/lib/WebGUI/Shop/Address.pm b/lib/WebGUI/Shop/Address.pm index f434fcc32..0674e5f74 100644 --- a/lib/WebGUI/Shop/Address.pm +++ b/lib/WebGUI/Shop/Address.pm @@ -232,16 +232,9 @@ The address book that this address belongs to. sub update { my ($self, $newProperties) = @_; my $id = id $self; - $properties{$id}{address2} = (exists $newProperties->{address2}) ? $newProperties->{address2} : $properties{$id}{address2}; - $properties{$id}{address3} = (exists $newProperties->{address3}) ? $newProperties->{address3} : $properties{$id}{address3}; - $properties{$id}{state} = (exists $newProperties->{state}) ? $newProperties->{state} : $properties{$id}{state}; - $properties{$id}{code} = $newProperties->{code} || $properties{$id}{code}; - $properties{$id}{city} = $newProperties->{city} || $properties{$id}{city}; - $properties{$id}{label} = $newProperties->{label} || $properties{$id}{label}; - $properties{$id}{name} = $newProperties->{name} || $properties{$id}{name}; - $properties{$id}{country} = $newProperties->{country} || $properties{$id}{country}; - $properties{$id}{address1} = $newProperties->{address1} || $properties{$id}{address1}; - $properties{$id}{phoneNumber} = $newProperties->{phoneNumber} || $properties{$id}{phoneNumber}; + foreach my $field (qw(address1 address2 address3 state code city label name country phoneNumber)) { + $properties{$id}{$field} = (exists $newProperties->{$field}) ? $newProperties->{$field} : $properties{$id}{$field}; + } $properties{$id}{addressBookId} = $self->addressBook->getId; $self->addressBook->session->db->setRow("address","addressId",$properties{$id}); } diff --git a/lib/WebGUI/Shop/AddressBook.pm b/lib/WebGUI/Shop/AddressBook.pm index 61006464e..87123f5b8 100644 --- a/lib/WebGUI/Shop/AddressBook.pm +++ b/lib/WebGUI/Shop/AddressBook.pm @@ -280,10 +280,9 @@ Assign the session that owns this adress book. Will automatically be set to "" i sub update { my ($self, $newProperties) = @_; my $id = id $self; - $properties{$id}{lastShipId} = $newProperties->{lastShipId} || $properties{$id}{lastShipId}; - $properties{$id}{lastPayId} = $newProperties->{lastPayId} || $properties{$id}{lastPayId}; - $properties{$id}{userId} = (exists $newProperties->{userId}) ? $newProperties->{userId} : $properties{$id}{userId}; - $properties{$id}{sessionId} = (exists $newProperties->{sessionId}) ? $newProperties->{sessionId} : $properties{$id}{sessionId}; + foreach my $field (qw(lastPayId lastShipId userId sessionId)) { + $properties{$id}{$field} = (exists $newProperties->{$field}) ? $newProperties->{$field} : $properties{$id}{$field}; + } ##Having both a userId and sessionId will confuse create. if ($properties{$id}{userId} ne "") { $properties{$id}{sessionId} = ""; diff --git a/lib/WebGUI/Shop/Cart.pm b/lib/WebGUI/Shop/Cart.pm index 9d7a67a6e..d47752688 100644 --- a/lib/WebGUI/Shop/Cart.pm +++ b/lib/WebGUI/Shop/Cart.pm @@ -295,13 +295,18 @@ The unique id for a coupon used in this cart. The unique id for a shipping address attached to this cart. +=head4 shipperId + +The unique id of the configured shipping driver that will be used to ship these goods. + =cut sub update { my ($self, $newProperties) = @_; my $id = id $self; - $properties{$id}{couponId} = $newProperties->{couponId} || $properties{$id}{couponId}; - $properties{$id}{shippingAddressId} = $newProperties->{shippingAddressId} || $properties{$id}{shippingAddressId}; + foreach my $field (qw(couponId shippingAddressId shipperId)) { + $properties{$id}{$field} = (exists $newProperties->{$field}) ? $newProperties->{$field} : $properties{$id}{$field}; + } $self->session->db->setRow("cart","cartId",$properties{$id}); } @@ -315,6 +320,10 @@ Update the cart and the return the user back to the asset. sub www_continueShopping { my $self = shift; + my $cartView = $self->www_update; + if ($error{id $self} ne "") { + return $cartView; + } return undef; } @@ -429,20 +438,39 @@ 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;"|}), - 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 => ($self->get("shippingAddressId") ne ""), couponField => WebGUI::Form::text($session, {name=>"couponCode", value=>"", size=>20}), - couponDiscount => "todo", - totalPrice => "todo", - tax => $self->formatCurrency($tax->calculate($self)), subtotalPrice => $self->formatCurrency($self->calculateSubtotal()), + couponDiscount => $self->formatCurrency(0), ); my $address = eval { $self->getShippingAddress }; - unless (WebGUI::Error->caught) { - $var{shippingAddress} = $address->getHtmlFormatted; + if (WebGUI::Error->caught("WebGUI::Error::ObjectNotFound")) { + # choose another address cuz we've got a problem + $self->update({shippingAddressId=>""}); } + if (WebGUI::Error->caught) { + $var{shippingPrice} = $var{tax} = $self->formatCurrency(0); + } + else { + $var{hasShippingAddress} = 1; + $var{shippingAddress} = $address->getHtmlFormatted; + $var{tax} = $self->formatCurrency($tax->calculate($self)); + my $options = WebGUI::Shop::Ship->getOptions($self); + my %formOptions = (); + my $defaultOption = ""; + foreach my $option (keys %{$options}) { + $defaultOption = $option; + $formOptions{$option} = $options->{$option}{label}." (".$self->formatCurrency($options->{$option}{price}).")"; + } + $var{shippingOptions} = WebGUI::Form::selectBox($session, {name=>"shipperId", options=>\%formOptions, defaultValue=>$defaultOption, value=>$self->get("shipperId")}); + $var{shippingPrice} = ($self->get("shipperId") ne "") ? $options->{$self->get("shipperId")}{price} : $options->{$defaultOption}{price}; + $var{shippingPrice} = $self->formatCurrency($var{shippingPrice}); + } + if ($self->get("couponId")) { + $var{couponDiscount} = $self->formatCurrency(0); + } + $var{totalPrice} = $self->formatCurrency($var{subtotalPrice} + $var{couponDiscount} + $var{shippingPrice} + $var{tax}); my $template = WebGUI::Asset::Template->new($session, $session->setting->get("shopCartTemplateId")); $template->prepare; return $session->style->userStyle($template->process(\%var)); diff --git a/lib/WebGUI/Shop/CartItem.pm b/lib/WebGUI/Shop/CartItem.pm index a2babf543..2568a45bc 100644 --- a/lib/WebGUI/Shop/CartItem.pm +++ b/lib/WebGUI/Shop/CartItem.pm @@ -280,12 +280,12 @@ sub update { $newProperties->{assetId} = $newProperties->{asset}->getId; $newProperties->{configuredTitle} = $newProperties->{asset}->getConfiguredTitle; } - $properties{$id}{assetId} = $newProperties->{assetId} || $properties{$id}{assetId}; - $properties{$id}{configuredTitle} = $newProperties->{configuredTitle} || $properties{$id}{configuredTitle}; + foreach my $field (qw(assetId configuredTitle shippingAddressId)) { + $properties{$id}{$field} = (exists $newProperties->{$field}) ? $newProperties->{$field} : $properties{$id}{$field}; + } if (exists $newProperties->{options} && ref($newProperties->{options}) eq "HASH") { $properties{$id}{options} = JSON::to_json($newProperties->{options}); } - $properties{$id}{shippingAddressId} = $newProperties->{shippingAddressId} || $properties{$id}{shippingAddressId}; $self->cart->session->db->setRow("cartItems","itemId",$properties{$id}); } diff --git a/lib/WebGUI/Shop/Ship.pm b/lib/WebGUI/Shop/Ship.pm index a06460b45..4bd14f018 100644 --- a/lib/WebGUI/Shop/Ship.pm +++ b/lib/WebGUI/Shop/Ship.pm @@ -87,7 +87,7 @@ sub getDrivers { #------------------------------------------------------------------- -=head2 getOptions ( $session, $cart ) +=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. @@ -101,11 +101,17 @@ A WebGUI::Session object. A WebGUI::Error::InvalidParam exception will be throw =cut sub getOptions { - my $class = shift; - my $session = shift; - WebGUI::Error::InvalidParam->throw(error => q{Must provide a session variable}) - unless ref $session eq 'WebGUI::Session'; - return; + my ($class, $cart) = @_; + my $session = $cart->session; + WebGUI::Error::InvalidParam->throw(error => q{Need a cart.}) unless $cart->isa("WebGUI::Shop::Cart"); + my %options = (); + foreach my $shipper (@{$class->getShippers($session)}) { + $options{$shipper->getId} = { + label => $shipper->get("label"), + price => $shipper->calculate($cart), + }; + } + return \%options; } #------------------------------------------------------------------- @@ -127,14 +133,14 @@ sub getShippers { my $session = shift; WebGUI::Error::InvalidParam->throw(error => q{Must provide a session variable}) unless ref $session eq 'WebGUI::Session'; - my $drivers; + my @drivers = (); my $sth = $session->db->prepare('select shipperId from shipper'); $sth->execute(); while (my $driver = $sth->hashRef()) { - push @{ $drivers }, WebGUI::Shop::Ship->new($session, $driver->{shipperId}); + push @drivers, WebGUI::Shop::Ship->new($session, $driver->{shipperId}); } $sth->finish; - return $drivers; + return \@drivers; } #-------------------------------------------------------------------