[wellwell/interchange6: 2/5] Use the session_id for not logged in and take over the cart on restoring
Stefan Hornburg <[email protected]> Fri, 03 Mar 2017 07:53:24 +0000
| Newsgroups | gmane.comp.web.interchange.cvs |
|---|---|
| Message-ID | <[email protected]> |
commit 6624fe9154cb565eab386f2c387bb4e212450525 Author: Marco Pessotto <[email protected]> Date: Thu Nov 21 15:12:27 2013 +0100 Use the session_id for not logged in and take over the cart on restoring database/mysql/cart_products.sql | 7 +++++++ database/mysql/carts.sql | 10 ++++++++++ lib/WellWell/DatabaseCart.pm | 24 +++++++++++++++++++----- 3 files changed, 36 insertions(+), 5 deletions(-) --- diff --git a/database/mysql/cart_products.sql b/database/mysql/cart_products.sql new file mode 100644 index 0000000..835fb83 --- /dev/null +++ b/database/mysql/cart_products.sql @@ -0,0 +1,7 @@ +CREATE TABLE cart_products ( + cart integer NOT NULL, + sku varchar(32) NOT NULL, + position integer NOT NULL, + quantity integer NOT NULL DEFAULT 1 +); +CREATE UNIQUE INDEX cart_products_idx ON cart_products (cart, sku); diff --git a/database/mysql/carts.sql b/database/mysql/carts.sql new file mode 100644 index 0000000..e05cd9b --- /dev/null +++ b/database/mysql/carts.sql @@ -0,0 +1,10 @@ +CREATE TABLE carts ( + code serial PRIMARY KEY, + name varchar(255) NOT NULL default '', + type varchar(32) NOT NULL default '', + status varchar(32) NOT NULL default '', + uid varchar(255) NOT NULL, + session_id varchar(32) NOT NULL, + created integer NOT NULL, + last_modified integer NOT NULL default 0 +); diff --git a/lib/WellWell/DatabaseCart.pm b/lib/WellWell/DatabaseCart.pm index ac42335..4a1432a 100644 --- a/lib/WellWell/DatabaseCart.pm +++ b/lib/WellWell/DatabaseCart.pm @@ -42,7 +42,8 @@ sub cart_hook { $cartname = $Vend::Cfg->{DatabaseCart}; } - $cart = get_cart_by_name($cartname, 'cart', $Vend::Session->{username}, 1); + my $uid = $Vend::Session->{username} || $Vend::Session->{id}; + $cart = get_cart_by_name($cartname, 'cart', $uid, 1); if ($op eq 'add') { $cart->add_item($item); @@ -59,7 +60,8 @@ sub cart_hook { sub cart_restore { my $cart; - $cart = get_cart_by_name($Vend::Cfg->{DatabaseCart}, 'cart', $Vend::Session->{username}); + my $uid = $Vend::Session->{username} || $Vend::Session->{id}; + $cart = get_cart_by_name($Vend::Cfg->{DatabaseCart}, 'cart', $uid); if ($cart) { $cart->restore(); @@ -70,7 +72,8 @@ sub cart_restore { sub cart_clear { my $cart; - $cart = get_cart_by_name($Vend::Cfg->{DatabaseCart}, 'cart', $Vend::Session->{username}); + my $uid = $Vend::Session->{username} || $Vend::Session->{id}; + $cart = get_cart_by_name($Vend::Cfg->{DatabaseCart}, 'cart', $uid); if ($cart) { $cart->clear(); @@ -80,8 +83,9 @@ sub cart_clear { # Comparing database cart with session cart sub cart_compare { my ($cart, $cart_products, $session_products, $cart_item, $session_item, $max_count, @diff); - - $cart = get_cart_by_name($Vend::Cfg->{DatabaseCart}, 'cart', $Vend::Session->{username}); + + my $uid = $Vend::Session->{username} || $Vend::Session->{id}; + $cart = get_cart_by_name($Vend::Cfg->{DatabaseCart}, 'cart', $uid); if ($cart) { $cart_products = $cart->item_list(); @@ -142,6 +146,7 @@ sub get_cart_by_name { $code = $db_carts->set_slice($code, uid => $uid, created => Vend::Tags->time({format => '%s'}), + session_id => $Vend::Session->{id}, type => $type, name => $name); } @@ -222,6 +227,13 @@ sub restore { # empty session cart first @$Vend::Items = (); + # take over the cart, if it exits with the same session + if (my $sid = $Vend::Session->{id}) { + $set = $self->{db_carts}->query(qq{select code from carts where session_id = '%s' and uid = session_id}, $sid); + for my $cart (@$set) { + $self->{db_carts}->query(qq{update cart_products set cart = $self->{code} where cart = $cart->[0]}); + } + } $set = $self->{db_carts}->query(qq{select sku,quantity from cart_products where cart = $self->{code}}); @@ -230,6 +242,8 @@ sub restore { push (@$Vend::Items, $item); } + # update the session id of the cart + $self->{db_carts}->set_field($self->{code}, session_id => $Vend::Session->{id}); return; }