Hi.
I have a line item type with an image field for customers to upload designs to be added to printed products.
When they change, say, the gsm or size (fields defined in the product type and exposed as attributes) another upload field appears beneath the initial one.
At first this wasn't such a problem as I had the number of values limited to 2 (two sides of a business card or similar) but my client has told me that there will also be brochures with 20 pages.

I'd like to be able to call up a certain number of empty imagefield upload slots with js (dependent on a product option - single / double sided, no. of pages). That part is more of a support request and may belong in the imagefield issue queue, but the first thing I need to deal with is the fact that an extra field is added when an option is changed.

Comments

xcusso’s picture

I have the same isssue.

griz’s picture

Nudge.
At the moment I'm hiding the extra fields with jQuery, but clearly this is not ok.
One thing I've noticed is that it's only a problem while the field is empty.
Additionally, a weight dropdown is visible (as opposed to the drag handle).

urbanlegend’s picture

Confirmed this issue - using a field exposed as an attribute to be selected prior to Add to Cart (e.g. product color or size) and selecting a different value from default triggers an additional upload field for each selection. Conflict in jQuery script or based on reload of page from the attribute selection?

malberts’s picture

I was working on something else and found that I couldn't reproduce this issue. Then I turned off my module and the issue occurred.

What I found was that if I altered the add to cart form and re-injected the line item using the add to cart form submit code then the issue would go away. I don't have the time to put this in a module and I feel this is a bit of a hack that needs to be solved either in this module or in commerce_line_item. I think the problem is that the data stored in $form_state['line_item'] is not complete until you set that to a submitted line item.

For now, you'd want to do something like:


function mymodule_form_commerce_cart_add_to_cart_form_alter(&$form, &$form_state) {
  // If the form has not been submitted yet, fake the submitted values.
  if (empty($form_state['values'])) {
    $form_state['values'] = array();
  }

  // Get the current product.
  if (!empty($form_state['values']['product_id'])) {
    $product_id = $form_state['values']['product_id'];
    $product = commerce_product_load($product_id);
  }
  else {
    $product = $form_state['default_product'];
  }

  // We need to update the line item in the form state so that the selected
  // values are represented on it.
  // @see commerce_cart_add_to_cart_form_submit()
  if (empty($form_state['line_item']->line_item_id)) {
    // Create the new product line item of the same type.
    $line_item = commerce_product_line_item_new($product, 1 /*$form_state['values']['quantity']*/, 0, $form_state['line_item']->data, $form_state['line_item']->type);

    // Allow modules to prepare this as necessary. This hook is defined by the
    // Product Pricing module.
    drupal_alter('commerce_product_calculate_sell_price_line_item', $line_item);

    // Remove line item field values the user didn't have access to modify.
    foreach ($form_state['values']['line_item_fields'] as $field_name => $value) {
      // Note that we're checking the Commerce Cart settings that we inserted
      // into this form element array back when we built the form. This means a
      // module wanting to alter a line item field widget to be available must
      // update both its form element's #access value and the field_access value
      // of the #commerce_cart_settings array.
      if (empty($form['line_item_fields'][$field_name]['#commerce_cart_settings']['field_access'])) {
        unset($form_state['values']['line_item_fields'][$field_name]);
      }
    }

    // Unset the line item field values array if it is now empty.
    if (empty($form_state['values']['line_item_fields'])) {
      unset($form_state['values']['line_item_fields']);
    }

    // Add field data to the line item.
    field_attach_submit('commerce_line_item', $line_item, $form['line_item_fields'], $form_state);
    
    // Process the unit price through Rules so it reflects the user's actual
    // purchase price.
    rules_invoke_event('commerce_product_calculate_sell_price', $line_item);

    $form_state['line_item'] = $line_item;
  }
}
griz’s picture

Unfortunately this code produces a WSOD for me. Anybody else had any luck with it?

It actually just needs one more closing curly bracket at the end.
Thanks! This solves the problem for now.

malberts’s picture

Thanks for noticing it. I fixed it now.

I'm wondering if this issue shouldn't be moved to the Commerce module issue queue. I imagine that if you defined your line item type and fields in code (i.e. without Commerce Customizable Products) then the same problem should occur. The reason being that only core Commerce code is used when putting the line item onto the Add to Cart form, this one is just a UI for defining more line item types. However, somebody should try this first just to be sure.