Giftcard - user added price

Posts: 23
Joined: 01/14/2008

A site I recently built for a salon wants to sell giftcards. I set it up as one product with 3 text fields (to/from/message). I set the shipping cost to the fixed amount they told me to, and the default price when I created the product was $0. Obviously the user has to enter in the amount they want the card to be worth.

Initially I was thinking of trying to use the Ubercart Custom Prices module and maybe have a 4th text field called price where they enter in an amount. The custom field where you enter in the code would just need something like textfieldvalue + total = total. Problem #1: what synatx would I use for that? I remember I used to have documentation that listed usable terms, but I seem to have lost it. I don't see it conspicuously mentioned in the link above, nor in the zip file that I downloaded from there.

Also, if this doesn't work, can Ubercart be set up like this out-of-box? That is, no default price but rather the user can enter in?

If all else fails I'll probably set up the prices as options in a select dropdown. That's just tedious though, because I'd have to put in 500 options ($1-$500).

Posts: 23
Joined: 01/14/2008

Found a solution. I just used the combination of the product attribute token add-on and the custom price add-on. Worked like a charm. Just one small problem remains, which I'm hoping is simple due to it being a relatively common sense thing (which Drupal tends to be good at tackling with foresight). And that thing is field validation. Can you setup up field validation for an attribute on a product page? The field requirements and character limits are already being enforced. How would I go about adding more conditions, in this case making sure the field content is only numeric?

Posts: 5269
Joined: 08/07/2007
AdministratorHead Code Monkey - I eat bugs.

You can add validate handlers to forms using hook_form_alter(). I don't recall the exact notation for D5 atm, but you simply have to know the form ID of the form you want to alter and you can add a validate handler to it. In the validate handler, you can make whatever check on that field you need to. I'd cruise the docs on d.o or http://api.drupal.org a little bit for more info.

Posts: 23
Joined: 01/14/2008

If that's the only thing I can do then I guess I'll go that route. Unfortunately for me that seems to indicate I'll have to either make a small module or write a function somewhere in an existing file - both of which for me would be risky. My understanding of programming in general aside, I'm super noobish with PHP and I've never made a module before.

Posts: 5269
Joined: 08/07/2007
AdministratorHead Code Monkey - I eat bugs.

Ahh, I see. Yeah, there's a bit of a learning curve to get into Drupal development, especially if you don't have a general understanding of programming to work off of. (That was the only reason I was able to dive into it.) So, you can always try to pick it up or look at what other modules are doing and modify that. That's actually how I learned C back in the day... hacking old MUD code (if that means anything to you Eye-wink).

Posts: 23
Joined: 01/14/2008

I've been writing C++ for probably close to four years on/off now, and VB6 for probably eight. A little Java, even less VB.NET, some Perl, and of course tons of XHTML/CSS and my fair share of javascript. But when the web started shifting over to larger languages, the fate of the early ASP vs. PHP wars was uncertain. I foolishly assumed ASP would triumph and started using it. After only a year of that I basically stopped and focused more on my college studies and my jobs. So fast-forward to today; I've written less than 20 lines of PHP in my life probably, and I haven't touched ASP in years. But I can program pretty damn well. I just need a good starting point.

With that said, if I can burn through a whole VB book in a month, a PHP book and the Pro Drupal Development book shouldn't take me too long. As for the salon site, they want it launched and working by Friday. So for now that field isn't going to get validated. *lol*

Posts: 5269
Joined: 08/07/2007
AdministratorHead Code Monkey - I eat bugs.

lol Yeah, I'm sure you'll get up to speed on the Drupal dev pretty quick. fwiw, PHP is a lot like C, you can just make stupid mistakes a little easier. Smiling Pro Drupal Development (for D6) is probably a good investment... I learned just hacking away on modules and keeping drupal.org handy. Same way I learned PHP... php.net is actually an incredible resource.

Posts: 23
Joined: 01/14/2008

For the time being, I've devised a very makeshift solution that still presents one small problem.

I've used the Custom Price's PHP field on the product's page and I wrote the following function:

if (!is_numeric([Amount])) {
   form_set_error('', t('Error! Do not proceed! The amount you entered is not a valid numeric amount. Please
   delete Gift Card from cart and return to purchasing form.'));
}

It works by setting an error message on the Cart page. The amount is then $0, and the error appears in a red box atop the page. Problem: if you then try to empty the cart and go elsewhere, you can't. The cart page just keeps reloading with the error message and the same quantity.

Is there a way I can write the function, continuing to use it in the Custom Price's PHP field, such that it just reloads the product page with the error there - instead of going to the cart page at all?

Posts: 301
Joined: 11/19/2007
Bug FinderGetting busy with the Ubercode.

This isn't very well tested...but you might be able to use http://www.ubercart.org/contrib/5341

It will give you a checkbox next to your attribute that will let you specify that it must be numeric. Since you are already using 2 modules that I wrote that aren't very well tested, no harm in trying a third..eh?

Posts: 23
Joined: 01/14/2008

cYu wrote:
This isn't very well tested...but you might be able to use http://www.ubercart.org/contrib/5341

It will give you a checkbox next to your attribute that will let you specify that it must be numeric. Since you are already using 2 modules that I wrote that aren't very well tested, no harm in trying a third..eh?

Installed good. Checkbox showed up. Went to the product, added to cart using 'acb' in the field Amount. Went to cart no problem. Amount stayed $0, but amount reads 'abc.' So it works and it doesn't.

EDIT: When I enable the character limit it works. Thanks a million!!

Posts: 301
Joined: 11/19/2007
Bug FinderGetting busy with the Ubercode.

Ah...I got it. If you go back to that module and download what is there now that little bug should be fixed and you should be able to do numeric without the character limit. Enjoy.