diff --git a/commerce_reorder.module b/commerce_reorder.module index 5e6f72a..b0c704e 100644 --- a/commerce_reorder.module +++ b/commerce_reorder.module @@ -77,8 +77,27 @@ function commerce_reorder_reorder_tab($order) { /** * Helper function to reorder a previous order. + * + * @param object $order + * The commerce_order object to reorder. + * @param object $account + * The user account to assign the order to. + * @param array $settings + * An optional array that may contain the following values: + * - copy_profiles: An array of profile types from + * commerce_customer_profile_types() to be copied to the order. It will look + * for the fields 'commerce_customer_TYPE' on the order. e.g. + * array('shipping' => 'shipping'). */ -function commerce_reorder_helper($order = NULL, $account = NULL) { +function commerce_reorder_helper($order = NULL, $account = NULL, $settings = array()) { + $profile_types = commerce_customer_profile_types(); + foreach ($profile_types as $type) { + $default_profile_options[$type] = FALSE; + } + + // Set the default copy options. + $settings = (array)$settings + array('copy_profiles' => $default_profile_options); + if (!isset($order)) { return; } @@ -102,7 +121,7 @@ function commerce_reorder_helper($order = NULL, $account = NULL) { // Merge both line items to get the fields (if any). $new_line_item = (object) array_merge((array) $line_item, (array) $new_line_item); - // @TODO Add option to combine / add sepparately. + // @TODO Add option to combine / add separately. // See @commerce_cart_product_add commerce_cart_product_add($account->uid, $new_line_item); } @@ -114,4 +133,25 @@ function commerce_reorder_helper($order = NULL, $account = NULL) { drupal_set_message(t('Some products weren\'t copied to the cart as they aren\'t currently available'), 'status', FALSE); } } + + // Copy over profiles if they are set. + $cart = $cart_wrapper = null; + + foreach ($settings['copy_profiles'] as $profile_type => $copy) { + if ($profile_type !== $copy) { + continue; + } + if (!isset($cart)) { + $cart = commerce_cart_order_load($account->uid); + $cart_wrapper = entity_metadata_wrapper('commerce_order', $cart); + } + + $field_name = 'commerce_customer_' . $profile_type; + if (isset($order_wrapper->{$field_name}) && isset($cart_wrapper->{$field_name})) { + $cart_wrapper->{$field_name}->set($order_wrapper->{$field_name}->value()->profile_id); + } + } + if (isset($cart_wrapper)) { + $cart_wrapper->save(); + } } diff --git a/includes/views/commerce_reorder.views.inc b/includes/views/commerce_reorder.views.inc index a874e68..2b60b4d 100644 --- a/includes/views/commerce_reorder.views.inc +++ b/includes/views/commerce_reorder.views.inc @@ -11,7 +11,7 @@ function commerce_reorder_views_data_alter(&$data) { // Reorder button. $data['commerce_order']['commerce_reorder_button'] = array( 'field' => array( - 'title' => t('Reorder button.'), + 'title' => t('Reorder button'), 'help' => t('Display a button to copy the line items to a new order in cart status.'), 'handler' => 'commerce_reorder_handler_field_commerce_reorder_button', ), diff --git a/includes/views/handlers/commerce_reorder_handler_field_commerce_reorder_button.inc b/includes/views/handlers/commerce_reorder_handler_field_commerce_reorder_button.inc index e718391..c7a72ab 100644 --- a/includes/views/handlers/commerce_reorder_handler_field_commerce_reorder_button.inc +++ b/includes/views/handlers/commerce_reorder_handler_field_commerce_reorder_button.inc @@ -10,6 +10,10 @@ class commerce_reorder_handler_field_commerce_reorder_button extends views_handl $options['reorder_button_label'] = array('default' => 'Reorder', 'translatable' => TRUE); $options['redirect'] = array('default' => TRUE); $options['redirect_url'] = array('default' => 'cart'); + $profile_types = array_keys(commerce_customer_profile_types()); + foreach ($profile_types as $type) { + $options['copy_profiles'][$type] = FALSE; + } return $options; } @@ -17,21 +21,33 @@ class commerce_reorder_handler_field_commerce_reorder_button extends views_handl parent::options_form($form, $form_state); $form['reorder_button_label'] = array( '#type' => 'textfield', - '#title' => t('Reorder button label.'), + '#title' => t('Reorder button label'), '#default_value' => $this->options['reorder_button_label'], ); $form['redirect'] = array( '#type' => 'checkbox', - '#title' => t('Redirect after reorder.'), + '#title' => t('Redirect after reorder'), '#default_value' => $this->options['redirect'], ); $form['redirect_url'] = array( '#type' => 'textfield', - '#title' => t('Target url to redirect after reorder.'), + '#title' => t('Target URL to redirect after reorder'), '#default_value' => $this->options['redirect_url'], '#dependency' => array('edit-options-redirect' => array(TRUE)), ); + $types = commerce_customer_profile_types(); + $copy_profile_options = array(); + foreach ($types as $type => $type_data) { + $copy_profile_options[$type] = t($type_data['name']); + } + $form['copy_profiles'] = array( + '#type' => 'checkboxes', + '#title' => t('Copy customer profiles to the new order'), + '#description' => t('The selected profiles, if present on the existing order, will be copied to the new order.'), + '#options' => $copy_profile_options, + '#default_value' => $this->options['copy_profiles'], + ); } function construct() { @@ -79,7 +95,7 @@ class commerce_reorder_handler_field_commerce_reorder_button extends views_handl foreach (element_children($form[$field_name]) as $row_id) { if ($form_state['triggering_element']['#name'] == 'reorder-line-item-' . $row_id) { $order = commerce_order_load($form[$field_name][$row_id]['#order_id']); - commerce_reorder_helper($order); + commerce_reorder_helper($order, null, array('copy_profiles' => $this->options['copy_profiles'])); } }