[ic] recalculate not collapsing items with matrix options

Bob Ramstad interchange-users@interchange.redhat.com
Thu Apr 4 14:31:01 2002


Howdy.  I've tried a number of things, and I'm stuck again.  The
archives have some useful information, but not enough to solve the
problem.

I have a number of items that have matrix options.  In each case,
there's just a single attribute which determines which SKU is actually
being ordered.  A common one is flavor.

I have SeparateItems set to No.

Suppose I have quantity 1 of item A in flavor X in my shopping cart.

If I put another one of these in my shopping cart by going to the item
A page, selecting flavor X and buying it, everything is fine.  The
cart shows quantity 2 of item A in flavor X.

Now, for giggles, I went to the item A page, selected flavor Y, and
bought it.  The cart now shows two lines, item A flavor X qty 2 and
item A flavor Y qty 1.  Groovy.

Here's the weird part.  If I change the flavor from Y to X (or X to Y,
doesn't matter) and hit recalculate, even though the options are all
the same, the two lines are not collapsed into one line with quantity
3.

I'm puzzled, as it seems that recalculate does not run the same
algorithms to clean up the shopping cart as buying an item would.

Originally I had been using simple options, as these are adequate for
what I'm trying to do, but I ran into some serious problems with
SeparateItems set to No (as mentioned in the archives of this mailing
list).  I tried setting it to Yes and calling [recombine] at the start
of basket.html, but this still gave the same behavior i.e. new items
added to the cart would get munged in with the matching old item, but
recalculate wouldn't collapse items.

UserTag recombine Routine <<EOR
sub {
my $cart = $Vend::Items;
# Combine all similar products if attributes match
for (my $x=0;$x < @$cart;$x++) {
   REMAIN: for (my $y=$x+1;$y < @$cart;$y++) {
     next REMAIN unless ($$cart[$x]{code} eq $$cart[$y]{code});
     foreach (keys %{ $$cart[$y] }) {
       next REMAIN unless ($_ =~ /quantity|mv_price/i || $$cart[$x]{$_} eq 
$$cart[$y]{$_});
     }
     # we have checked all attributes, so combine items
     $$cart[$x]{quantity} += $$cart[$y]{quantity};
     splice(@$cart, $y, 1);
   }
}
return '';
}
EOR

It seems to me that the most elegant solution might be to modify the
cart component and allow passing of another undocumented parameter
(like continue_shopping) which munges any duplicate items together --
there's an obvious place to do this right before the items are
displayed.  I don't like the [recombine] approach too much because A)
the cart component is called from a lot of places and B) it doesn't
work for recalculations.

Anyone out there with a simple clean solution to the problem?

-- Bob