added basic quantity checking for inventory management, and a dateAdded field to the cart in case we need to monitor how long items have been in the cart

This commit is contained in:
JT Smith 2008-03-17 17:33:54 +00:00
parent 537824fdc9
commit c61890392c
4 changed files with 74 additions and 10 deletions

View file

@ -205,15 +205,16 @@ sub migrateToNewCart {
couponId varchar(22) binary,
index sessionId (sessionId)
)");
$session->db->write("create table cartItems (
$session->db->write("create table cartItem (
itemId varchar(22) binary not null primary key,
cartId varchar(22) binary not null,
assetId varchar(22) binary not null,
dateAdded datetime not null,
options mediumtext,
configuredTitle varchar(255),
shippingAddressId varchar(22) binary,
quantity integer not null default 1,
index cartId_assetId (cartId,assetId)
index cartId_assetId_dateAdded (cartId,assetId,dateAdded)
)");
$session->db->write("drop table shoppingCart");
$session->setting->add('shopCartTemplateId','aIpCmr9Hi__vgdZnDTz1jw');

View file

@ -72,6 +72,23 @@ sub addToCart {
#-------------------------------------------------------------------
=head2 adjustQuantityAvailable ( amount )
Adjust the quantity of this product that is available. Send a negative number to decrease, or a positive number to increase. Returns getQuantityAvailable.
=head3 amount
A signed integer that represents the amount to adjust
=cut
sub adjustQuantityAvailable {
my ($self, $amount) = @_;
return $self->setQuantityAvailable($self->getQuantityAvailable + $amount);
}
#-------------------------------------------------------------------
=head2 applyOptions ( options )
Accepts a configuration data hash reference that configures a sku a certain way. For example to turn "a t-shirt" into "an XL red t-shirt". See also getOptions().
@ -206,12 +223,13 @@ sub getOptions {
=head2 getMaxAllowedInCart ( )
Returns 99999999. Should be overriden by subclasses that have a specific value. Subclasses that are unique should return 1. Subclasses that have an inventory count should return the amount in inventory.
Returns getQuantityAvailable(). Should be overriden by subclasses that have a specific value. Subclasses that are unique should return 1. Subclasses that have an inventory count should return the amount in inventory.
=cut
sub getMaxAllowedInCart {
return 99999999;
my $self = shift;
return $self->getQuantityAvailable;
}
#-------------------------------------------------------------------
@ -228,6 +246,18 @@ sub getPrice {
#-------------------------------------------------------------------
=head2 getQuantityAvailable ( )
Returns 99999999. Needs to be overriden by subclasses. Tells the commerce system how many of this item is on hand.
=cut
sub getQuantityAvailable {
return 99999999;
}
#-------------------------------------------------------------------
=head2 getTaxRate ( )
Returns undef unless the "Override tax rate?" switch is set to yes. If it is, then it returns the value of the "Tax Rate Override" field.
@ -267,6 +297,19 @@ sub indexContent {
}
#-------------------------------------------------------------------
=head2 isRecurring
Returns a boolean indicating whether this sku is recurring. Defaultly returns 0. Needs to be overriden by subclasses that do recurring transactions, because not all payment gateways can process recurring transactions.
=cut
sub isRecurring {
return 0;
}
#-------------------------------------------------------------------
=head2 isShippingRequired
@ -323,6 +366,23 @@ sub processStyle {
#-------------------------------------------------------------------
=head2 setQuantityAvailable ( amount )
Set the quantity of this product that is available. Returns getQuantityAvailable(). Should be overridden by skus that keep track of quantity.
=head3 amount
A signed integer that represents the quantity to set it to.
=cut
sub setQuantityAvailable {
my ($self, $amount) = @_;
return $self->getQuantityAvailable;
}
#-------------------------------------------------------------------
=head2 www_view ( )
Renders self->view based upon current style, subject to timeouts. Returns Privilege::noAccess() if canView is False.

View file

@ -238,7 +238,7 @@ Returns an array reference of WebGUI::Asset::Sku objects that are in the cart.
sub getItems {
my ($self) = @_;
my @itemsObjects = ();
my $items = $self->session->db->read("select itemId from cartItems where cartId=?",[$self->getId]);
my $items = $self->session->db->read("select itemId from cartItem where cartId=?",[$self->getId]);
while (my ($itemId) = $items->array) {
push(@itemsObjects, $self->getItem($itemId));
}

View file

@ -62,9 +62,10 @@ sub create {
WebGUI::Error::InvalidObject->throw(expected=>"WebGUI::Asset::Sku", got=>(ref $sku), error=>"Need a SKU item.");
}
my $itemId = $cart->session->id->generate;
$cart->session->db->write('insert into cartItems (quantity, cartId, assetId, itemId) values (1,?,?,?)', [$cart->getId, $sku->getId, $itemId]);
$cart->session->db->write('insert into cartItem (quantity, cartId, assetId, itemId, dateAdded) values (1,?,?,?,now())', [$cart->getId, $sku->getId, $itemId]);
my $self = $class->new($cart, $itemId);
$self->update({asset=>$sku});
$sku->adjustQuantityAvailable(-1);
return $self;
}
@ -185,7 +186,7 @@ sub new {
unless (defined $itemId) {
WebGUI::Error::InvalidParam->throw(error=>"Need an itemId.");
}
my $item = $cart->session->db->quickHashRef('select * from cartItems where itemId=?', [$itemId]);
my $item = $cart->session->db->quickHashRef('select * from cartItem where itemId=?', [$itemId]);
if ($item->{itemId} eq "") {
WebGUI::Error::ObjectNotFound->throw(error=>"Item not found.", id=>$itemId);
}
@ -209,7 +210,7 @@ Removes this item from the cart.
sub remove {
my $self = shift;
$self->cart->session->db->deleteRow("cartItems","itemId",$self->getId);
$self->cart->session->db->deleteRow("cartItem","itemId",$self->getId);
undef $self;
return undef;
}
@ -230,6 +231,7 @@ The number to set the quantity to. Zero or less will remove the item from cart.
sub setQuantity {
my ($self, $quantity) = @_;
my $id = id $self;
my $currentQuantity = $self->get("quantity");
if ($quantity > $self->getSku->getMaxAllowedInCart) {
WebGUI::Error::Shop::MaxOfItemInCartReached->throw(error=>"Cannot have that many of this item in cart.");
}
@ -237,7 +239,8 @@ sub setQuantity {
return $self->remove;
}
$properties{$id}{quantity} = $quantity;
$self->cart->session->db->setRow("cartItems","itemId", $properties{$id});
$self->getSku->adjustQuantityAvailable($currentQuantity + $quantity);
$self->cart->session->db->setRow("cartItem","itemId", $properties{$id});
}
#-------------------------------------------------------------------
@ -286,7 +289,7 @@ sub update {
if (exists $newProperties->{options} && ref($newProperties->{options}) eq "HASH") {
$properties{$id}{options} = JSON::to_json($newProperties->{options});
}
$self->cart->session->db->setRow("cartItems","itemId",$properties{$id});
$self->cart->session->db->setRow("cartItem","itemId",$properties{$id});
}