Crosspost from Ubercart.org

I really dislike the fact that Ubercart is unable to handle usefull html form types like checkboxes and multiple select forms.

To help find a solution, I wrote some code/patch to implement this functionality. It's far from finished or usable and written for Ubercart 1.x, but the functionality should be implementable for Ubercart 2 easily.
You can see the code on the product page+cart at work in the link below.

http://www.dinilu.cn/node/11

(ps for maintainers, Ubercart now only accepts one option per attribute. Luckily, that one option is a string so my proposed code just puts a comma (could be anything) between the selected options. Might not be the best solution, but it works and is relatively easy to implement.)

http://www.ubercart.org/forum/ideas_and_suggestions/9098/checkboxes_attr...

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

mariusilie’s picture

Version: 6.x-2.x-dev » 6.x-2.0-rc3
FileSize
6.14 KB

I made some changes to "uc_attribute.module" file and now it's possible to have checkboxes for attributes.

I made only minor changes and I would like to see some reviews on this, because It seems to work just fine and I don't know why nobody did this before.

Island Usurper’s picture

Status: Active » Needs review
FileSize
4.39 KB

I'm just amazed that it's actually that easy. I'd been digging my heels against this because I thought there would have to be major database changes to accommodate the possibility of multiple options.

I've cleaned up the modified file a little bit and turned it into a patch. I changed the way the list of options are displayed on the cart page, and fixed it so that they're all listed on the order pages.

Thank you very much for this.

Island Usurper’s picture

FileSize
5.69 KB

Small change to prevent checkbox attributes from showing up on the Adjustments tab. I shudder at the thought of having a different model number for each possible combination of checkboxes.

Adding or removing attributes will still reset the adjustments page, even if they don't actually affect the number of rows that are needed. I thought about changing it, but I think it's too much effort for little gain.

splash112’s picture

Great, many thanks for trying to add this functionality. Ubercool!

andreiashu’s picture

Thanks ! Just needed this one.
Is this going to be into the next release of Ubercart ?

andreiashu’s picture

Just applied patch from #3 and is working really nice. I'll post back here if I find any sort of problems.

mariusilie’s picture

I'm glad I can help.

It would be really nice if this would be included the next release (rc4) of ubercart.

andreiashu’s picture

Status: Needs review » Needs work

I might have found a problem: if call uc_cart_get_contents() in a hook_form_alter checkbox attributes aren't appearing (all others appear).

Island Usurper’s picture

I don't really see how that could happen, but I'm not sure what you're trying to do either. Where are the checkbox attributes not appearing, on the product page or the cart page afterwards?

andreiashu’s picture

The checkboxes appear on the product page, but on the same product page you cannot find that attribute in the array attributes returned by uc_cart_get_contents

andreiashu’s picture

Status: Needs work » Needs review

I cannot replicate this behavior anymore. When I reported it the website had 50+ contrib modules. Maybe some of them just mangled that array.
Sorry for the false report. I don't want to keep this from being added to HEAD.

Agileware’s picture

Testing this feature also (& subscribing)

xibun’s picture

+1 (thanks - and yes, would be nice to see this in rc4)

Island Usurper’s picture

Status: Needs review » Fixed

Thanks for reviewing, everyone. Committed.

mariusilie’s picture

so this patch would be included in ubercart RC4?

noslokire’s picture

Testing, so far so good! Nice work...

andreiashu’s picture

Status: Fixed » Needs work

I wasn't sure if I should open a new issue or just reopen this one...
Anyway, this is not finished yet: it won't work for manual adding orders on 'admin/store/orders/62/edit' for example. The selected product will be added to the order with all its options as checked!

This is because we forgot to update uc_order.js to take care of unchecked checkboxes.
On line 311 of uc_order.js, instead of $('#uc-order-add-product-form :input').not(':radio:not(:checked)').each( I just replaced/added: $('#uc-order-add-product-form :input').not(':radio:not(:checked)').not(':checkbox:not(:checked)').each(.

I reckon that instead of not(':radio:not(:checked)').not(':checkbox:not(:checked)') it would have worked with not(':radio:not(:checked), :checkbox:not(:checked)'). Is that right ?

@Island: as of #15 in this issue: "so this patch would be included in ubercart RC4?"

Cheers

Island Usurper’s picture

Status: Needs work » Fixed

Good catch. I used the not(':radio:not(:checked), :checkbox:not(:checked)') version, and it works great.

Basically, if a patch is committed before a release is made, then it's included in the release. RC4 doesn't exist yet, so both of these changes will be in it.

andreiashu’s picture

Great,
Thanks Island !

gurglemoo’s picture

How do I make a one-option checkbox remain unchecked by default?

Basically I have created a checkbox option to indicate whether the customer wants gift wrapping (either enabled or disabled), and whenever the page loads then this box is automatically checked which I do not want.

andreiashu’s picture

gurglemoo - you might want to use hook_form_alter for that.

gurglemoo’s picture

Thanks, I'm now attempting a custom module with the following:

function uncheckoption_form_alter($form_id, &$form) {
  if ($form_id == 'add_product_form') {
    $form['products']['attributes']['#default_value'] = FALSE;
  }
}

Obviously this doesn't work as I am quite new to overriding and hooks. I have trawled through hundreds of examples on the web and through the uc modules folders but I can't seem to figure out how to compose the $form statement to affect the checkboxes. :/

andreiashu’s picture

You will want to use the dpm function from Devel's module. If you don't have devel module installed I would suggest installing it and use that function this way:

// if your module's name is 'uncheckoption'
// this is the implementation of hook_form_alter for D6
function uncheckoption_form_alter(&$form, $form_state, $form_id) {
  // output the form id
  dpm($form_id);
  // then you continue form here
  if ($form_id == 'add_product_form') {
    
    // here you will have something like
    // $form['products']['attributes']['YOUR_ATTRIBUTE_ID']['#default_value'] = FALSE;
    
    // dpm will help you here
    // output the whole form nicely :)
    dpm($form);

    // this is probably incorrect
    // $form['products']['attributes']['#default_value'] = FALSE;
  }
}

Hope this helps. Remeber: enable devel module otherwise you will not have dpm function available ;)

mariusilie’s picture

or you can use 2 radio buttons, something like this:

Do you want your gift wrapped?
* no
* yes

gurglemoo’s picture

Status: Fixed » Active

After much head scratching I have come to the conclusion that trying to uncheck a single checkbox by default via any extra modules or hooks is unnecessary and there is an easier way.

What I did in the end was create a checkbox attribute called "Packaging" with 2 options - one option called "Free Gift Wrapping" and another temporary option called "Liquid Crystal Donkey". I then went to the product "node/edit/options" tab and assigned "Liquid Crystal Donkey" to be the default option. I applied the same setting to all my other products and once complete, I went back to the attributes config page and deleted the option "Liquid Crystal Donkey" - this causes the checkbox for "Free Gift Wrapping" to remain, but unchecked since there is no longer a default.

This leads me to request a simple amendment and that is for the 'requirement' for having a default option for checkboxes be removed?

Island Usurper’s picture

Status: Active » Fixed

I can't seem to get any checkbox attributes to be checked by default. Are you using the latest version of Ubercart?

If it's still a problem, I'd like you to make another issue since it's more of a bug report than a feature request at this point.

BDuell’s picture

Thanks for making this patch - it rocks!!!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

domineaux’s picture

I've posted on a very similar issue at UC forums couple times now. I just found this posting, which is very similar to what I need.

I need to apply local delivery charges based on the delivery location town or zipcode.

I would like to use the Attribute/Options to do this.

example... to Morristown, or Taaka,etc. A spin box instead of a selector drop down.

The drop down would take up entirely too much room when I have a large number of choices (up to 110 locations) for customer to select from.

Morristown delivery = $15.00
Taaka delivery = $12.00
Fredericks delivery =$8.00
Frankston delivery = $5.00
Millston delivery = $22.00
Evansburg =$44.00
etc..etc..

This way I could use alphabetized or numerical order of the options and change the delivery charges at will as gas prices go up and down.

Currently, a flat rate is being used, which is an average. It isn't fair to average the charges nor is it competitive in many instances.

blagrone’s picture

I'm running into a error due to the checkbox data being sent in a array instead of a string.
When using the patch, it generates an error message for every option checked on a checkbox:

On submit:
warning: Invalid argument supplied for foreach() in /home/deadwoodzombie/culturebank/includes/form.inc on line 1200.

And when I view the product:
* warning: preg_match() expects parameter 2 to be string, array given in /var/www/html/drupal/includes/bootstrap.inc on line 771.
* warning: preg_match() expects parameter 2 to be string, array given in /var/www/html/drupal/includes/bootstrap.inc on line 771.
* warning: preg_match() expects parameter 2 to be string, array given in /var/www/html/drupal/includes/bootstrap.inc on line 771.
that line is checking the string to be valid utf8 chars, but the data is sent as an array rather than a string.

My scenario is that i am using ubercart with node checkout and ads.
I'm not sure how to convert the array into a string at this point in the patch but will give it a shot.

I think that since the original code wants a string it would improve the functionality to send a string instead of an array.

IMHO - I'm not a programmer, so please forgive me if I don't know what I'm talking about...

*********
update
*********

The error I received is unrelated to checkboxes

mailfox’s picture

I used all that I have described above, all works but as soon as I found uc_ajax_cart I am having trouble adding attributes basket does not appear, it only appears after I hit the second add to cart the goods but with the added in duplicate ... how to fix?

wesjones’s picture

Hey blagrone,

I am receiving the same error messages when viewing the node. What did you figure out the cause was? I am also using node checkout and ubercart.

blagrone’s picture

so far I've been able to get it to work by editing the bootstrap file.
I found it was not the checkboxes patch, so I posted my edits on another more appropriate page.
http://drupal.org/node/225211#comment-1934784

******
Updated - **DO NOT HACK CORE** If you read the link - you'll see why

charlie-s’s picture

What's wrong with different stock level for every combo?

In a store that sells small and large, red and blue t-shirts they certainly want to manage stock for

small red
small blue
large red
lage blue

TR’s picture

@csdco: Please don't post the same question to multiple threads. Also, this issue is CLOSED, which means that very few people will ever see new comments attached to the thread. For general support questions, you should ask in the forums at ubercart.org. Otherwise, you should open a new issue.