Mailing List Archive

[wellwell/interchange6: 3/5] First revision (still experimental) to manage options in cart
commit 09f3b4d464708cdffd69e23a313882c51f3572a7
Author: Marco Pessotto <melmothx@gmail.com>
Date: Fri Nov 22 16:55:30 2013 +0100

First revision (still experimental) to manage options in cart

Without this patch, the DatabaseCart is not able to detect items with
different attributes but same skus.

database/mysql/cart_products.sql | 3 +-
lib/WellWell/DatabaseCart.pm | 69 ++++++++++++++++++++++++++++++++++----
2 files changed, 64 insertions(+), 8 deletions(-)
---
diff --git a/database/mysql/cart_products.sql b/database/mysql/cart_products.sql
index 835fb83..133a8e8 100644
--- a/database/mysql/cart_products.sql
+++ b/database/mysql/cart_products.sql
@@ -2,6 +2,7 @@ CREATE TABLE cart_products (
cart integer NOT NULL,
sku varchar(32) NOT NULL,
position integer NOT NULL,
+ options varchar(500) NOT NULL,
quantity integer NOT NULL DEFAULT 1
);
-CREATE UNIQUE INDEX cart_products_idx ON cart_products (cart, sku);
+CREATE UNIQUE INDEX cart_products_idx ON cart_products (cart, sku, options);
diff --git a/lib/WellWell/DatabaseCart.pm b/lib/WellWell/DatabaseCart.pm
index 4a1432a..26b3776 100644
--- a/lib/WellWell/DatabaseCart.pm
+++ b/lib/WellWell/DatabaseCart.pm
@@ -178,7 +178,9 @@ sub add_item {
my ($self, $item) = @_;

unless (exists $item->{inactive} && $item->{inactive}) {
- $self->{db_products}->set_slice([$self->{code}, $item->{code}], quantity => $item->{quantity},
+ my $opts = item_options_string($item);
+ $self->{db_products}->set_slice([$self->{code}, $item->{code}, $opts],
+ quantity => $item->{quantity},
position => 0);
$self->touch();
}
@@ -186,19 +188,36 @@ sub add_item {

sub delete_item {
my ($self, $item) = @_;
-
- $self->{db_products}->delete_record([$self->{code}, $item->{code}]);
+ my $opts = item_options_string($item);
+ $self->{db_products}->delete_record([$self->{code}, $item->{code}, $opts]);

$self->touch();
}

sub modify_item {
my ($self, $item, $changes) = @_;
+ my $opts_str = item_options_string($item);
+
+ # do a copy to avoid disasters
+ my $item_copy = { %$item };
+
+ foreach my $k (keys %$item_copy) {
+ if (exists $changes->{$k}) {
+ $item_copy->{$k} = $changes->{$k};
+ }
+ }
+ my $new_opts_string = item_options_string($item_copy);

if ($changes->{quantity}) {
- $self->{db_products}->set_slice([$self->{code}, $item->{code}], quantity => $changes->{quantity});
+ $self->{db_products}->set_slice([$self->{code}, $item->{code}, $opts_str],
+ quantity => $changes->{quantity});
}

+ if ($new_opts_string ne $opts_str) {
+ $self->{db_products}->set_slice([$self->{code}, $item->{code}, $opts_str],
+ options => $new_opts_string);
+ }
+
$self->touch();
}

@@ -235,10 +254,17 @@ sub restore {
}
}

- $set = $self->{db_carts}->query(qq{select sku,quantity from cart_products where cart = $self->{code}});
-
+ $set = $self->{db_carts}->query(qq{select sku,quantity,options from cart_products where cart = $self->{code}});
for (@$set) {
- $item = WellWell::Cart::cart_item(@$_);
+ my ($sku, $quantity, $opts) = @$_;
+ my @args;
+ if ($opts) {
+ @args = ($sku, $quantity, item_options_string_to_hashref($opts));
+ }
+ else {
+ @args = ($sku, $quantity);
+ }
+ $item = WellWell::Cart::cart_item(@args);
push (@$Vend::Items, $item);
}

@@ -270,6 +296,35 @@ sub item_list {
return \@list;
}

+sub item_options_string {
+ my $itemref = shift;
+ my $modifiers = "";
+ my @mods;
+ if (ref($Vend::Cfg->{UseModifier}) eq 'ARRAY') {
+ for (@{$Vend::Cfg->{UseModifier}}) {
+ if (exists $itemref->{$_} and defined $itemref->{$_}) {
+ push @mods, $_ . "\0:\0" . $itemref->{$_};
+ }
+ }
+ }
+ if (@mods) {
+ $modifiers = join("\0;\0", @mods);
+ }
+ return $modifiers;
+}
+
+sub item_options_string_to_hashref {
+ my $item_string = shift;
+ return unless $item_string;
+ my @fields = split(/\0;\0/, $item_string);
+ my %out;
+ foreach my $f (@fields) {
+ my ($k, $v) = split(/\0:\0/, $f);
+ $out{$k} = $v;
+ }
+ return \%out;
+}
+
package Vend::Config;

sub parse_database_cart {

_______________________________________________
interchange-cvs mailing list
interchange-cvs@icdevgroup.org
http://www.icdevgroup.org/mailman/listinfo/interchange-cvs