function commerce_reorder_helper in Commerce Reorder 7.2
Same name and namespace in other branches
- 7 commerce_reorder.module \commerce_reorder_helper()
Helper function to reorder a previous order.
This function copies the line items and optionally, profile fields, over to a the shopping cart of the targeted uid.
Parameters
object $order: The commerce_order object to reorder.
int $uid: An account uid. The uid will be used with the add to cart functions for adding products to a cart.
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').
2 calls to commerce_reorder_helper()
- commerce_reorder_reorder_callback in ./
commerce_reorder.module - Perform the reorder action for the operations menu
- commerce_reorder_reorder_form_submit in ./
commerce_reorder.module - Submit handler: Completes the reorder.
File
- ./
commerce_reorder.module, line 106 - Allows users to create a new order from their order history.
Code
function commerce_reorder_helper($order = NULL, $uid = NULL, $settings = array()) {
$profile_types = array_keys(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,
'copy_profiles_source' => 'order',
'suppress_cart_messages' => FALSE,
);
if (!isset($order)) {
return;
}
if (empty($uid)) {
$uid = $order->uid;
}
if ($settings['suppress_cart_messages']) {
$existing_status_updates = $_SESSION['messages']['status'];
}
// Get the line items of the order.
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
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 one.
$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);
// @TODO Add option to combine / add separately.
// See @commerce_cart_product_add
commerce_cart_product_add($uid, $new_line_item);
}
else {
drupal_set_message(t('Some products weren\'t copied to the cart as they aren\'t currently available.'), 'status', FALSE);
}
}
else {
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($uid);
$cart_wrapper = entity_metadata_wrapper('commerce_order', $cart);
}
$field_name = 'commerce_customer_' . $profile_type;
$profile_id = FALSE;
// If the new order doesn't have this field defined, skip it.
if (!isset($cart_wrapper->{$field_name})) {
continue;
}
switch ($settings['copy_profiles_source']) {
case 'order':
if (!isset($order_wrapper->{$field_name})) {
continue;
}
$profile_id = $order_wrapper->{$field_name}
->value()->profile_id;
break;
case 'addressbook':
$profile_id = commerce_addressbook_get_default_profile_id($account->uid, $profile_type);
break;
}
if ($profile_id != FALSE) {
$cart_wrapper->{$field_name}
->set($profile_id);
}
}
// Save the $cart_wrapper.
if (isset($cart_wrapper)) {
$cart_wrapper
->save();
}
if ($settings['suppress_cart_messages']) {
$_SESSION['messages']['status'] = $existing_status_updates;
if (isset($_SESSION['messages']['commerce-add-to-cart-confirmation'])) {
unset($_SESSION['messages']['commerce-add-to-cart-confirmation']);
}
}
}