On our shop, we have one product type with the 'Color' and 'Size' attributes. The product is displayed using a 'product display' node type that contains the 'add to cart' form. Now if I create one 'product display' node that references only one product, the 'add to cart' form doesn't show the Color and Size attributes. In our case, it would still be nice for the user to actually see the color and size attributes for the product he/she is buying. I've digged in the code and I've commented out this piece in commerce_cart.module line 812:

if (count($products) == 1) {
       $form['product_id'] = array(
         '#type' => 'hidden',
         '#value' => key($products),
       );
     }
     else {

The attribute drop-down menus now get displayed even if there's only one product.

Now, I am not sure why it was decided that the attributes are not displayed when there's only one referenced product. Maybe I missed the correct way to do this. What do you guys think? Of course it's not great to display a drop-down if there's only one possible option but the attribute shouldn't disappear since it constitutes an important piece of information about the product.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

rszrama’s picture

Component: Cart » User experience
Status: Active » Postponed (maintainer needs more info)
Issue tags: +dcsprint6

I didn't do this because I think visually we don't want to display a select box when there's only one option. I think the idea is simply that the product page for a single referenced product would adequately describe the product. In fact, since the attribute select list is populated using field data, you can simply have those fields be shown as visible on your node type... it will be kept current whether you reference one or multiple products based on the currently selected product.

Does this solve your problem?

We'll need to review this decision in June with Bojhan at the sprint, so I'm tagging it.

ReeceMarsland’s picture

I've just stumbled across this issue too. I have clothing products of various size and color and we're using the attributes as a way to display this on the product. Perhaps we could simply show the attributes as text in place of the select lists if we only have 1 product?

liupascal’s picture

How about an option in the field setting ?
Like the one that says "Enable this field to function as an attribute field on Add to Cart forms."

Maybe a radio button with

Display behavior for single value attribute
o Display as a select box
o Do not display

liupascal’s picture

Here is a patch for an optional display of the attribute widget, with a checkbox setting at the field level.

Example for the product entity from kickstart, in /admin/commerce/products/types/product/fields check "Display the widget even if there is only product being displayed."

The patch does the following
- adds this a little form element in the field form ui to store the field setting
- remove the if (count($products) == 0) { in commerce_cart_add_to_cart_form
- fix the comment of the following else { to match the new behavior
- add a condition on the attribute form type to have a "hidden" field
'#type' => $data['commerce_cart_settings']['single_product_display'] ? 'hidden' : $data['commerce_cart_settings']['attribute_widget'],
The patch looks huge but it's simply because a big chunk of the code was moved up.

I am not sure this is the perfect solution but at least it gives the option to have the attribute widget displayed or not.
I do think this is a necessary option. Even if one can show the field display itself to have the information on screen when the attribute is not shown, it forces to :
- either to have 2 times the value on the screen (from the attribute widget + the field value)
- or to have to customize the display through code (of panels etc...) to remove/display the field value if the attribute widget is show/hidden
And if we need to have the field value displayed in the same way as the attribute widget (most probably, as having 2 different page templates for the same content is definitely not the right way to do in ecommerce), it requires even more coding... (theming + form alters)

Please have a second thought :-)

liupascal’s picture

Status: Postponed (maintainer needs more info) » Needs review
FileSize
28.1 KB

Forgot to change the status !
Re-attach the patch to have it tested

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

The last submitted patch, display_or_hide_attribute_widget-1029002-5308124.patch, failed testing.

wajudai’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, display_or_hide_attribute_widget-1029002-5308124.patch, failed testing.

uditmahajan’s picture

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

commerce_visible_attributes.patch queued for re-testing.

Paul Lomax’s picture

Had this problem too, got round it with a fairly simple form alter, maybe this will help others:

/*
 * Replace the select boxes on add to cart forms with radio buttons 
 */
function mymodule_form_commerce_cart_add_to_cart_form_alter(&$form, &$form_state) {
  
  // Switch the select boxes over to use radio buttons
  if (!empty($form['product_id']['#options'])) {
    $form['product_id']['#type'] = 'radios';
  }
  
  // Replace the missing product selector with a dummy radio button
  if (count($form_state['context']['entity']->field_products[LANGUAGE_NONE]) == 1) {
    $form['fake_radio'] = array(
      '#type' => 'radios',
      '#options' => array(
        $form_state['default_product']->title,
      )
    );
  }

}
liupascal’s picture

Ok I tried to roll up another patch giving the option to display the attribute widget even when there is only 1 product in the product display.

The idea is to add a setting at the "commerce_product_reference" "commerce_cart_add_to_cart_form" field formatter.

Here are the changes I wanted to do, but i'm stuck at a certain point :
1/ in commerce_cart.module line 2094

function commerce_cart_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state)

add

    $element['single_product_attribute_display'] = array(
      '#type' => 'checkbox',
      '#title' => t('Display the product\'s attribute(s) widget if there is only product being displayed.'),
      '#default_value' => $settings['single_product_attribute_display'],
    );

Then in commerce_cart_add_to_cart_form(), at 1475 : where we see
if (count($products) == 1) {
replace by
if (count($products) == 1 && !$single_product_attribute_display) {

Where $single_product_attribute_display is the setting we created.
The thing is i am not sure how to "cleanly" get this setting value at this point without "reloading" the whole field formatter settings values (which wouldn't be so clean).
This is the reason why i didn't directly propose a patch.

Thanks

wajudai’s picture

commerce_visible_attributes.patch queued for re-testing.

bojanz’s picture

Title: Add to cart form doesn't show attributes drop-down when there is only one associated product » Add to cart form should optionally show the rendered attributes when there is only one associated product
Assigned: Unassigned » kotnik
Category: support » feature
Status: Needs review » Active
Issue tags: -dcsprint6

Discussed this with Ryan, and we agreed on the following course of action:

1) Add an "Add to cart form" view mode for products.
2) Add a checkbox (off by default) to the "Add to cart form" formatter settings saying "If there's only one product referenced, show the attributes using the 'Add to cart form' view mode."
3) In commerce_cart_add_to_cart_form(), if there's only one product and that setting is true, do an entity_view(), take the render arrays for each attribute, render them and return them (make sure that their weight is respected).

So that's basically #2 implemented.
Kotnik agreed to work on this, so I'm assigning it to him.

kotnik’s picture

Status: Active » Needs review
FileSize
4.35 KB

Implemented workflow described in #13 in patch attached.

bojanz’s picture

Status: Needs review » Needs work

Just a reminder of our today's IRC talk:

'custom settings' => FALSE should be TRUE.
Instead of passing the complete $content into the form, the code needs to take only the attribute render arrays (so it needs to iterate over the attribute fields like it does when there are multiple products).

kotnik’s picture

Status: Needs work » Needs review
FileSize
5.27 KB

Here's a patch that deals with comments in #15.

kotnik’s picture

Damian did not like the fact that we have separate pathways user must take if we have a product with one or many variations. Hence this patch which simplifies things, from a user perspective.

Damien Tournoud’s picture

Title: Add to cart form should optionally show the rendered attributes when there is only one associated product » Add to cart form should optionally show when there is only one associated product

Re-scoping the issue. I think the option should be named something like show_if_only_product.

kotnik’s picture

kotnik’s picture

Did s/attributes_single/show_if_only_product/g as suggested in #18.

rszrama’s picture

Ok, so the idea here is that we're showing attribute select lists even when there's only one product on the Add to Cart form. It looks like the ability to just display the formatted output for the field was scuttled, which is fine with me. However, in the event that an Add to Cart form only has one product and an optional attribute field was left empty, we're now seeing an unnecessary product select box:

I'll go ahead and fix this and commit the patch. No update function necessary as the default setting for the new option is the current behavior.

rszrama’s picture

Status: Needs review » Needs work
FileSize
80.34 KB

Uploading the image here.

rszrama’s picture

Assigned: kotnik » Unassigned
Status: Needs work » Fixed

Alrighty, here's the commit. One other substantive change that you should keep in mind for future patches looking at line item data like this did in its first hunk - we can't assume that every line item for which this form is built will have the new array key set. Instead of just using !, I used empty() so that non-existent keys and FALSE options alike are accommodated.

Overall, it's a great patch for its simplicity. When we finally optimize this form, we'll want to be able to cut out all the logic that a single product form with attributes showing will go through to determine the default product, used options, etc. We can streamline it a lot if we know there's just one product on the form. Out of scope for this issue, and likely not to be addressed until 2.x.

Commit: http://drupalcode.org/project/commerce.git/commitdiff/aec9de4

Status: Fixed » Closed (fixed)

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

PedroKTFC’s picture

Sorry guys, I am thoroughly baffled! I've been looking for this setting all night and can't find it anywhere! I'm sure I'm doing something stupid but can't think what. Can someone point me to it, but very very plainly please! I've looked at products, product types, product displays, store without any success.

Thanks
Edited note. Of course after posting this I had another look and found it immediately! Doh!

alexanansi’s picture

@PedroKTFC - can you tell me where you found this option? I can't find it!

PedroKTFC’s picture

Hi Alex

If you go to the product display content type, go to manage display and for the Product field, click on the "gear" link at the right to edit its specific attributes and you'll find the checkbox "Show attribute widgets even if the Add to Cart form only represents one product." there.

Happy clicking!