' . t('There are no products in your shopping cart.') . '
';
}
return $output;
}
/**
* Themes the summary table at the bottom of the default shopping cart block.
*
* @param $item_count
* The number of items in the shopping cart.
* @param $item_text
* A textual representation of the number of items in the shopping cart.
* @param $total
* The unformatted total of all the products in the shopping cart.
* @param $summary_links
* An array of links used in the summary.
*
* @ingroup themeable
*/
function theme_uc_cart_block_summary($variables) {
$item_count = $variables['item_count'];
$item_text = $variables['item_text'];
$total = $variables['total'];
$summary_links = $variables['summary_links'];
// Build the basic table with the number of items in the cart and total.
$output = '
';
return $output;
}
/**
* Implements hook_views_api().
*/
function uc_cart_views_api() {
return array(
'api' => '2.0',
'path' => drupal_get_path('module', 'uc_cart') . '/views',
);
}
/*******************************************************************************
* Hook Functions (Ubercart)
******************************************************************************/
/**
* Implements hook_uc_message().
*/
function uc_cart_uc_message() {
global $user;
$messages['checkout_instructions'] = '';
$messages['review_instructions'] = t("Your order is almost complete. Please review the details below and click 'Submit order' if all the information is correct. You may use the 'Back' button to make changes to your order if necessary.");
$messages['completion_message'] = t('Your order is complete! Your order number is [uc_order:order-id].');
$messages['completion_logged_in'] = t('Thank you for shopping at [store:name]. While logged in, you may continue shopping or view your current order status and order history.');
$messages['completion_existing_user'] = t("Thank you for shopping at [store:name]. Your current order has been attached to the account we found matching your e-mail address.\n\nLogin to view your current order status and order history. Remember to login when you make your next purchase for a faster checkout experience!", array('!user_url' => url('user')));
$messages['completion_new_user'] = t("Thank you for shopping at [store:name]. A new account has been created for you here that you may use to view your current order status.\n\nLogin to your new account using the following information:\n\nUsername: !new_username\nPassword: !new_password", array('!user_url' => url('user')));
$messages['continue_shopping'] = t('Return to the front page.');
return $messages;
}
/**
* Implements hook_uc_cart_pane().
*/
function uc_cart_uc_cart_pane($items) {
$body = array();
if (!is_null($items)) {
$body = drupal_get_form('uc_cart_view_form', $items) + array(
'#prefix' => '
',
'#suffix' => '
',
);
}
$panes[] = array(
'id' => 'cart_form',
'title' => t('Default cart form'),
'enabled' => TRUE,
'weight' => 0,
'body' => $body,
);
return $panes;
}
/**
* Implements hook_uc_checkout_pane().
*/
function uc_cart_uc_checkout_pane() {
$panes[] = array(
'id' => 'cart',
'callback' => 'uc_checkout_pane_cart',
'title' => t('Cart contents'),
'desc' => t("Display the contents of a customer's shopping cart."),
'weight' => 1,
'process' => FALSE,
'collapsible' => FALSE,
);
$panes[] = array(
'id' => 'customer',
'callback' => 'uc_checkout_pane_customer',
'title' => t('Customer information'),
'desc' => t('Get the necessary information to create a customer on the site.'),
'weight' => 2,
);
$panes[] = array(
'id' => 'delivery',
'callback' => 'uc_checkout_pane_delivery',
'title' => t('Delivery information'),
'desc' => t('Get the information for where the order needs to ship.'),
'weight' => 3,
'shippable' => TRUE,
);
$panes[] = array(
'id' => 'billing',
'callback' => 'uc_checkout_pane_billing',
'title' => t('Billing information'),
'desc' => t('Get basic information needed to collect payment.'),
'weight' => 4,
);
$panes[] = array(
'id' => 'comments',
'callback' => 'uc_checkout_pane_comments',
'title' => t('Order comments'),
'desc' => t('Allow a customer to put comments on an order.'),
'weight' => 7,
);
return $panes;
}
/*******************************************************************************
* Callback Functions, Forms, and Tables
******************************************************************************/
/**
* Updates a user's cart to include items from their anonymous session.
*/
function uc_cart_login_update($uid) {
if (!isset($_SESSION['uc_cart_id'])) {
return;
}
// Get the current contents of the cart.
$items = uc_cart_get_contents($uid);
// Update the cart so the ID is switched from the session to user ID.
db_update('uc_cart_products')
->fields(array(
'cart_id' => $uid,
))
->condition('cart_id', $_SESSION['uc_cart_id'])
->execute();
// If there were items before the update, we need to re-add them all to
// take care of item consolidation.
if (count($items) > 0) {
// Store again what items these are.
$items = uc_cart_get_contents($uid, 'rebuild');
// Remove from the table all the items in the cart.
// Should be a function call instead of a single query. -RS
db_delete('uc_cart_products')
->condition('cart_id', $uid)
->execute();
// Reset the cart item cache.
uc_cart_get_contents($uid, 'rebuild');
// Loop through what the items should be and re-add them to the cart.
foreach ($items as $key => $item) {
uc_cart_add_item($item->nid, $item->qty, $item->data, $uid, FALSE, FALSE, FALSE);
}
// Unset the anonymous cart ID, it's no longer needed
unset($_SESSION['uc_cart_id']);
// Rebuild the cart item cache.
uc_cart_get_contents($uid, 'rebuild');
}
}
/**
* Returns the text displayed for an empty shopping cart.
*
* @ingroup themeable
*/
function theme_uc_empty_cart() {
return '
' . t('There are no products in your shopping cart.') . '
';
}
/**
* Display a page allowing the customer to view the contents of his or her cart.
*
* Handles simple or complex objects. Some cart items may have a list of
* products that they represent. These are displayed but are not able to
* be changed by the customer.
*
* @see uc_cart_view_form_submit()
* @see theme_uc_cart_view_form()
* @see theme_uc_cart_view_price()
* @see uc_cart_view_table()
* @ingroup forms
*/
function uc_cart_view_form($form, &$form_state, $items = NULL) {
$form['items'] = array(
'#type' => 'tapir_table',
'#tree' => TRUE,
);
$i = 0;
foreach ($items as $item) {
module_invoke_all('uc_cart_item', 'view', $item);
$display_item = module_invoke($item->module, 'uc_cart_display', $item);
if (!empty($display_item)) {
$form['items'][$i] = $display_item;
$form['items'][$i]['image'] = uc_product_get_picture($display_item['nid']['#value'], 'uc_cart');
$description = $display_item['title']['#markup'] . $display_item['description']['#markup'];
$form['items'][$i]['desc']['#markup'] = $description;
if (isset($form['items'][$i]['remove'])) {
// Backward compatibility with old checkbox method.
if ($form['items'][$i]['remove']['#type'] == 'checkbox') {
$form['items'][$i]['remove'] = array('#type' => 'submit', '#value' => t('Remove'));
}
$form['items'][$i]['remove']['#name'] = 'remove-' . $i;
}
$form['items'][$i]['title']['#type'] = 'value';
$form['items'][$i]['description']['#type'] = 'value';
if (empty($display_item['qty'])) {
$form['items'][$i]['qty'] = array(
'#markup' => '',
);
}
$form['items'][$i]['total'] = array(
'#theme' => 'uc_price',
'#price' => $display_item['#total'],
);
$i++;
}
}
$form['items'] = tapir_get_table('uc_cart_view_table', $form['items']);
$form['actions'] = array('#type' => 'actions');
// If the continue shopping element is enabled...
if (($cs_type = variable_get('uc_continue_shopping_type', 'link')) !== 'none') {
// Setup the text used for the element.
$cs_text = variable_get('uc_continue_shopping_text', '') ? variable_get('uc_continue_shopping_text', '') : t('Continue shopping');
// Add the element to the form based on the element type.
if (variable_get('uc_continue_shopping_type', 'link') == 'link') {
$form['continue_shopping'] = array(
'#markup' => l($cs_text, uc_cart_continue_shopping_url()),
);
}
elseif (variable_get('uc_continue_shopping_type', 'link') == 'button') {
$form['actions']['continue_shopping'] = array(
'#type' => 'submit',
'#value' => $cs_text,
);
$form['continue_shopping_text'] = array(
'#type' => 'hidden',
'#value' => $cs_text,
);
}
}
// Add the control buttons for updating and proceeding to checkout.
$form['actions']['update'] = array(
'#type' => 'submit',
'#value' => t('Update cart'),
);
if (variable_get('uc_checkout_enabled', TRUE)) {
$form['actions']['checkout'] = array(
'#type' => 'submit',
'#value' => t('Checkout'),
);
}
return $form;
}
/**
* Form submission handler for uc_cart_view_form().
*
* @see uc_cart_view_form()
*/
function uc_cart_view_form_submit($form, &$form_state) {
// Remove the cart order variable if the customer came here during checkout.
if (isset($_SESSION['cart_order'])) {
unset($_SESSION['cart_order']);
}
// If a remove button was clicked, set the quantity for that item to 0.
if (substr($form_state['triggering_element']['#name'], 0, 7) == 'remove-') {
$item = substr($form_state['triggering_element']['#name'], 7);
$form_state['values']['items'][$item]['qty'] = 0;
drupal_set_message(t('!product-title removed from your shopping cart.', array('!product-title' => $form['items'][$item]['title']['#markup'])));
}
// Update the items in the shopping cart based on the form values.
uc_cart_update_item_object((object)$form_state['values']);
// Specify the appropriate redirect based on the button used to submit.
switch ($form_state['triggering_element']['#value']) {
// Continue shopping button.
case variable_get('uc_continue_shopping_text', t('Continue shopping')):
$form_state['redirect'] = uc_cart_continue_shopping_url();
break;
// Update cart button.
case t('Update cart'):
// No redirect. Just display a message and preserve the last URL.
drupal_set_message(t('Your cart has been updated.'));
break;
// Checkout button.
case t('Checkout'):
$form_state['redirect'] = variable_get('uc_checkout_enabled', TRUE) ? 'cart/checkout' : 'cart';
break;
}
}
/**
* Themes the uc_cart_view_form().
*
* @see uc_cart_view_form()
* @ingroup themeable
*/
function theme_uc_cart_view_form($variables) {
$form = $variables['form'];
drupal_add_css(drupal_get_path('module', 'uc_cart') . '/uc_cart.css');
$output = '
'
. drupal_render($form['items']) . '
';
foreach (element_children($form['items']) as $i) {
foreach (array('title', 'options', 'remove', 'image', 'qty') as $column) {
$form['items'][$i][$column]['#printed'] = TRUE;
}
$form['items'][$i]['#printed'] = TRUE;
}
// Add the continue shopping element and cart submit buttons.
if (($type = variable_get('uc_continue_shopping_type', 'link')) != 'none') {
// Render the continue shopping element into a variable.
$cs_element = drupal_render($form['continue_shopping']);
// Add the element with the appropriate markup based on the display type.
if ($type == 'link') {
$output .= '
'
. $cs_element . '
' . drupal_render_children($form) . '
';
}
elseif ($type == 'button') {
$output .= '
'
. drupal_render_children($form) . '
'
. $cs_element . '
';
}
}
else {
$output .= '
' . drupal_render_children($form) . '
';
}
return $output;
}
/**
* Displays the total price of the cart items.
*
* @see uc_cart_view_form()
* @ingroup themeable
*/
function theme_uc_cart_view_price($variables) {
return uc_currency_format($variables['form']['#markup']);
}
/**
* Lists the products in the cart in a TAPIr table.
*/
function uc_cart_view_table($table) {
$table['#attributes'] = array('width' => '100%');
$table['#columns'] = array(
'remove' => array(
'cell' => t('Remove'),
'weight' => 0,
),
'image' => array(
'cell' => t('Products'),
'weight' => 1,
),
'desc' => array(
'cell' => '',
'weight' => 2,
),
'qty' => array(
'cell' => t('Qty.'),
'weight' => 3,
),
'total' => array(
'cell' => t('Total'),
'weight' => 4,
),
);
$subtotal = 0;
foreach (element_children($table) as $i) {
$subtotal += $table[$i]['#total'];
$table[$i]['remove']['#cell_attributes'] = array('align' => 'center', 'class' => array('remove'));
$table[$i]['image']['#cell_attributes'] = array('class' => array('image'));
$table[$i]['desc']['#cell_attributes'] = array('class' => array('desc'));
$table[$i]['qty']['#cell_attributes'] = array('class' => array('qty'));
$table[$i]['total']['#cell_attributes'] = array('class' => array('price'));
$table[$i]['#attributes'] = array('valign' => 'top');
}
$table[] = array(
'total' => array(
'#theme' => 'uc_price',
'#prefix' => '' . t('Subtotal:') . ' ',
'#price' => $subtotal,
'#cell_attributes' => array(
'colspan' => 'full',
'align' => 'right',
'class' => array('subtotal'),
),
),
);
return $table;
}
/*******************************************************************************
* Module and Helper Functions
******************************************************************************/
/**
* Returns the URL redirect for the continue shopping element on the cart page.
*
* @param $unset
* TRUE or FALSE indicating whether or not to unset the last URL variable.
*
* @return
* The URL or Drupal path that will be used for the continue shopping element.
*/
function uc_cart_continue_shopping_url($unset = TRUE) {
$url = '';
// Use the last URL if enabled and available.
if (variable_get('uc_continue_shopping_use_last_url', TRUE) && isset($_SESSION['uc_cart_last_url'])) {
$url = $_SESSION['uc_cart_last_url'];
}
// If the URL is still empty, fall back to the default.
if (empty($url)) {
$url = variable_get('uc_continue_shopping_url', '');
}
// Unset the last URL if specified.
if ($unset) {
unset($_SESSION['uc_cart_last_url']);
}
return $url;
}
/**
* Completes a sale, including adjusting order status and creating user account.
*
* @param $order
* The order object that has just been completed.
* @param $login
* Whether or not to login a new user when this function is called.
*
* @return
* The HTML text of the default order completion page.
*/
function uc_cart_complete_sale($order, $login = FALSE) {
global $user;
// Logic to create new user if necessary:
if ($order->uid == 0) {
// Check for an existing user account with the e-mail address from checkout.
$result = db_query("SELECT uid FROM {users} WHERE mail = :email", array(':email' => $order->primary_email));
// If it was found, update the order.
if ($account = $result->fetchObject()) {
$order->uid = $account->uid;
$account = user_load($account->uid);
db_update('uc_orders')
->fields(array('uid' => $order->uid))
->condition('order_id', $order->order_id)
->execute();
$message_type = 'existing_user';
}
else {
// Get a valid new username.
if (empty($order->data['new_user']['name'])) {
$name = uc_store_email_to_username($order->primary_email);
}
else {
$name = $order->data['new_user']['name'];
}
// Setup the account fields array and save it as a new user.
$fields = array(
'name' => $name,
'mail' => $order->primary_email,
'init' => $order->primary_email,
'pass' => empty($order->data['new_user']['pass']) ? user_password(variable_get('uc_pwd_length', 6)) : $order->data['new_user']['pass'],
'roles' => array(),
'status' => variable_get('uc_new_customer_status_active', TRUE) ? 1 : 0,
);
$account = user_save('', $fields);
// Send the customer their account details if enabled.
if (variable_get('uc_new_customer_email', TRUE)) {
// Manually set the password so it appears in the e-mail.
$account->password = $fields['pass'];
// Send the e-mail through the user module.
drupal_mail('user', 'register_no_approval_required', $order->primary_email, NULL, array('account' => $account), uc_store_email_from());
}
// Store the login details in the session for use on the page display.
$_SESSION['new_user'] = array('name' => $fields['name'], 'pass' => $fields['pass']);
// Update the order's uid in this request and in the database.
$order->uid = $account->uid;
unset($order->data['new_user']['pass']);
db_update('uc_orders')
->fields(array(
'uid' => $order->uid,
'data' => serialize($order->data),
))
->condition('order_id', $order->order_id)
->execute();
// Login the user if specified.
if ($login) {
$form_state = array('values' => $fields);
drupal_form_submit('user_login', $form_state);
}
$message_type = 'new_user';
}
}
else {
if ($order->uid == $user->uid) {
$message_type = 'logged_in';
$account = clone $user;
}
else {
$message_type = 'existing_user';
$account = user_load($order->uid);
}
}
$messages = array();
$messages[] = variable_get('uc_msg_order_submit', uc_get_message('completion_message'));
if ($message = variable_get('uc_msg_order_' . $message_type, uc_get_message('completion_' . $message_type))) {
if (isset($_SESSION['new_user'])) {
$variables['!new_username'] = check_plain($_SESSION['new_user']['name']);
$variables['!new_password'] = check_plain($_SESSION['new_user']['pass']);
$messages[] = strtr($message, $variables);
}
else {
$messages[] = $message;
}
}
$messages[] = variable_get('uc_msg_continue_shopping', uc_get_message('continue_shopping'));
$output_message = '';
foreach ($messages as $message) {
$message = filter_xss_admin(token_replace($message, array('uc_order' => $order)));
$output_message .= $message;
}
// Move an order's status from "In Checkout" to "Pending"
if (uc_order_status_data($order->order_status, 'state') == 'in_checkout') {
$status = uc_order_state_default('post_checkout');
if (uc_order_update_status($order->order_id, $status)) {
$order->order_status = $status;
}
}
// Empty that cart...
uc_cart_empty(uc_cart_get_id(FALSE), 'checkout');
// Clear our the session variables used to force the cart workflow.
unset($_SESSION['cart_order'], $_SESSION['do_complete'], $_SESSION['new_user']);
module_invoke_all('uc_checkout_complete', $order, $account);
if (module_exists('rules')) {
rules_invoke_event('uc_checkout_complete', $order);
}
return array(
'#theme' => 'uc_cart_complete_sale',
'#message' => $output_message,
);
}
/**
* Themes the sale completion page.
*
* @param $message
* Message containing order number info, account info, and link to continue
* shopping.
*
* @ingroup themeable
*/
function theme_uc_cart_complete_sale($variables) {
return $variables['message'];
}
/**
* Returns the unique cart_id of the user.
*
* @param $create
* Toggle auto creation of cart id.
*
* @return
* Cart id. If $create is FALSE will return FALSE if there is no cart id.
*/
function uc_cart_get_id($create = TRUE) {
global $user;
if ($user->uid) {
return $user->uid;
}
elseif (!isset($_SESSION['uc_cart_id']) && $create) {
$_SESSION['uc_cart_id'] = md5(uniqid(rand(), TRUE));
}
return isset($_SESSION['uc_cart_id']) ? $_SESSION['uc_cart_id'] : FALSE;
}
/**
* Grabs the items in a shopping cart for a user.
*
* If $cid is not passed in, this function uses the uid of the person currently
* accessing this function.
*/
function uc_cart_get_contents($cid = NULL, $action = NULL) {
static $items = array();
$cid = $cid ? $cid : uc_cart_get_id(FALSE);
// If we didn't get a cid, return empty.
if (!$cid) {
return array();
}
if ($action == 'rebuild') {
unset($items[$cid]);
}
if (!isset($items[$cid])) {
$items[$cid] = array();
$result = db_query("SELECT c.*, n.title, n.vid FROM {node} n INNER JOIN {uc_cart_products} c ON n.nid = c.nid WHERE c.cart_id = :id", array(':id' => $cid));
foreach ($result as $item) {
for ($i = 0; $i < count($items[$cid]); $i++) {
if ($items[$cid][$i]->nid == $item->nid && $items[$cid][$i]->data == $item->data) {
$items[$cid][$i]->qty += $item->qty;
continue 2;
}
}
$product = node_load($item->nid);
$item->cost = $product->cost;
$item->price = $product->sell_price;
$item->weight = $product->weight;
$item->weight_units = $product->weight_units;
$item->data = unserialize($item->data);
$item->module = $item->data['module'];
$item->model = $product->model;
// Invoke hook_uc_cart_item() with $op = 'load' in enabled modules.
foreach (module_implements('uc_cart_item') as $module) {
$func = $module . '_uc_cart_item';
if (function_exists($func)) {
// $item must be passed by reference.
$func('load', $item);
}
}
$items[$cid][] = $item;
}
// Allow other modules a chance to alter the fully loaded cart object.
drupal_alter('uc_cart', $items[$cid]);
// When there are no longer any items in the cart, the anonymous cart ID is no longer required.
// To guard against unsetting the session ID in the middle of an uc_cart_add_item() call, we only do this on rebuild
// See issue 858816 for details.
if ($action == 'rebuild' && empty($items[$cid]) && isset($_SESSION['uc_cart_id']) && $_SESSION['uc_cart_id'] == $cid) {
unset($_SESSION['uc_cart_id']);
}
}
return $items[$cid];
}
/**
* Returns the total number of items in the shopping cart.
*
* The total number of items in the cart isn't simply queried directly from the
* database, because when the shopping cart is loaded items may in fact be
* altered or removed. Hence we actually load the cart and tally up the total
* number of items from the fully loaded cart instead.
*
* @param $cid
* The cart ID of the shopping cart whose items we're totalling; defaults to
* the current user's cart.
*
* @return
* An integer representing the total number of items in the cart.
*/
function uc_cart_get_total_qty($cid = NULL) {
$qty = 0;
if (empty($cid)) {
$cid = uc_cart_get_id(FALSE);
}
if ($cid) {
foreach (uc_cart_get_contents($cid) as $item) {
$qty += $item->qty;
}
}
return $qty;
}
/**
* Allows us to get one single line item in a cart
*
* @param $cart_item_id
* cart_item_id to fetch.
*
* @return
* Fully loaded cart item or NULL if item not found.
*/
function uc_cart_get_item($cart_item_id) {
$result = db_query("SELECT c.*, n.title, n.vid FROM {node} n INNER JOIN {uc_cart_products} c ON n.nid = c.nid WHERE c.cart_item_id = :id", array(':id' => $cart_item_id));
$item = $result->fetchObject();
if (empty($item)) {
return;
}
$product = node_load($item->nid);
$item->cost = $product->cost;
$item->price = $product->sell_price;
$item->weight = $product->weight;
$item->data = unserialize($item->data);
$item->module = $item->data['module'];
$item->model = $product->model;
// Invoke hook_uc_cart_item() with $op = 'load' in enabled modules.
foreach (module_implements('uc_cart_item') as $module) {
$func = $module . '_uc_cart_item';
if (function_exists($func)) {
// $item must be passed by reference.
$func('load', $item);
}
}
return $item;
}
/**
* Updates a cart item.
*
* @param $item
* The loaded cart item.
*
* @return
* unknown_type
*/
function uc_cart_update_item($item) {
db_update('uc_cart_products')
->fields(array(
'qty' => $item->qty,
'changed' => REQUEST_TIME,
'data' => serialize($item->data),
))
->condition('cart_item_id', $item->cart_item_id)
->execute();
}
/**
* Adds an item to a user's cart.
*/
function uc_cart_add_item($nid, $qty = 1, $data = NULL, $cid = NULL, $msg = TRUE, $check_redirect = TRUE, $rebuild = TRUE) {
if (isset($_SESSION['cart_order'])) {
unset($_SESSION['cart_order']);
}
$cid = $cid ? $cid : uc_cart_get_id();
$node = node_load($nid);
if (is_null($data)) {
$data = array('module' => 'uc_product');
}
if (!isset($data['module'])) {
$data['module'] = 'uc_product';
}
if (!uc_product_is_product($node->type)) {
drupal_set_message(t('@title is not a product. Unable to add to cart.', array('@title' => $node->title)), 'error');
return;
}
$result = module_invoke_all('uc_add_to_cart', $nid, $qty, $data);
if (is_array($result) && !empty($result)) {
foreach ($result as $row) {
if ($row['success'] === FALSE) {
if (isset($row['message']) && !empty($row['message'])) {
$message = $row['message'];
}
else {
$message = t('Sorry, that item is not available for purchase at this time.');
}
if ($row['silent'] === TRUE) {
if ($check_redirect) {
if (isset($_GET['destination'])) {
drupal_goto();
}
$redirect = variable_get('uc_add_item_redirect', 'cart');
if ($redirect != '') {
$_SESSION['uc_cart_last_url'] = uc_referer_uri();
return $redirect;
}
else {
return uc_referer_uri();
}
}
}
else {
drupal_set_message($message, 'error');
}
return uc_referer_uri();
}
}
}
$item = db_query("SELECT * FROM {uc_cart_products} WHERE cart_id = :cart_id AND nid = :nid AND data = :data", array(':cart_id' => $cid, ':nid' => $node->nid, ':data' => serialize($data)))->fetchObject();
// If the item isn't in the cart yet, add it.
if (is_null($item) || $item === FALSE) {
db_insert('uc_cart_products')
->fields(array(
'cart_id' => $cid,
'nid' => $node->nid,
'qty' => $qty,
'changed' => REQUEST_TIME,
'data' => serialize($data),
))
->execute();
if ($msg) {
drupal_set_message(t('@product-title added to your shopping cart.', array('@product-title' => $node->title, '!url' => url('cart'))));
}
}
else {
// Update the item instead.
if ($msg) {
drupal_set_message(t('Your item(s) have been updated.'));
}
$qty += $item->qty;
module_invoke($data['module'], 'uc_update_cart_item', $node->nid, $data, min($qty, 999999), $cid);
}
// If specified, rebuild the cached cart items array.
if ($rebuild) {
uc_cart_get_contents($cid, 'rebuild');
}
if ($check_redirect) {
if (isset($_GET['destination'])) {
drupal_goto();
}
$redirect = variable_get('uc_add_item_redirect', 'cart');
if ($redirect != '') {
$_SESSION['uc_cart_last_url'] = uc_referer_uri();
return $redirect;
}
else {
return uc_referer_uri();
}
}
}
/**
* Removes an item from the cart
*
* @param $nid
* The node ID of the item to remove.
* @param $cid
* The cart ID of the item to remove.
* @param $data
* The data array for the item to remove.
* @param $op
* The $op parameter to pass to hook_cart_item(), if not the default 'remove'.
*/
function uc_cart_remove_item($nid, $cid = NULL, $data = array(), $op = 'remove') {
if (empty($nid)) {
return;
}
$cart_id = !(is_null($cid) || empty($cid)) ? $cid : uc_cart_get_id();
// Invoke hook_uc_cart_item() with $op = 'remove' in enabled modules.
$result = db_query("SELECT c.*, n.title, n.vid FROM {node} n INNER JOIN {uc_cart_products} c ON n.nid = c.nid WHERE c.cart_id = :cart_id AND c.nid = :nid AND c.data = :data", array(':cart_id' => $cart_id, ':nid' => $nid, ':data' => serialize($data)));
if ($item = $result->fetchObject()) {
foreach (module_implements('uc_cart_item') as $module) {
$func = $module . '_uc_cart_item';
if (function_exists($func)) {
// $item must be passed by reference.
$func($op, $item);
}
}
}
db_delete('uc_cart_products')
->condition('cart_id', $cart_id)
->condition('nid', $nid)
->condition('data', serialize($data))
->execute();
}
/**
* Updates the quantity of all the items in a cart object
*/
function uc_cart_update_item_object($cart) {
if (is_object($cart)) {
foreach ($cart->items as $item) {
module_invoke($item['module'], 'uc_update_cart_item', $item['nid'], unserialize($item['data']), $item['qty']);
}
// Rebuild the cached cart items.
uc_cart_get_contents(NULL, 'rebuild');
}
}
/**
* Empties a cart of its contents.
*
* @param $cart_id
* The ID of the cart.
* @param $op
* The $op parameter to pass to hook_cart_item(), if not the default 'remove'.
*/
function uc_cart_empty($cart_id, $op = 'remove') {
if (is_null($cart_id) || empty($cart_id)) {
return;
}
// Empty cart one item at a time. This will ensure the cart_item hook is fired with $op set to 'remove' for each item.
$items = uc_cart_get_contents($cart_id);
foreach ($items as $item) {
uc_cart_remove_item($item->nid, $cart_id, $item->data, $op);
}
// Probably don't need this query, but it will ensure anything not removed with the above loop is removed here.
db_delete('uc_cart_products')
->condition('cart_id', $cart_id)
->execute();
// Remove cached cart.
uc_cart_get_contents($cart_id, 'rebuild');
}
/**
* Gets all of the enabled, sorted cart panes.
*
* @param $items
* The contents of the cart.
* @param $action
* If 'rebuild' is passed, the static pane cache is cleared.
*/
function uc_cart_cart_pane_list($items, $action = NULL) {
static $panes;
if (count($panes) > 0 && $action !== 'rebuild') {
return $panes;
}
$panes = module_invoke_all('uc_cart_pane', $items);
// Allow other modules to alter the panes.
drupal_alter('uc_cart_pane', $panes, $items);
if (!is_array($panes) || count($panes) == 0) {
return array();
}
foreach ($panes as $i => $value) {
$panes[$i]['enabled'] = variable_get('uc_cap_' . $panes[$i]['id'] . '_enabled', (!isset($panes[$i]['enabled']) ? TRUE : $panes[$i]['enabled']));
$panes[$i]['weight'] = variable_get('uc_cap_' . $panes[$i]['id'] . '_weight', (!isset($panes[$i]['weight']) ? 0 : $panes[$i]['weight']));
}
usort($panes, 'uc_weight_sort');
return $panes;
}
/**
* Determines whether a cart contains shippable items or not.
*/
function uc_cart_is_shippable($cart_id = NULL, $action = '') {
static $shippable;
if ($action != 'refresh' && ($shippable === FALSE || $shippable === TRUE)) {
return $shippable;
}
$shippable = FALSE;
$items = uc_cart_get_contents($cart_id);
// Return FALSE if the cart is empty!
if (empty($items)) {
return FALSE;
}
foreach ($items as $item) {
$result[] = uc_cart_product_is_shippable($item);
}
// Return TRUE if any product in the cart is shippable.
if (in_array(TRUE, $result)) {
$shippable = TRUE;
return TRUE;
}
return FALSE;
}
/**
* Determines whether a product is shippable or not.
*/
function uc_cart_product_is_shippable($product) {
// Return FALSE if the product form specifies this as not shippable.
if ($product->data['shippable'] == FALSE) {
return FALSE;
}
$result = array();
// See if any other modules have a say in the matter...
foreach (module_implements('uc_cart_item') as $module) {
$func = $module . '_uc_cart_item';
if (function_exists($func)) {
// $product must be passed by reference.
$return = $func('can_ship', $product);
if (!is_null($return)) {
$result[] = $return;
}
}
}
// Return TRUE by default.
if (empty($result) || in_array(TRUE, $result)) {
return TRUE;
}
return FALSE;
}
/**
* Removes panes from the list that match the given conditions.
*
* @return
* A checkout pane array with panes filtered out that have key values
* matching the combinations in the $remove array.
*/
function uc_cart_filter_checkout_panes($panes, $remove = NULL) {
if (is_array($remove)) {
for ($i = 0, $j = count($panes); $i < $j; $i++) {
foreach ($remove as $key => $value) {
if (isset($panes[$i][$key]) and $panes[$i][$key] == $value) {
unset($panes[$i]);
}
}
}
}
return $panes;
}