Is it possible to hide the extra field if a certain condition is met i.e. customer only has one item in their cart?

Thanks.

Comments

megachriz’s picture

No, currently this is not possible with Extra Fields Pane. Can you tell me what your use case is?

Enzomaticus’s picture

Sure - if a customer purchases only one item they need to select a particular shipping option. For clients who purchase more than one product the shipping option is actually a confusing one (in this particular situation anyway) so I would like them not to see the dropdown select.

megachriz’s picture

Component: Documentation » User interface
Assigned: Unassigned » megachriz

I thought maybe I could implement this functionality with Conditional Actions, but it appears there is no trigger for when the checkout page is loaded.

If you know some PHP, the best thing you can do is create a module and implement a form alter in it:
1. Create a function called mymodule_form_alter() or mymodule_form_uc_cart_checkout_form_alter() where 'mymodule' is the name of your module.
2. In case you had created mymodule_form_alter(), check if $form_id is 'uc_cart_checkout_form'.
3. Check the number of products in the cart.
4. In case it is more than 1 unset() the field. The field will still be displayed on the review page, though.

Example: unset the field 'ucxf_delivery_option' in the delivery pane:

<?php
function mymodule_form_uc_cart_checkout_form_alter($form, $form_state) {
  // (your check for the number of products in the cart here)

  // Unset the delivery option field
  unset($form['panes']['delivery']['delivery_ucxf_delivery_option']);
}
?>

Hm, maybe I need to add a hook that let other modules say if the field may be displayed or not, because you might want to hide the field on the review page too.

jday’s picture

I would like to use a checkbox in the extra fields to determine if the additional fields are displayed or not.
Seems like a jquery/javascript task.

I would have a single checkbox for users who wanted to use a credit card on file for their purchase, and if checked, then two additional fields would be displayed: Name on the card, and Last 4 digits of the card. Otherwise these fields should be hidden.

Has anyone been able to add the conditional fields feature?

kinnaz’s picture

kinnaz’s picture

"
Example: unset the field 'ucxf_delivery_option' in the delivery pane:

function mymodule_form_uc_cart_checkout_form_alter($form, $form_state) {
  // (your check for the number of products in the cart here)

  // Unset the delivery option field
  unset($form['panes']['delivery']['delivery_ucxf_delivery_option']);
}

"
Would something similar work with 1.x uc_extra_fields_pane?

I tried to use the example code but for some reason it didnot hide the extra field panes at checkout like expected.

Did i misunderstood the example code or this aproach isnt suitable for 1.x version?

megachriz’s picture

@kinnaz
The solution I posted is specific for the 2.x version, because in the 1.x version the extra address fields didn't appear in the delivery/billing panes provided by Ubercart, but in separate panes.

However, a similar solution should also work for the 1.x version. The difference is that the field is located somewhere else in the form. Do something like a print_r() on the $form variable in your custom module to see how the form tree is structured.

Example to get detailed information about the form structure:

<?php
function mymodule_form_uc_cart_checkout_form_alter($form, $form_state) {
  echo '<pre>';
  print_r($form);
  echo '</pre>';
  die();
}
?>

The form array will be printed and you will see where in the form array the field is you want to hide. The form array can be big though, it can take you a while to locate the field. If you have the devel module installed, you can also use the dpm() function, which will print the form array in a way it's easier to go through.

Example with dpm():

<?php
function mymodule_form_uc_cart_checkout_form_alter($form, $form_state) {
  dpm($form);
}
?>
megachriz’s picture

Status: Active » Postponed

A hook to let other modules say if a field may be displayed (see comment #3) could be added. Because I personally don't need this feature I have no plans to implement this at the moment, but patches are welcome. In the meantime this issue will be marked as postponed.

megachriz’s picture

Assigned: megachriz » Unassigned

I'm no longer working on this, so unassigning myself. Patches are still welcome. See also #1542256: Hide Extra Field if empty for a similar request.