This one is a bit difficult to explain, so I will provide an example scenario:

I have created a product type with the the following fields: Product SKU, Title, Price, Status, and Number of Shipments.

The Number of Shipments field is a list box with the following options: 1 month, 3 months, 6 months, or 12 months.

I then create my products based upon this type:

  • Oreo Cookies - 1 shipment
  • Oreo Cookies - 3 shipments
  • Oreo Cookies - 6 shipments
  • Oreo Cookies - 12 shipments
  • Ginger Snaps - 1 shipment
  • Ginger Snaps - 3 shipments
  • Ginger Snaps - 6 shipments
  • Ginger Snaps - 12 shipments
  • ...

Next, I create a product page which references all of the cookies, except for the single shipments. Everything works just fine: select a # of shipments, select a product, and add to cart. No problems.

I create a second product page for one-time shipments, and here is where I see a UI problem. Even though all products reference have the same shipment field setting("1 shipment"), a number of shipments selection prompt appears with only that option available.

If a product display determines that only a single option is available for multiple product references, then the option should not be displayed as a select list box.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

c600g’s picture

The function which I believe will need to be modified is function commerce_cart_add_to_cart_form().

Perhaps the entire <div class="attribute-widgets"> could be hidden if the number of options is equal to one?

rszrama’s picture

Hmm, so your "single shipment" page is still letting users specify which cookie type they want, right? I think the problem isn't so much hiding single use options (b/c that actually should be happening) but having an actual option to turn a multi-value reference field into an attribute selection vs. title select list. In your case, it would work just fine if it just listed the product titles, right? The only reason it's showing the number of selection option is because of the magic attribute form conversion... and this magic form adjustment it's what's getting us in trouble in a few different places.

c600g’s picture

Correct - the "Single Shipment" page is correctly displaying the product title selection box containing { "Oreo", "Ginger Snap", "Chips-ahoy!", ...}. However, it is also displaying a "Number of shipments" selection box which holds only the single option of "1 shipment".

From an end user-interface point of view, if a product display selection box only offers a single selection, then it doesn't make sense (to me) to offer it as a form input.

I realize that I could create a completely new product type specifically for single shipment "cookies", but that offends my OOP sensibilities... ;>

c600g’s picture

I may have implemented it with the following code from function commerce_cart_add_to_cart_form()

          // If for some reason no options for this field are used, remove it
          // from the qualifying fields array.
          if (empty($used_options[$field_name])) {
            unset($qualifying_fields[$field_name]);
          }
          else {
            $form['attributes'][$field_name] = array(
              '#type' => 'select',
              '#title' => check_plain($data['instance']['label']),
              '#options' => array_intersect_key($data['options'], drupal_map_assoc($used_options[$field_name])),
              '#default_value' => $default_product_wrapper->{$field_name}->value(),
              '#weight' => $data['instance']['widget']['weight'],
              '#ajax' => array(
                'callback' => 'commerce_cart_add_to_cart_form_attributes_refresh',
              ),
            );
            // hide the selection list if there is only one option in the list
            if (count($form['attributes'][$field_name]['#options']) == 1) {
              $form['attributes'][$field_name]['#type'] = 'hidden';
            }
            $form['unchanged_attributes'][$field_name] = array(
              '#type' => 'value',
              '#value' => $default_product_wrapper->{$field_name}->value(),
            );
          }

The code I added was the 4 lines beginning with the comment "// hide the selection list if..."

AdamGerthel’s picture

Has this been commited? I'm also a bit surprised about this. I have a furniture store project, with a product type called 'lamp'. Some lamps have light source options of LED, Light bulb or halogen. This works well, when the Display node is referencing all kinds of light source options. However, if the display node is only referencing two 'lamp' products and both of them have 'LED', then the select list still appears - but only with one option available.

AdamGerthel’s picture

Version: 7.x-1.x-dev » 7.x-1.0-beta4

Moved to Beta 4 since the early comments were about Magic configuration which is no longer part of Commerce. The issue is still there though.

rszrama’s picture

I'm not sure what the best solution here is. It may end up requiring an option, because just as surely as you'd like to hide the option, other people will want it to be always visible. The main problem as I see it is that we don't know at core whether in the context of the product display there will be any way for the customer to know the contents of that field or not. Maybe it shouldn't matter, but maybe there's only one option showing because the other three are temporarily out of stock. : ?

One thing you can do is alter the Add to Cart form and change that single option select list into a hidden form element. Everything should still function properly if you do that.

AdamGerthel’s picture

How/where would you alter the Add to cart form? As a theme override?

AdamGerthel’s picture

FileSize
129.66 KB

I couldn't edit my comment and add a screenshot, so here it is.

I did a screenshot to make sure we're talking about the same thing. This site is still a work in progress, so the styling might look odd (things overlapping each other) but I think you can still make out what I mean.

davidwhthomas’s picture

Getting a similar problem here using RC1

Product reference field selects multiple related products and the attribute select boxes show up.

However, some of the select boxes contain only one value.
Also, one of the select boxes is just the default 'None' with no options to select.

It would perhaps be better, if the select box has only one value, show it as just text.
Also, to remove select boxes that have no value at all would be much more preferable.

Is there a reason for this form configuration?

DT

P.S Awesome module btw, generally working really well.

jakonore’s picture

I think this problem should be handled by an option on the field setting such as "Hide if only one value is available".

It should appear bellow "Enable this field to function as an attribute field on Add to Cart forms."

Unfortunately I can't code.

davidwhthomas’s picture

Version: 7.x-1.0-beta4 » 7.x-1.0
FileSize
17.24 KB

I've just upgraded to 7.x-1.0 and unfortunately this issue with empty attribute select boxes showing persists.

I know there was work done to allow radio and select box options for the add to cart form.
It would be great if select fields could also be hidden if they have no options.

See attached screenshot.

In this particular case, none of the referenced products for that display node had values for weekdays and copies, yet the select boxes showed empty anyway.

jbova’s picture

I was about to file an issue for this, when I saw this post. I have attached a diff for version 7.x-1.0 to fix this. We need to check to see if any of the available products have this option set to determine if we should show the form for this field. Please try the patch and provide feedback.

--- commerce.a/modules/cart/commerce_cart.module  2011-08-23 14:30:31.000000000 -0400
+++ commerce.b/modules/cart/commerce_cart.module  2011-09-13 03:04:03.000000000 -0400
@@ -1544,6 +1544,17 @@
         // Sort the fields by weight.
         uasort($qualifying_fields, 'drupal_sort_weight');

+        /* Loop through fields and products to make sure at least one product has the field set. */
+        $field_has_products = array();
+        foreach ($qualifying_fields as $field_name => $data) {
+          // Build an options array of widget options used by referenced products.
+          foreach ($products as $product_id => $product) {
+            if ( $product_wrapper->{$field_name}->raw() ) {
+              $field_has_products[$field_name] = 1;
+              break;
+            }
+          }
+        }
         foreach ($qualifying_fields as $field_name => $data) {
           // Build an options array of widget options used by referenced products.
           foreach ($products as $product_id => $product) {
@@ -1564,7 +1575,9 @@

             // With our hard dependency on widgets provided by the Options
             // module, we can make assumptions about where the data is stored.
-            $used_options[$field_name][] = $product_wrapper->{$field_name}->raw();
+             // module, we can make assumptions about where the data is stored.
+            if ( isset($field_has_products[$field_name])) {
+              $used_options[$field_name][] = $product_wrapper->{$field_name}->raw();
+            }
           }

           // If for some reason no options for this field are used, remove it

jakonore’s picture

Commerce Cart Option seems to take care of the problem. See the description of the module.
http://drupal.org/sandbox/ryan.armstrong/1146154
Haven't tested yet. Will come back with feedback.

jbova’s picture

@jakonore, You could try the patch and report back, since the sandbox project you referenced hasn't been updated since May 3rd.

davidwhthomas’s picture

The above patch in #13 solved the issue for me.

Though I'm thinking perhaps a hook to allow other modules to adjust the fields would be preferable, does such a hook already exist?

DT

jbova’s picture

DT,

The patch is intended to eliminate fields that have no applicable products. These prevents the select field containing "None" from being displayed. This should really be the default behavior.

However, choosing whether or not to display fields which contain only one value is a different story. This is user preference. As far as I know, there is no hook for this. The patch in #13 should not affect this behavior at all.

Jim

jbova’s picture

Title: Product field options appear when not necessary » Product attribute fields appear on add to cart form as "none", when no product references contain said attributes
Version: 7.x-1.0 » 7.x-1.2
Category: feature » bug

I'm bumping this issue, which still exists in 7.x-1.2. I've been applying the same patch proposed in #1118498-13: Product attribute fields appear on add to cart form as "none", when no product references contain said attributes with each new version to fix the problem. Can we get this patch applied, or at least something else to correct the issue? Status changed to bug report, since this is more of a user interface bug than a feature request.

rszrama’s picture

Status: Active » Needs review

Sure, this just needed to be set to "needs review." And just to be clear, the issue here is if you're building an Add to Cart form for products that a non-required field when none of the referenced products have a value for a given field? Just asking so we can get a simple reproduction of the issue to test.

davidwhthomas’s picture

Hi rszrama,

And just to be clear, the issue here is if you're building an Add to Cart form for products that a non-required field when none of the referenced products have a value for a given field

Yes, that was the exact issue for me / our project.

The add to cart form would show attribute select boxes where none of the referenced products had options for those attributes/select boxes.

Hoping we can get this fix committed to core commerce at some point!

cheers,

David

P.S Commerce rocks, thanks so much for your hard work on this excellent project.

amateescu’s picture

Version: 7.x-1.2 » 7.x-1.x-dev
Assigned: Unassigned » amateescu
Issue tags: +1.3 review
FileSize
1.35 KB

The idea from #13 is good, but I think it can be done in a cleaner way.

helior’s picture

Assigned: amateescu » Unassigned
FileSize
1.36 KB

This broke on fields which contained "0" as the raw value, which is still a valid value. Checking the returned raw value against NULL in the updated patch.

rszrama’s picture

Status: Needs review » Fixed

Great, thanks for the patches and the review. I've committed #22 for the 1.3 release.

p55mac’s picture

Version: 7.x-1.x-dev » 7.x-1.3

I just upgraded to the 1.3 release and although empty attributes fields are not displayed on the add doc art form I am getting the following error...

Notice: Undefined index: field_size in commerce_cart_add_to_cart_form() (line 1701 of /profiles/commerce_kickstart/modules/commerce/modules/cart/commerce_cart.module).

* Edit tested a clean install of Commerce Kickstart 1.17 and Drupal commerce 1.3 and had the same error.

However, I was using taxonomies terms for attributes options. When I used a list (text) field for the attributes I did not get the error.

Anyway can I use taxonomies as a product attribute without this error?

FrancescoUK’s picture

Same here after upgrading to 1.3, I'm getting the log full of:

Notice: Undefined index: field_size in commerce_cart_add_to_cart_form() (line 1701 of /<...>/sites/all/modules/commerce/modules/cart/commerce_cart.module).

And I'm using taxonomy terms for attributes.

Any fix?

rszrama’s picture

Version: 7.x-1.3 » 7.x-1.x-dev
Status: Fixed » Needs review

Sounds like taxonomy term reference fields may be functioning differently when empty from other fields. Let's get someone to test and confirm this is reproducible.

sreher’s picture

I update to the dev Version but i continue to get the error message.

I add a fix for me - i don't know really what effects it has. Helps for the moment.
Can i do it so?

Status: Needs review » Needs work

The last submitted patch, empty-attributes-1118498-27.patch, failed testing.

RowboTony’s picture

Hi, I wanted to comment with my experience as I just encountered a related issue with my site. In short - I have product attributes that are taxonomy terms, not all of the attributes are required and not every product will have said attribute. Example attribute "chocolate type": milk, dark, white. Some items come in milk, dark, and white, other items come only in milk chocolate. So items with multiple attributes show the drop-down or radio properly, but items that are only milk (and empty as attributes are only set on variant items, meaning if it's ONLY milk then the field is not set) so the field comes out as 0 on my form.

Further, this only happens with combined product displays. Example: I have 1/2lb and 1lb attribute for a product that only comes in milk chocolate, the select list will show two empty <options> in my list (see attached). I've found that if I alter the form I can fix my site for now, but it would be great to have the attribute drop-down not show if it's empty (or zero). I think my difficulties relate to this being a taxonomy based attribute. Anyhow, here's what I put in my theme template.php to fix this issue on my site for now, and a screenshot for clarity.

function MYTHEME_form_commerce_cart_add_to_cart_form_alter(&$form, &$form_state, $form_id) {
   if (empty($form['attributes']['field_chocolate_type']['#options'])) {
      hide($form['attributes']['field_chocolate_type']);
   }     
}

infines’s picture

Status: Needs work » Needs review
Issue tags: -1.3 review

#27: empty-attributes-1118498-27.patch queued for re-testing.

Status: Needs review » Needs work
Issue tags: +1.3 review

The last submitted patch, empty-attributes-1118498-27.patch, failed testing.

iqxmk’s picture

Version: 7.x-1.x-dev » 7.x-1.7
Component: User experience » Developer experience
Status: Needs work » Needs review

Hi There,

I still have the problem of empty attribute fields showing in the add to card form, when using multiple term reference fields for product variations, and some of them don't contain any terms for certain product variations.

I'm using commerce 1.7 (from commerce kickstart 2.8).

The following lines from commerce_cart.module should work fine, but I don't know why the empty attribute dropdown is still showing:

          // If for some reason no options for this field are used, remove it
          // from the qualifying fields array.
          
		  if (empty($field_has_options[$field_name]) || empty($used_options[$field_name])) {
            unset($qualifying_fields[$field_name]);
		  }

Any ideas?

Regards,
Martin

rszrama’s picture

Version: 7.x-1.7 » 7.x-1.x-dev
Status: Needs review » Closed (fixed)

To the folks continuing to report issues here, I'd like to request we start a new support request to handle it. We no longer have the original issue as far as I can tell. I could not create a scenario using List (text) fields or Term reference fields just now that created a "none" attribute widget. Additionally, I'm not successfully generating any notices / error messages or able to reproduce the issue pictured in the screen shots.

If you're still experiencing trouble on 7.x-1.x-dev, please open a new support request with a detailed explanation that would allow me to reproduce the issue locally. I need to know what product type you're using, specifically what attribute fields it has with what values. I'll then need to know how you've created products for this type and how they're related through a display node along with what your Add to Cart form display formatter settings are. Thanks!