The Product Reference module displays Product fields in Nodes when the node type contains a product reference field. At this time, it only supports single value reference fields and has no alternate behavior for multiple value fields. This should be worked on... at least defaulting to displaying the fields from the first referenced product. It could also include displaying image fields in the Ubercart style with the first referenced image being large and all the others being thumbnails and/or a price range where the cheapest and most expensive price option are displayed like "$8 - $10".

The function in question at this point is commerce_product_reference_node_view().

CommentFileSizeAuthor
#4 disable_single_attribute_fields.patch775 bytesrszrama

Comments

rszrama’s picture

Issue tags: -low-hanging fruit

Just a quick note on this... I've taken a two step approach. I've already committed a change that will use the first value from a multiple value product reference field to pull product fields into the node for display. The Add to Cart form uses this manner of selecting a default right now, too.

The second step, yet to be committed, is to have the Add to Cart form update these fields based on the currently selected product as the customer chooses attributes. This is actually working, but I don't have a generic solution in place to accommodate different contexts for the Add to Cart form (i.e. tell the form what node it is being embedded on). That should be a quick bit of work in the morning and then this will be committed.

It's too exciting not to share, though, and I think these pictures are worth a thousand words. First we have an image of a product display node that references each of the products in my test site. These products have a size field that the Add to Cart form uses to generate product selection widgets instead of just a product title select list:

Only local images are allowed.

When the new size is selected, the Add to Cart form refreshes via #ajax with a new default product. We return a set of AJAX replacement commands to re-render the product fields on the page using the new product:

Only local images are allowed.

Once I get the context issue sorted out, I'll do another commit, and then I'll make this a little more performant (i.e. only rendering fields that are actually shown, since that's one of the most expensive processes in Drupal).

rszrama’s picture

Status: Active » Needs review

This is now working and just needs some further testing to root out any bugs. Relevant commits:

https://github.com/rszrama/drupalcommerce/commit/f08c544f00dd4cd53eb7c70...
https://github.com/rszrama/drupalcommerce/commit/88b2050b6220ae1d02d0d0f...
https://github.com/rszrama/drupalcommerce/commit/b55fbc4068ccf33ef978ea8...

This was held up for quite a while by the fact that I was doing a full form replacements when the AJAX callback function was called. The problem with re-rendering the whole form is that the ID that I was using to target the form for replacement was being incremented each time by drupal_html_id(), so it turns out I had to duplicate the id in the class of the form and target it by that.

We'll see how this plays out in real use. The only remaining question is what to do if Javascript is off...

Would appreciate anyone's review and feedback.

rszrama’s picture

Just added support for dependent attributes. Also, I need feedback on this idea... when an attribute select list only has one option, I'm thinking I should disable it so that it's clear the customer doesn't need to choose anything but the information will still be visible in the form. Anyone think otherwise? The only viable alternative I can think of is to leave it enabled with a single value, which is just a little confusing.

Commit (here be dragons): https://github.com/rszrama/drupalcommerce/commit/8b51609383e7c332f45df39...

rszrama’s picture

StatusFileSize
new775 bytes

I'm attaching a patch so you can test out what it looks like when an attribute select list has only a single option available. Without the patch it will render as a normal select list with a single option. With the patch it will render as a disabled select list in which the single option is still visible.

To get the full benefit, you'll need to configure a product type with two fields that qualify as attribute fields for the Add to Cart form (i.e. a List type field that is required and single value with a definite set of options). Then make products for each option of the first field that have any option for the second field. Then make another product or two that reuse some of the first field's options but use different options for the second field than their counterparts. Fiddle around with it... I'll need to rebuild our demo to show these things off so it's easier to discuss. : P

rszrama’s picture

Status: Needs review » Fixed

Actually, I think it's probably better to leave the single option if it exists. Disabled form elements have their own FAPI and usability problems... best to leave it alone for now. This one was done a long time ago, just putting the fork in it.

Status: Fixed » Closed (fixed)

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

bogdan1988’s picture

To display node previews in add to cart form you can use information from here. http://html-and-cms.com/blog/drupal-commerce-add-cart-form-alter-enablin...

rszrama’s picture

Thanks for putting together the little snippet. You might also check out http://drupal.org/project/commerce_add_to_cart_extras which allows you to use Views to create a display formatter for a product reference field that adds multiple products to the cart at once. Not the same as your patch, but perhaps that module could be enhanced to allow the View to provide the options for a radio select list instead.