Gossamer Forum
Home : Products : DBMan : Customization :

shopping cart mod question

Quote Reply
shopping cart mod question
I'm fiddling with o grain's shopping cart mod. As far as I understand it, it only allows users to select quantities for ONE item. Has anyone modified it so that it is possible to simultaneously select quantities for MORE items?




kellner
Quote Reply
Re: shopping cart mod question In reply to
It's been awhile since I've looked at that mod, but I remember testing it. I believe you can order one item and then from there you can choose another item and continue ordering and adding items.

Have you installed the mod or tested it?

Unoffical DBMan FAQ
http://webmagic.hypermart.net/dbman/
Quote Reply
Re: shopping cart mod question In reply to
Yes, that's true. I have tested the mod up to the stage where orders can be added. What I want to do however is have a list of search results and the possibility to add more than one item to the shopping cart. So I'm asking whether anyone has modified it to this effect.

I think the trick lies in the value "antal", which is used to pass on the quantity to the temporary file that stores the order - one would have to code a variable instead, based on the database key, for the quantity for each database entry. But before I devote more time to this, I'm asking whether someone else has already done it.

kellner
Quote Reply
Re: shopping cart mod question In reply to
Hi kellner,

I have problems with my Internet connection, but as soon as I get my private connection back I'll change the text in the demo to English (now that it seems that more people are interested in it).

Yes, "Antal" is "Quantity".

I'm not sure that I understand what you mean with

In Reply To:
the possibility to add more than one item
You can specify the quantity 5 for item xxxxx, and then (after another search) quantity 2 for item yyyyy, but that is not what you mean, I take it?


Quote Reply
Re: shopping cart mod question In reply to
Hi o grain, glad to have the Master of This Mod Himself (or Herself) reply. Smile

You get a list of search results like this:

??? Apples
??? Peaches
??? Oranges

The Question-marks stand for the quantity input field. I would like to be able to input quantities for ALL three items on this list and add them to the shopping-cart in one go, without having to do a search for Apples, then another one for Peaches, and another one for Oranges.

kellner
Quote Reply
Re: shopping cart mod question In reply to
Hi kellner,

So you mean like you search for "fruit" and get a list of fruits, and there is a small field for quantity at each row. And you can enter quantities for apples, peaches and oranges in one go and press submit and you'll have in the order basket

10 Apples
12 Peaches
2 Oranges

Nice idea, but I have not implemented this. Sounds like a challenge, but I'm afraid I can't spend very much time on web work the nearest month. I'm too busy making and flying kites Smile. If you decide to go ahead yourself I will of course try to give you support with unclear things in the mod description (or the mod as such).

o grain
(himself Wink)
http://tangokites.org/...okites/html/vem.html


Quote Reply
Re: shopping cart mod question In reply to
OK, o grain or whoever is willing to assist my poor programming skills in this task, here's what I've come up with so far:

sub html_view_success returns a list of results. the results are printed as a form with a text input field for adding order quantities. the user enters a quantity for each result and presses a submit button. dbman then parses the form input and, for each result plus entered quantity, adds the info to a temporary file which has the user's e-mail name as its title, and which already exists.

my problem is that I don't know how to code the green part. right now, my form passes on, for each record, "dbkey", which has $db_key as its value, and "antal", which has the entered quantity as its value. This information then goes to a sub called "add_to_order" which prints the values into the temporary order file.
However, I don't know how to have dbman associate each record with the appropriate quantity figure. Right now, the strings passed on to "dbkey" and "antal" look like "1~~14~~25" and "2~~5~~1" respectively. But what to do next, what to do next ...



kellner
Quote Reply
Re: shopping cart mod question In reply to
Hi kellner,

I'm afraid this is beyond me. I have no idea how to do it. I hope some of the DBman wizards can help you with this. It would be interesting to see the result, though.

Good luck!

O grain

Quote Reply
Re: shopping cart mod question In reply to
thanks anyway. I am now up to the point where I think I have to work with a multidimensional array:
The form passes on a string called "antal" which looks like "1~~5~~8", and another called "dbkey" which looks like "15~~26~~29". "antal" contains the order quantities, and "dbkey" the database keys. It is easy to split these strings and store the values in arrays @antal and @dbkey.

What I'm more doubtful about is what I think I have to do next:
- declare a multidimensional array @matrix ( \@antal, \@dbkey).
- foreach member of @matrix: print the value of $antal, get the record based on $dbkey (i.e. sub get_record($dbkey)), and print certain values from the record (like author's name, title of book, price).

I have never before worked with multidimensional arrays though, so I am not sure whether this is the way to go at all. Nor do I know how to write code in order to access individual members of the arrays that the multidimensional array refers to.

Any help from anyone will be GREATLY appreciated,

kellner
Quote Reply
Re: shopping cart mod question In reply to
Well, here I am replying to myself, but just in case anyone's interested - I found out how to do it. The key is to use perl's capability to pass on multiple arrays to a subroutine. The following presupposes o grain's shopping cart mod and is a small modification of it.

Imagine that dbman outputs a list of search results, which are printed using sub html_record. html_record is coded as a form which allows users to add quantities of items that they want to order. Into the actual order form, you want to have printed the quantity of items, the ordered items (value of $rec{'item'}), and the price of one item (value of $rec{'price'}).

In the beginning of html_record, add the following code:
$order = "$rec{'item'}, price per item: $rec{'price'}.";

The form in html_record must contain these two input fields:
<input type=text name="quantity" value="1">
<input type=hidden name="order" value="$order">


In the beginning of sub html_add_order in html.pl, add the following code:
@quantity = split(/~~/, $in{'quantity'});
@order = split(/~~/, $in{'order'});

The form will give you the quantities for each ordered record in a string like "1~~5~~6", and do the same for the order data. This code produces two arrays and removes the tildes in between the individual elements.

In the same sub, there's an "else" loop which calls sub add_to_order. The sub must be called with &add_to_order (\@quantity, \@order);.

In db.cgi, there's the sub add_to_order. It contains a lot of extra code like checking for duplicates that I'm not concerned with here. For our purpose, the essential part is the code that opens the temporary order file and appends the order data to it. This code should look like follows:

sub add_to_order {
open (FILE, ">>$orders_dir/$in{'order_mail'}.txt") || &fatal_error("Can't open $orders_dir/$in{'order_mail'}.txt.");
$i = 0;
my ($a, $b,) = @_;
foreach (@$a) {
print FILE "\*";
print FILE @$a[$i];
print FILE qq| pcs.: |;
print FILE @$b[$i];
print FILE "\n";
$i++;
}
close (FILE);
}


More modifications are necessary, but this is the basic principle which allows users to add orders of more than one item in a list of search results. Check out the following URL if you're interested in the perl basics behind it:
http://www.gamehendge.org/Marvin/Perl/Teach_Yourself_Perl_in_21_Days/ch18.htm#UsingSubroutinestoWorkwithMultipleArrays

cheers,

kellner
Quote Reply
Thanks for sharing the solution! In reply to
Hi kellner,

Nice work, kellner! I reckoned that a loop indexing through the arrays would do the trick, once you had the arrays. The difficult part seemed to be to create and pass the arrays. Glad you made it! The URL you gave will probably come in handy from time to time.

I'll try to implement this add-on as an option in the mod, if I may.

O grain




Quote Reply
Re: Thanks for sharing the solution! In reply to
of course you may - saves me the time to write a mod of your mod! Smile

kellner