It would be nice to be able to display a -None- option in the product selector for the add to cart formatter.
Now is kind of ready, but there's no direct way for this code to execute (commerce_cart_add_to_cart_form()) as the product reference widget removes the empty values

            if (!$data['required'] && in_array('', $used_options[$field_name])) {
              $form['attributes'][$field_name]['#empty_value'] = $properties['empty_option'];
            }

Patch attached that exposes a setting for the add to cart formatter and takes care of the defaults in the add to cart form.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

rszrama’s picture

I'm not sure this is feasible, as we always need the select lists on an Add to Cart form to point to a valid product. Without that, we'd end up trying to render fields from a NULL product when attributes changed or something. The only thing we could do is "fake it" like I suppose your patch does - by just showing fields from the first product in the list even if it isn't actually selected.

pcambra’s picture

Exactly, this patch still renders the default product but allows you to have an empty default option.
This way you could still do some stuff around the entity view, formatter, etc.

Maybe it doesn't make much sense to expose the setting in UI but probably it would be good to have the chance of having an empty selector.

pcambra’s picture

I'd say that if someone wants this, it's just a matter of adding an #empty_option and #default_value in a form_alter to the fields they want to be emptied and then take care of it by code.

Either we think about having this "empty option" option for the site builders or not, but if we don't want to support it by default, we should remove the piece of code mentioned in #0 as it's probably doing nothing.

rszrama’s picture

Status: Needs review » Needs work

Just noticed the patch as is won't work because you're changing the function signature of the Add to Cart form builder. In the 1.x branch we can't be changing the order of parameters, and I'm not sure we'd want to add this new setting as an additional parameter to that function anyways. In order to reproduce this form from the resulting product line item later, we'd need to store this option in the line item's data array somehow (likely in its context sub-array where other pertinent information is stored).

vasike’s picture

Status: Needs work » Needs review
FileSize
8.07 KB

reworked the patch, adding the new setting as line item's data array instead of function parameter.

it does work for product selector
BUT it seems it won't work for products with attributes, the products data remains the same no matter of attribute(s) changes.

joachim’s picture

I've a use case for this from a client: many of their customers click the 'add to cart' button without bothering to check the product attribute for the size, and thus buy the wrong size. The client then has to exchange them and loses money on the transaction.

This patch needs some changes if #1784534: allow fields other than those with option lists to function as product attribute selection fields gets in; I'll work on it tomorrow.

joachim’s picture

I'm somewhat stumped how to make this work.

I can add an initial empty option to the $options for $form['attributes'][$field_name]. But on the node's add to cart form, even when the select dropdown for the attribute says '- None -' or '- Select -', I am able to click the 'add to cart' button and the first actual product is added to my cart. Which is not at all what should happen -- the user should be forced to select an attribute before clicking the 'add to cart' button.

rszrama’s picture

Version: 7.x-1.x-dev » 8.x-2.x-dev
Issue summary: View changes
Status: Needs review » Active

I'm moving this to the 2.x version. I really don't think we can make this change in 1.x because code written to alter the Add to Cart form would currently be able to depend on the product_id form field containing a value. In 1.x, I think the only solution for now will be a client-side tweaking of the form element w/ validation.

bojanz’s picture

This should be easy to implement in 2.x
We add a setting to the attribute selection widget that allows you to specify which attributes are empty by default, and we add #empty_option based on that.

steveoliver’s picture

Priority: Normal » Minor
Status: Active » Needs work
drugan’s picture

This feature is now implemented on the Commerce Correct Attributes submodule of the Commerce Extended Attributes.