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.
Comment | File | Size | Author |
---|---|---|---|
#22 | add-to-cart-form-patch.png | 80.34 KB | rszrama |
#20 | commerce-1029002-20_add_attr_one_prod.patch | 3.2 KB | kotnik |
#17 | commerce-1029002-17_add_attr_one_prod.patch | 3.15 KB | kotnik |
#16 | commerce-1029002-16_add_attr_one_prod.patch | 5.27 KB | kotnik |
#14 | commerce-1029002-14_add_attr_one_prod.patch | 4.35 KB | kotnik |
Comments
Comment #1
rszrama CreditAttribution: rszrama commentedI 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.
Comment #2
ReeceMarsland CreditAttribution: ReeceMarsland commentedI'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?
Comment #3
liupascal CreditAttribution: liupascal commentedHow 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
Comment #4
liupascal CreditAttribution: liupascal commentedHere 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 :-)
Comment #5
liupascal CreditAttribution: liupascal commentedForgot to change the status !
Re-attach the patch to have it tested
Comment #7
wajudai CreditAttribution: wajudai commented#5: display_or_hide_attribute_widget-1029002-5308124.patch queued for re-testing.
Comment #9
uditmahajan CreditAttribution: uditmahajan commentedcommerce_visible_attributes.patch queued for re-testing.
Comment #10
Paul Lomax CreditAttribution: Paul Lomax commentedHad this problem too, got round it with a fairly simple form alter, maybe this will help others:
Comment #11
liupascal CreditAttribution: liupascal commentedOk 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
add
Then in
commerce_cart_add_to_cart_form(), at 1475
: where we seeif (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
Comment #12
wajudai CreditAttribution: wajudai commentedcommerce_visible_attributes.patch queued for re-testing.
Comment #13
bojanz CreditAttribution: bojanz commentedDiscussed 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.
Comment #14
kotnik CreditAttribution: kotnik commentedImplemented workflow described in #13 in patch attached.
Comment #15
bojanz CreditAttribution: bojanz commentedJust 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).
Comment #16
kotnik CreditAttribution: kotnik commentedHere's a patch that deals with comments in #15.
Comment #17
kotnik CreditAttribution: kotnik commentedDamian 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.
Comment #18
Damien Tournoud CreditAttribution: Damien Tournoud commentedRe-scoping the issue. I think the option should be named something like
show_if_only_product
.Comment #19
kotnik CreditAttribution: kotnik commentedRelated #1630458: User should be able to change field display if there is only one variation of it. to be implemented after this gets merged.
Comment #20
kotnik CreditAttribution: kotnik commentedDid s/attributes_single/show_if_only_product/g as suggested in #18.
Comment #21
rszrama CreditAttribution: rszrama commentedOk, 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.
Comment #22
rszrama CreditAttribution: rszrama commentedUploading the image here.
Comment #23
rszrama CreditAttribution: rszrama commentedAlrighty, 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
Comment #25
PedroKTFC CreditAttribution: PedroKTFC commentedSorry 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!
Comment #26
alexanansi CreditAttribution: alexanansi commented@PedroKTFC - can you tell me where you found this option? I can't find it!
Comment #27
PedroKTFC CreditAttribution: PedroKTFC commentedHi 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!