From e5ead13da3d8fae6a63168d0d415767fa2046503 Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Mon, 28 Sep 2009 10:04:52 -0700 Subject: [PATCH] Force the user to pick a shipping method before checking out. Implements RFE #10940 If there is only 1 shipping method, automatically choose it. Displays an message to the user if they have an address, but no shipping method. --- docs/changelog/7.x.x.txt | 1 + lib/WebGUI/Shop/Cart.pm | 41 +++++++++++++++++++++++---------- lib/WebGUI/i18n/English/Shop.pm | 12 ++++++++++ t/Shop/Cart.t | 10 ++++---- 4 files changed, 48 insertions(+), 16 deletions(-) diff --git a/docs/changelog/7.x.x.txt b/docs/changelog/7.x.x.txt index 47c1aed4c..78bde1a91 100644 --- a/docs/changelog/7.x.x.txt +++ b/docs/changelog/7.x.x.txt @@ -14,6 +14,7 @@ - fixed #11032: The Thingy form field "otherThingy" not checking for privilege - fixed #11037: Maintenance page is being cached - fixed #11056: No history in wiki + - added RFE #10940: Force the user to pick a shipping method before checking out. 7.8.0 - upgraded YUI to 2.8.0r4 diff --git a/lib/WebGUI/Shop/Cart.pm b/lib/WebGUI/Shop/Cart.pm index 59f0fbd12..b9a3d5ace 100644 --- a/lib/WebGUI/Shop/Cart.pm +++ b/lib/WebGUI/Shop/Cart.pm @@ -14,6 +14,7 @@ use WebGUI::Shop::Credit; use WebGUI::Shop::Ship; use WebGUI::Shop::Tax; use WebGUI::User; +use Tie::IxHash; =head1 NAME @@ -520,6 +521,9 @@ sub readyForCheckout { return 0 if $total < $requiredAmount; } + ##Must have a configured shipping id. + return 0 if ! $self->get('shipperId'); + ##Check for any other logged errors return 0 if $error{ id $self }; @@ -843,22 +847,35 @@ sub www_view { $var{shippingAddress} = $address->getHtmlFormatted; my $ship = WebGUI::Shop::Ship->new($self->session); my $options = $ship->getOptions($self); - my %formOptions = (); - my $defaultOption = ""; - foreach my $option (keys %{$options}) { - $defaultOption = $option; - $formOptions{$option} = $options->{$option}{label}." (".$self->formatCurrency($options->{$option}{price}).")"; - } - if ($defaultOption) { - $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}); - } - else { + 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}).")"; + } + my $defaultOption = $self->get('shipperId') ? $self->get('shipperId') : ''; + $var{shippingOptions} = WebGUI::Form::selectBox($session, {name=>"shipperId", options=>\%formOptions, value=>$self->get("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 diff --git a/lib/WebGUI/i18n/English/Shop.pm b/lib/WebGUI/i18n/English/Shop.pm index 89141c9e3..a03244c55 100644 --- a/lib/WebGUI/i18n/English/Shop.pm +++ b/lib/WebGUI/i18n/English/Shop.pm @@ -1659,6 +1659,18 @@ our $I18N = { context => q|Error message in the manage ship driver screen.|, }, + 'Choose a shipping method' => { + message => q|Choose a shipping method|, + lastUpdated => 0, + context => q|Label to make the user choose a shipping method|, + }, + + 'Choose a shipping method and update the cart to checkout' => { + message => q|Choose a shipping method and update the cart to checkout|, + lastUpdated => 0, + context => q|Label to make the user choose a shipping method|, + }, + }; 1; diff --git a/t/Shop/Cart.t b/t/Shop/Cart.t index 683f5e3d5..b3b96e266 100644 --- a/t/Shop/Cart.t +++ b/t/Shop/Cart.t @@ -33,7 +33,7 @@ my $i18n = WebGUI::International->new($session, "Shop"); #---------------------------------------------------------------------------- # Tests -plan tests => 29; # Increment this number for each test you create +plan tests => 30; # Increment this number for each test you create #---------------------------------------------------------------------------- # put your tests here @@ -105,13 +105,15 @@ my $ship = WebGUI::Shop::Ship->new( $session ); my $shipper = $ship->addShipper( 'WebGUI::Shop::ShipDriver::FlatRate', {flatFee => 1 } ); $cart->update( { shippingAddressId => $address->getId, - shipperId => $shipper->getId, } ); -is($cart->readyForCheckout, 1, 'Cart is ready for checkout'); +ok(! $cart->readyForCheckout, 'readyForCheckout: returns false due to no shipperId'); + +$cart->update( { shipperId => $shipper->getId, } ); +ok($cart->readyForCheckout, '... returns true when it has shipperId, and shipping address'); # Check shipping address constraint $cart->update( {shippingAddressId => 'Does Not Exist'} ); -is( $cart->readyForCheckout, 0, 'Cannot checkout cart without shipping address' ); +ok( ! $cart->readyForCheckout, '... Cannot checkout cart without shipping address' ); # Check minimum transaction amount $session->setting->set( 'shopCartCheckoutMinimum', 1000 );