sessionization
This commit is contained in:
parent
5c5c8075cf
commit
991f4bea9f
4 changed files with 60 additions and 28 deletions
|
|
@ -134,11 +134,12 @@ Returns a reference to an array of all enabled instantiated payment plugins.
|
|||
=cut
|
||||
|
||||
sub getEnabledPlugins {
|
||||
my ($session) = @_;
|
||||
my (@enabledPlugins, $plugin, @plugins);
|
||||
@enabledPlugins = $self->session->db->buildArray("select namespace from commerceSettings where type='Shipping' and fieldName='enabled' and fieldValue='1'");
|
||||
@enabledPlugins = $session->db->buildArray("select namespace from commerceSettings where type='Shipping' and fieldName='enabled' and fieldValue='1'");
|
||||
|
||||
foreach (@enabledPlugins) {
|
||||
$plugin = WebGUI::Commerce::Shipping->load($_);
|
||||
$plugin = WebGUI::Commerce::Shipping->load($session, $_);
|
||||
push(@plugins, $plugin) if ($plugin);
|
||||
}
|
||||
|
||||
|
|
@ -158,19 +159,21 @@ The namespace of the plugin.
|
|||
=cut
|
||||
|
||||
sub init {
|
||||
my ($class, $namespace, $properties, $shoppingCart);
|
||||
my ($class, $session, $namespace, $properties, $shoppingCart);
|
||||
$class = shift;
|
||||
$session = shift;
|
||||
$namespace = shift;
|
||||
|
||||
$self->session->errorHandler->fatal('No namespace passed to init.') unless ($namespace);
|
||||
$session->errorHandler->fatal('No namespace passed to init.') unless ($namespace);
|
||||
|
||||
$properties = $self->session->db->buildHashRef("select fieldName, fieldValue from commerceSettings where namespace=".$self->session->db->quote($namespace)." and type='Shipping'");
|
||||
$shoppingCart = WebGUI::Commerce::ShoppingCart->new;
|
||||
$properties = $session->db->buildHashRef("select fieldName, fieldValue from commerceSettings where namespace=".$session->db->quote($namespace)." and type='Shipping'");
|
||||
$shoppingCart = WebGUI::Commerce::ShoppingCart->new($session);
|
||||
|
||||
bless {_properties=>$properties,
|
||||
_shippingParameters => {},
|
||||
_shoppingCart => $shoppingCart,
|
||||
_namespace=>$namespace,
|
||||
_session=>$session,
|
||||
_enabled=>$properties->{enabled},
|
||||
_shippingItems => []}, $class;
|
||||
}
|
||||
|
|
@ -203,16 +206,17 @@ The namespace of the plugin.
|
|||
sub load {
|
||||
my ($class, $namespace, $load, $cmd, $plugin);
|
||||
$class = shift;
|
||||
my $session = shift;
|
||||
$namespace = shift;
|
||||
|
||||
$self->session->errorHandler->fatal('No namespace passed to load.') unless ($namespace);
|
||||
$session->errorHandler->fatal('No namespace passed to load.') unless ($namespace);
|
||||
|
||||
$cmd = "WebGUI::Commerce::Shipping::$namespace";
|
||||
$load = "use $cmd";
|
||||
eval($load);
|
||||
$self->session->errorHandler->warn("Shipping plugin failed to compile: $cmd.".$@) if($@);
|
||||
$session->errorHandler->warn("Shipping plugin failed to compile: $cmd.".$@) if($@);
|
||||
$plugin = eval($cmd."->init");
|
||||
$self->session->errorHandler->warn("Couldn't instantiate shipping plugin: $cmd.".$@) if($@);
|
||||
$session->errorHandler->warn("Couldn't instantiate shipping plugin: $cmd.".$@) if($@);
|
||||
return $plugin;
|
||||
}
|
||||
|
||||
|
|
@ -225,7 +229,8 @@ Returns the (display) name of the plugin. You must override this method.
|
|||
=cut
|
||||
|
||||
sub name {
|
||||
return $self->session->errorHandler->fatal("You must override the name method in the shipping plugin.");
|
||||
my ($session) = @_;
|
||||
return $session->errorHandler->fatal("You must override the name method in the shipping plugin.");
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
|
@ -298,6 +303,19 @@ sub processOptionsForm {
|
|||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 session
|
||||
|
||||
Returns the cached, local session variable.
|
||||
|
||||
=cut
|
||||
|
||||
sub session {
|
||||
my ($self) = @_;
|
||||
return $self->{_session};
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 setOptions ( options )
|
||||
|
||||
Stores the supplied option hash into the plugin object.
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ shopping cart is tied to the sessionId and, thus, expires when the sessionId exp
|
|||
|
||||
=head1 SYNOPSIS
|
||||
|
||||
$shoppingCart = WebGUI::Commerce::ShoppingCart->new;
|
||||
$shoppingCart = WebGUI::Commerce::ShoppingCart->new($session);
|
||||
|
||||
$shoppingCart->add('myItemId', 'myItem', 3);
|
||||
$shoppingCart->setQuantity('myItemId', 'myItem', 2);
|
||||
|
|
@ -243,16 +243,30 @@ of the current user.
|
|||
=cut
|
||||
|
||||
sub new {
|
||||
my ($class, $sessionId, $sth, $row, %items);
|
||||
my ($class, $session, $sessionId, $sth, $row, %items);
|
||||
$class = shift;
|
||||
$sessionId = shift || $self->session->var->get("sessionId");
|
||||
$session = shift;
|
||||
$sessionId = shift || $session->var->get("sessionId");
|
||||
|
||||
$sth = $self->session->db->read("select * from shoppingCart where sessionId=".$self->session->db->quote($sessionId));
|
||||
$sth = $session->db->read("select * from shoppingCart where sessionId=".$session->db->quote($sessionId));
|
||||
while ($row = $sth->hashRef) {
|
||||
$items{$row->{itemId}."_".$row->{itemType}} = $row;
|
||||
}
|
||||
|
||||
bless {_sessionId => $sessionId, _items => \%items}, $class;
|
||||
bless {_session=>$session, _sessionId => $sessionId, _items => \%items}, $class;
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 session
|
||||
|
||||
Returns the cached, local session variable.
|
||||
|
||||
=cut
|
||||
|
||||
sub session {
|
||||
my ($self) = @_;
|
||||
return $self->{_session};
|
||||
}
|
||||
|
||||
1;
|
||||
|
|
|
|||
|
|
@ -70,7 +70,7 @@ sub _shippingSelected {
|
|||
my $session = shift;
|
||||
return 0 unless ($session->scratch->get('shippingMethod'));
|
||||
|
||||
my $plugin = WebGUI::Commerce::Shipping->load($session->scratch->get('shippingMethod'));
|
||||
my $plugin = WebGUI::Commerce::Shipping->load($session, $session->scratch->get('shippingMethod'));
|
||||
if ($plugin) {
|
||||
$plugin->setOptions(Storable::thaw($session->scratch->get('shippingOptions'))) if ($session->scratch->get('shippingOptions'));
|
||||
return 1 if ($plugin->enabled && $plugin->optionsOk);
|
||||
|
|
@ -82,7 +82,7 @@ sub _shippingSelected {
|
|||
#-------------------------------------------------------------------
|
||||
sub www_addToCart {
|
||||
my $session = shift;
|
||||
WebGUI::Commerce::ShoppingCart->new->add($session->form->process("itemId"), $session->form->process("itemType"), $session->form->process("quantity"));
|
||||
WebGUI::Commerce::ShoppingCart->new($session)->add($session->form->process("itemId"), $session->form->process("itemType"), $session->form->process("quantity"));
|
||||
|
||||
return WebGUI::Operation::execute('viewCart');
|
||||
}
|
||||
|
|
@ -134,7 +134,7 @@ sub www_checkoutConfirm {
|
|||
$var{errorLoop} = [ map {{message => $_}} @{$errors} ] if $errors;
|
||||
|
||||
# Put contents of cart in template vars
|
||||
$shoppingCart = WebGUI::Commerce::ShoppingCart->new;
|
||||
$shoppingCart = WebGUI::Commerce::ShoppingCart->new($session);
|
||||
($normal, $recurring) = $shoppingCart->getItems;
|
||||
|
||||
foreach (@$normal) {
|
||||
|
|
@ -163,7 +163,7 @@ sub www_checkoutConfirm {
|
|||
|
||||
$var{subTotal} = sprintf('%.2f', $total);
|
||||
|
||||
$shipping = WebGUI::Commerce::Shipping->load($session->scratch->get('shippingMethod'));
|
||||
$shipping = WebGUI::Commerce::Shipping->load($session, $session->scratch->get('shippingMethod'));
|
||||
$shipping->setOptions(Storable::thaw($session->scratch->get('shippingOptions'))) if ($session->scratch->get('shippingOptions'));
|
||||
|
||||
$var{shippingName} = $shipping->name;
|
||||
|
|
@ -213,12 +213,12 @@ sub www_checkoutSubmit {
|
|||
return WebGUI::Operation::execute('checkout') unless (_paymentSelected && _shippingSelected);
|
||||
|
||||
# Load shipping plugin.
|
||||
$shipping = WebGUI::Commerce::Shipping->load($session->scratch->get('shippingMethod'));
|
||||
$shipping = WebGUI::Commerce::Shipping->load($session, $session->scratch->get('shippingMethod'));
|
||||
$shipping->setOptions(Storable::thaw($session->scratch->get('shippingOptions'))) if ($session->scratch->get('shippingOptions'));
|
||||
|
||||
# Load payment plugin.
|
||||
$plugin = WebGUI::Commerce::Payment->load($session, $session->scratch->get('paymentGateway'));
|
||||
$shoppingCart = WebGUI::Commerce::ShoppingCart->new;
|
||||
$shoppingCart = WebGUI::Commerce::ShoppingCart->new($session);
|
||||
($normal, $recurring) = $shoppingCart->getItems;
|
||||
|
||||
# Check if shoppingcart contains any items. If not the user probably clicked reload, so we redirect to the current page.
|
||||
|
|
@ -359,7 +359,7 @@ sub www_confirmTransaction {
|
|||
#-------------------------------------------------------------------
|
||||
sub www_deleteCartItem {
|
||||
my $session = shift;
|
||||
WebGUI::Commerce::ShoppingCart->new->delete($session->form->process("itemId"), $session->form->process("itemType"));
|
||||
WebGUI::Commerce::ShoppingCart->new($session)->delete($session->form->process("itemId"), $session->form->process("itemType"));
|
||||
|
||||
return WebGUI::Operation::execute('viewCart');
|
||||
}
|
||||
|
|
@ -471,7 +471,7 @@ sub www_editCommerceSettings {
|
|||
# Shipping plugins...
|
||||
# Check which payment plugins will compile, and load them.
|
||||
foreach (@{$session->config->get("shippingPlugins")}) {
|
||||
$plugin = WebGUI::Commerce::Shipping->load($_);
|
||||
$plugin = WebGUI::Commerce::Shipping->load($session, $_);
|
||||
if ($plugin) {
|
||||
push(@shippingPlugins, $plugin);
|
||||
$shippingPlugins{$_} = $plugin->name;
|
||||
|
|
@ -713,7 +713,7 @@ sub www_selectShippingMethod {
|
|||
_clearShippingScratch;
|
||||
|
||||
$i18n = WebGUI::International->new($session, 'Commerce');
|
||||
$plugins = WebGUI::Commerce::Shipping->getEnabledPlugins;
|
||||
$plugins = WebGUI::Commerce::Shipping->getEnabledPlugins($session);
|
||||
|
||||
if (scalar(@$plugins) > 1) {
|
||||
foreach (@$plugins) {
|
||||
|
|
@ -742,7 +742,7 @@ sub www_selectShippingMethod {
|
|||
#-------------------------------------------------------------------
|
||||
sub www_selectShippingMethodSave {
|
||||
my $session = shift;
|
||||
my $shipping = WebGUI::Commerce::Shipping->load($session->form->process("shippingMethod"));
|
||||
my $shipping = WebGUI::Commerce::Shipping->load($session, $session->form->process("shippingMethod"));
|
||||
|
||||
$shipping->processOptionsForm;
|
||||
return WebGUI::Operation::execute('selectShipping') unless ($shipping->optionsOk);
|
||||
|
|
@ -766,7 +766,7 @@ sub www_transactionComplete {
|
|||
#-------------------------------------------------------------------
|
||||
sub www_updateCart {
|
||||
my $session = shift;
|
||||
my $shoppingCart = WebGUI::Commerce::ShoppingCart->new;
|
||||
my $shoppingCart = WebGUI::Commerce::ShoppingCart->new($session);
|
||||
|
||||
foreach my $formElement (keys(%{$session{form}})) {
|
||||
if ($formElement =~ m/^quantity~([^~]*)~([^~]*)$/) {
|
||||
|
|
@ -785,7 +785,7 @@ sub www_viewCart {
|
|||
$i18n = WebGUI::International->new($session, 'Commerce');
|
||||
|
||||
# Put contents of cart in template vars
|
||||
$shoppingCart = WebGUI::Commerce::ShoppingCart->new;
|
||||
$shoppingCart = WebGUI::Commerce::ShoppingCart->new($session);
|
||||
($normal, $recurring) = $shoppingCart->getItems;
|
||||
|
||||
foreach (@$normal) {
|
||||
|
|
|
|||
|
|
@ -415,7 +415,7 @@ sub www_listSubscriptions {
|
|||
#-------------------------------------------------------------------
|
||||
sub www_purchaseSubscription {
|
||||
my $session = shift;
|
||||
WebGUI::Commerce::ShoppingCart->new->add($session->form->process("sid"), 'Subscription');
|
||||
WebGUI::Commerce::ShoppingCart->new($session)->add($session->form->process("sid"), 'Subscription');
|
||||
|
||||
return WebGUI::HTTP::setRedirect($session->url->page('op=checkout'));
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue