You are here

commerce_repeat_order.module in Commerce Repeat Order 7

Allows users to create a new order from their order history.

File

commerce_repeat_order.module
View source
<?php

/**
 * @file
 * Allows users to create a new order from their order history.
 */

/**
 * Implements hook_menu().
 */
function commerce_repeat_order_menu() {
  $items = array();
  $items['admin/commerce/config/order/repeat-order'] = array(
    'title' => 'Commerce Repeat Order Setting Form',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'commerce_repeat_order_settings_form',
    ),
    'access arguments' => array(
      'commerce repeat order admin access',
    ),
  );
  $items['commerce-repeat-order/%'] = array(
    'title' => 'Customer Repeat Order',
    'page callback' => 'commerce_repeat_order',
    'page arguments' => array(
      1,
    ),
    'access arguments' => array(
      'view own commerce_order entities',
    ),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Implements hook_permission().
 */
function commerce_repeat_order_permission() {
  return array(
    'commerce repeat order access' => array(
      'title' => t('Commerce Repeat Order'),
      'description' => t('Access for order to be repeated by customer'),
    ),
  );
}

/**
 * Setting form for Commerce Customer Repeat Order.
 */
function commerce_repeat_order_settings_form($form, &$form_state) {
  $form['commerce_repeat_order_add_or_override'] = array(
    '#title' => "Add Product or Override Products",
    '#description' => t("Add product to existing cart or override existing cart"),
    '#type' => 'radios',
    '#options' => array(
      'add' => t('Add Product'),
      'override' => t('Override'),
    ),
    '#required' => TRUE,
    '#default_value' => variable_get('commerce_repeat_order_add_or_override', 'add'),
  );
  $form['commerce_repeat_order_status_message'] = array(
    '#title' => "Show/Hide add to cart status message",
    '#description' => t("show or hide add to cart set message of product"),
    '#type' => 'radios',
    '#options' => array(
      'show' => t('Show'),
      'hide' => t('Hide'),
    ),
    '#required' => TRUE,
    '#default_value' => variable_get('commerce_repeat_order_show_or_hide', 'hide'),
  );
  return system_settings_form($form);
}

/**
 * Repeats the customer order.
 */
function commerce_repeat_order($order_id) {
  global $user;
  $uid = $user->uid;
  $order_details = commerce_order_load($order_id);
  $order_uid = $order_details->uid;
  $status_message = variable_get('commerce_repeat_order_show_or_hide', 'hide');
  if (!empty($order_uid) && $uid == $order_uid) {
    $add_or_override = variable_get('commerce_repeat_order_add_or_override', 'add');
    if ($add_or_override == 'override') {
      $order = commerce_cart_order_load($user->uid);
      commerce_cart_order_empty($order);
    }
    $order_wrapper = entity_metadata_wrapper('commerce_order', $order_details);
    foreach ($order_wrapper->commerce_line_items as $line_item_wrapper) {
      if (in_array($line_item_wrapper->type
        ->value(), commerce_product_line_item_types())) {
        $product = $line_item_wrapper->commerce_product
          ->value();
        if ($product->status) {
          $line_item = $line_item_wrapper
            ->value();

          // Generate a line item product based in the current order line item.
          $new_line_item = commerce_product_line_item_new($product, $line_item->quantity, $line_item->order_id, $line_item->data, $line_item->type);

          // Merge both line items to get the fields (if any).
          $new_line_item = (object) array_merge((array) $line_item, (array) $new_line_item);
          commerce_cart_product_add($uid, $new_line_item);
        }
        elseif ($status_message == 'show') {
          drupal_set_message(t('Some products were not copied to the cart as they are not currently available.'), 'status', FALSE);
        }
      }
      elseif ($status_message == 'show') {
        drupal_set_message(t('Some products were not copied to the cart as they are mpt currently available.'), 'status', FALSE);
      }
    }
    drupal_goto('cart');
  }
}

Functions

Namesort descending Description
commerce_repeat_order Repeats the customer order.
commerce_repeat_order_menu Implements hook_menu().
commerce_repeat_order_permission Implements hook_permission().
commerce_repeat_order_settings_form Setting form for Commerce Customer Repeat Order.