diff --git a/modules/customer/commerce_customer.module b/modules/customer/commerce_customer.module index 477528b..c26974f 100644 --- a/modules/customer/commerce_customer.module +++ b/modules/customer/commerce_customer.module @@ -402,14 +402,37 @@ function commerce_customer_commerce_checkout_pane_info() { 'title' => !empty($instance['label']) ? check_plain($instance['label']) : $profile_type['name'], 'file' => 'includes/commerce_customer.checkout_pane.inc', 'base' => 'commerce_customer_profile_pane', - 'page' => 'checkout', + 'page' => !empty($instance['label']) ? 'checkout' : 'disabled', 'weight' => isset($profile_type['checkout_pane_weight']) ? $profile_type['checkout_pane_weight'] : $weight++, ); } return $checkout_panes; } - +/** + * Implementation of hook_form_FORM_ID_alter() for Checkout settings form + */ +function commerce_customer_form_commerce_checkout_builder_form_alter(&$form, &$form_state, $form_id) { + $form['actions']['submit']['#submit'][] = $form['actions']['reset']['#submit'][] = 'commerce_customer_commerce_checkout_builder_form_submit'; +} +/** + * Disable the Pane if Customer Profile empty + */ +function commerce_customer_commerce_checkout_builder_form_submit($form, &$form_state) { + foreach ($form_state['values']['panes'] as $pane_id => $pane_values) { + if (strstr($pane_id, 'customer_profile_')) { + $checkout_pane = commerce_checkout_pane_load($pane_id); + $customer_profile = variable_get('commerce_' . $checkout_pane['pane_id'] . '_field', ''); + if (empty($customer_profile)) { + $checkout_pane['enabled'] = FALSE; + $checkout_pane['page'] = 'disabled'; + commerce_checkout_pane_save($checkout_pane); + drupal_static_reset('commerce_checkout_panes'); + drupal_set_message(t('%checkout_pane Checkout pane disabled. No Customer Profile field selected.', array('%checkout_pane' => $checkout_pane['title']))); + } + } + } +} /** * Implements hook_field_views_data(). */ @@ -1243,3 +1266,27 @@ function commerce_customer_profile_set_properties($profile, $name, $value) { $profile->uid = $value; } } +/** + * Implements hook_field_delete_field(). + * Disable the Panes if Customer Profile empty + */ +function commerce_customer_field_delete_field($field) { + if ($field['type'] != 'commerce_customer_profile_reference') { + return; + } + + $pane_profile_vars = db_query("SELECT name FROM {variable} WHERE value LIKE '%" . $field['field_name'] . "%' AND name LIKE 'commerce_customer_profile%'")->fetchAll(); + if ($pane_profile_vars) { + foreach ($pane_profile_vars as $pane_profile_var) { + variable_set($pane_profile_var->name, ''); + //dsm($pane_profile_var); + $profile_type = str_replace('commerce_customer_profile_', '', $pane_profile_var->name); + $profile_type = str_replace('_field', '', $profile_type); + $checkout_pane = commerce_checkout_pane_load('customer_profile_' . $profile_type); + $checkout_pane['enabled'] = FALSE; + $checkout_pane['page'] = 'disabled'; + commerce_checkout_pane_save($checkout_pane); + drupal_static_reset('commerce_checkout_panes'); + } + } +} \ No newline at end of file diff --git a/modules/customer/includes/commerce_customer.checkout_pane.inc b/modules/customer/includes/commerce_customer.checkout_pane.inc index 20cca56..7382ff6 100644 --- a/modules/customer/includes/commerce_customer.checkout_pane.inc +++ b/modules/customer/includes/commerce_customer.checkout_pane.inc @@ -32,10 +32,27 @@ function commerce_customer_profile_pane_settings_form($checkout_pane) { '#description' => t('Specify the customer profile reference field on the order to populate with profile data from this checkout pane.'), '#options' => $options, '#default_value' => variable_get('commerce_' . $checkout_pane['pane_id'] . '_field', ''), + '#element_validate' => array('commerce_customer_profile_pane_settings_form_validate'), ); - + return $form; } +/** + * Checkout pane callback: validates a customer profile edit form. + */ +function commerce_customer_profile_pane_settings_form_validate($element, &$form_state, $form) { + $checkout_pane = commerce_checkout_pane_load($form_state['values']['checkout_pane']['pane_id']); + // Disabled the Pane if Customer Profile empty + if (empty($form_state['values']['commerce_' . $checkout_pane['pane_id'] . '_field'])) { + $checkout_pane['enabled'] = FALSE; + $checkout_pane['page'] = 'disabled'; + commerce_checkout_pane_save($checkout_pane); + drupal_static_reset('commerce_checkout_panes'); + drupal_set_message(t('%checkout_pane Checkout pane disabled. No Customer Profile field selected.', array('%checkout_pane' => $checkout_pane['title']))); + } + + return TRUE; +} /** * Checkout pane callback: returns a customer profile edit form.