uc_cart.module 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388
  1. <?php
  2. /**
  3. * @file
  4. * Handles all things concerning Ubercart's shopping cart.
  5. *
  6. * The Ubercart cart system functions much like the e-commerce cart at its base
  7. * level... in fact, most carts do. This module handles the cart display,
  8. * adding items to a cart, and checking out. The module enables the cart,
  9. * products, and checkout to be extensible.
  10. */
  11. require_once dirname(__FILE__) . '/uc_cart_checkout_pane.inc';
  12. /**
  13. * Time in seconds after which a cart order is deemed abandoned.
  14. */
  15. define('UC_CART_ORDER_TIMEOUT', 86400); // 24 hours
  16. /**
  17. * Time in seconds after which the checkout page is deemed abandoned.
  18. */
  19. define('UC_CART_CHECKOUT_TIMEOUT', 1800); // 30 minutes
  20. /**
  21. * Implements hook_menu().
  22. */
  23. function uc_cart_menu() {
  24. $items = array();
  25. $items['admin/store/settings/cart'] = array(
  26. 'title' => 'Cart',
  27. 'description' => 'Configure the cart page display and settings.',
  28. 'page callback' => 'drupal_get_form',
  29. 'page arguments' => array('uc_cart_cart_settings_form'),
  30. 'access arguments' => array('administer store'),
  31. 'file' => 'uc_cart.admin.inc',
  32. );
  33. $items['admin/store/settings/checkout'] = array(
  34. 'title' => 'Checkout',
  35. 'description' => 'Configure the checkout page display and completion rules.',
  36. 'page callback' => 'drupal_get_form',
  37. 'page arguments' => array('uc_cart_checkout_settings_form'),
  38. 'access arguments' => array('administer store'),
  39. 'file' => 'uc_cart.admin.inc',
  40. );
  41. $items['admin/store/settings/checkout/settings'] = array(
  42. 'title' => 'Settings',
  43. 'description' => 'Edit the basic checkout settings.',
  44. 'access arguments' => array('administer store'),
  45. 'type' => MENU_DEFAULT_LOCAL_TASK,
  46. );
  47. $items['admin/store/settings/checkout/rules'] = array(
  48. 'title' => 'Rules',
  49. 'description' => 'Edit the checkout rules.',
  50. 'page callback' => 'uc_cart_checkout_rules',
  51. 'access arguments' => array('administer store'),
  52. 'type' => MENU_LOCAL_TASK,
  53. 'weight' => 2,
  54. 'file' => 'uc_cart.admin.inc',
  55. );
  56. $items += rules_ui()->config_menu('admin/store/settings/checkout/rules');
  57. $items['cart'] = array(
  58. 'title' => 'Shopping cart',
  59. 'description' => 'View/modify the contents of your shopping cart or proceed to checkout.',
  60. 'page callback' => 'uc_cart_view',
  61. 'access arguments' => array('access content'),
  62. 'file' => 'uc_cart.pages.inc',
  63. );
  64. $items['cart/empty'] = array(
  65. 'title' => 'Empty cart',
  66. 'description' => 'Confirm that the cart should be emptied.',
  67. 'page callback' => 'drupal_get_form',
  68. 'page arguments' => array('uc_cart_empty_confirm'),
  69. 'access arguments' => array('access content'),
  70. 'type' => MENU_CALLBACK,
  71. 'file' => 'uc_cart.pages.inc',
  72. );
  73. $items['cart/checkout'] = array(
  74. 'title' => 'Checkout',
  75. 'description' => 'Purchase the items in your shopping cart.',
  76. 'page callback' => 'uc_cart_checkout',
  77. 'access arguments' => array('access content'),
  78. 'type' => MENU_CALLBACK,
  79. 'file' => 'uc_cart.pages.inc',
  80. );
  81. $items['cart/checkout/review'] = array(
  82. 'title' => 'Review order',
  83. 'description' => 'Review an order before final submission.',
  84. 'page callback' => 'uc_cart_checkout_review',
  85. 'access arguments' => array('access content'),
  86. 'type' => MENU_CALLBACK,
  87. 'file' => 'uc_cart.pages.inc',
  88. );
  89. $items['cart/checkout/complete'] = array(
  90. 'title' => 'Order complete',
  91. 'description' => 'Display information upon completion of an order.',
  92. 'page callback' => 'uc_cart_checkout_complete',
  93. 'access arguments' => array('access content'),
  94. 'type' => MENU_CALLBACK,
  95. 'file' => 'uc_cart.pages.inc',
  96. );
  97. return $items;
  98. }
  99. /**
  100. * Implements hook_image_default_styles().
  101. */
  102. function uc_cart_image_default_styles() {
  103. $styles = array();
  104. $styles['uc_cart'] = array(
  105. 'effects' => array(
  106. array(
  107. 'name' => 'image_scale',
  108. 'data' => array(
  109. 'width' => '50',
  110. 'height' => '50',
  111. 'upscale' => 0,
  112. ),
  113. 'weight' => '0',
  114. ),
  115. ),
  116. );
  117. return $styles;
  118. }
  119. /**
  120. * Implements hook_theme().
  121. */
  122. function uc_cart_theme() {
  123. return array(
  124. 'uc_cart_block_title' => array(
  125. 'variables' => array(
  126. 'title' => NULL,
  127. 'icon_class' => 'cart-empty',
  128. 'collapsible' => TRUE,
  129. 'collapsed' => TRUE,
  130. ),
  131. 'file' => 'uc_cart.theme.inc',
  132. ),
  133. 'uc_cart_block_title_icon' => array(
  134. 'variables' => array('icon_class' => NULL),
  135. 'file' => 'uc_cart.theme.inc',
  136. ),
  137. 'uc_cart_block_content' => array(
  138. 'variables' => array(
  139. 'help_text' => NULL,
  140. 'items' => NULL,
  141. 'item_count' => NULL,
  142. 'item_text' => NULL,
  143. 'total' => NULL,
  144. 'summary_links' => NULL,
  145. 'collapsed' => TRUE,
  146. ),
  147. 'file' => 'uc_cart.theme.inc',
  148. ),
  149. 'uc_cart_block_items' => array(
  150. 'variables' => array('items' => NULL, 'collapsed' => TRUE),
  151. 'file' => 'uc_cart.theme.inc',
  152. ),
  153. 'uc_cart_block_summary' => array(
  154. 'variables' => array(
  155. 'item_count' => NULL,
  156. 'item_text' => NULL,
  157. 'total' => NULL,
  158. 'summary_links' => NULL,
  159. ),
  160. 'file' => 'uc_cart.theme.inc',
  161. ),
  162. 'uc_cart_view_form' => array(
  163. 'render element' => 'form',
  164. 'file' => 'uc_cart.theme.inc',
  165. ),
  166. 'uc_cart_checkout_buttons' => array(
  167. 'render element' => 'buttons',
  168. 'file' => 'uc_cart.theme.inc',
  169. ),
  170. 'uc_empty_cart' => array(
  171. 'variables' => array(),
  172. 'file' => 'uc_cart.theme.inc',
  173. ),
  174. 'uc_cart_checkout_form' => array(
  175. 'render element' => 'form',
  176. 'file' => 'uc_cart.pages.inc',
  177. ),
  178. 'uc_cart_review_table' => array(
  179. 'variables' => array('items' => array(), 'show_subtotal' => TRUE),
  180. 'file' => 'uc_cart_checkout_pane.inc',
  181. ),
  182. 'uc_cart_checkout_review' => array(
  183. 'variables' => array('panes' => NULL, 'form' => NULL),
  184. 'file' => 'uc_cart.pages.inc',
  185. ),
  186. 'uc_checkout_pane_cart_review' => array(
  187. 'variables' => array('items' => array()),
  188. 'file' => 'uc_cart_checkout_pane.inc',
  189. ),
  190. 'uc_cart_complete_sale' => array(
  191. 'variables' => array('message' => '', 'order' => NULL),
  192. 'file' => 'uc_cart.theme.inc',
  193. ),
  194. );
  195. }
  196. /**
  197. * Implements hook_init().
  198. */
  199. function uc_cart_init() {
  200. global $conf;
  201. $conf['i18n_variables'][] = 'uc_cart_breadcrumb_text';
  202. }
  203. /**
  204. * Implements hook_cron().
  205. */
  206. function uc_cart_cron() {
  207. // Empty anonymous carts.
  208. $time = strtotime(variable_get('uc_cart_anon_duration', '4') . ' ' . variable_get('uc_cart_anon_unit', 'hours') . ' ago');
  209. $result = db_query("SELECT DISTINCT cart_id FROM {uc_cart_products} WHERE changed <= :changed", array(':changed' => $time));
  210. foreach ($result as $row) {
  211. if (strlen($row->cart_id) >= 22) {
  212. uc_cart_empty($row->cart_id);
  213. }
  214. }
  215. // Empty authenticated carts.
  216. $time = strtotime(variable_get('uc_cart_auth_duration', '1') . ' ' . variable_get('uc_cart_auth_unit', 'years') . ' ago');
  217. $result = db_query("SELECT DISTINCT cart_id FROM {uc_cart_products} WHERE changed <= :changed", array(':changed' => $time));
  218. foreach ($result as $row) {
  219. if (strlen($row->cart_id) < 22) {
  220. uc_cart_empty($row->cart_id);
  221. }
  222. }
  223. // Update status of abandoned orders.
  224. $result = db_query("SELECT order_id FROM {uc_orders} WHERE order_status = :status AND modified < :time",
  225. array(':status' => 'in_checkout', ':time' => REQUEST_TIME - UC_CART_ORDER_TIMEOUT))->fetchCol();
  226. foreach ($result as $order_id) {
  227. uc_order_update_status($order_id, 'abandoned');
  228. }
  229. }
  230. /**
  231. * Implements hook_node_delete().
  232. */
  233. function uc_cart_node_delete($node) {
  234. if (uc_product_is_product($node->type)) {
  235. $efq = new EntityFieldQuery();
  236. $result = $efq->entityCondition('entity_type', 'uc_cart_item')
  237. ->propertyCondition('nid', $node->nid)
  238. ->execute();
  239. if (!empty($result['uc_cart_item'])) {
  240. entity_delete_multiple('uc_cart_item', array_keys($result['uc_cart_item']));
  241. }
  242. }
  243. }
  244. /**
  245. * Implements hook_user_load().
  246. */
  247. function uc_cart_user_load($users) {
  248. if (request_uri() == '/user/register?destination=cart/checkout') {
  249. foreach ($users as $account) {
  250. if ($account->uid != 0) {
  251. // Add items from an anonymous cart to a user's permanent cart on login.
  252. uc_cart_login_update($account->uid);
  253. }
  254. }
  255. }
  256. }
  257. /**
  258. * Implements hook_user_login().
  259. */
  260. function uc_cart_user_login(&$edit, $account) {
  261. // Add items from an anonymous cart to a user's permanent cart on login.
  262. uc_cart_login_update($account->uid);
  263. }
  264. /**
  265. * Implements hook_block_info().
  266. */
  267. function uc_cart_block_info() {
  268. $blocks = array();
  269. $blocks['cart'] = array(
  270. 'info' => t('Shopping cart'),
  271. 'cache' => DRUPAL_NO_CACHE,
  272. 'pages' => 'admin*',
  273. );
  274. return $blocks;
  275. }
  276. /**
  277. * Implements hook_block_view().
  278. */
  279. function uc_cart_block_view($delta = '') {
  280. global $user;
  281. if ($delta == 'cart') {
  282. $product_count = count(uc_cart_get_contents());
  283. // Display nothing if the block is set to hide on empty and there are no
  284. // items in the cart.
  285. if (!variable_get('uc_cart_block_empty_hide', FALSE) || $product_count) {
  286. // Add the cart block CSS.
  287. drupal_add_css(drupal_get_path('module', 'uc_cart') . '/uc_cart_block.css');
  288. // If the block is collapsible, add the appropriate JS.
  289. if (variable_get('uc_cart_block_collapsible', TRUE)) {
  290. drupal_add_js(drupal_get_path('module', 'uc_cart') . '/uc_cart_block.js');
  291. }
  292. // First build the help text.
  293. $help_text = FALSE;
  294. // uc_cart_help_text variable is only here for backward compatibility.
  295. if (variable_get('uc_cart_show_help_text', FALSE) && ($text = variable_get('uc_cart_help_text', t('Click title to display cart contents.')))) {
  296. $help_text = check_plain($text);
  297. }
  298. $items = FALSE;
  299. $item_count = 0;
  300. $total = 0;
  301. if ($product_count) {
  302. $display_items = entity_view('uc_cart_item', uc_cart_get_contents(), 'cart');
  303. foreach (element_children($display_items['uc_cart_item']) as $key) {
  304. $display_item = $display_items['uc_cart_item'][$key];
  305. if (count(element_children($display_item))) {
  306. $items[] = array(
  307. 'nid' => $display_item['nid']['#value'],
  308. 'qty' => theme('uc_qty', array('qty' => $display_item['qty']['#default_value'])),
  309. 'title' => $display_item['title']['#markup'],
  310. 'price' => $display_item['#total'],
  311. 'desc' => isset($display_item['description']['#markup']) ? $display_item['description']['#markup'] : FALSE,
  312. );
  313. $total += $display_item['#total'];
  314. $item_count += $display_item['qty']['#default_value'];
  315. }
  316. }
  317. }
  318. // Build the item count text and cart links.
  319. $item_text = format_plural($item_count, '<span class="num-items">1</span> Item', '<span class="num-items">@count</span> Items');
  320. $summary_links = array(
  321. 'cart-block-view-cart' => array(
  322. 'title' => t('View cart'),
  323. 'href' => 'cart',
  324. 'attributes' => array('rel' => 'nofollow'),
  325. ),
  326. );
  327. // Only add the checkout link if checkout is enabled.
  328. if (variable_get('uc_checkout_enabled', TRUE)) {
  329. $summary_links['cart-block-checkout'] = array(
  330. 'title' => t('Checkout'),
  331. 'href' => 'cart/checkout',
  332. 'attributes' => array('rel' => 'nofollow'),
  333. );
  334. }
  335. $block['subject'] = t('Shopping cart');
  336. $block['content'] = theme('uc_cart_block_content', array(
  337. 'help_text' => $help_text,
  338. 'items' => $items,
  339. 'item_count' => $item_count,
  340. 'item_text' => $item_text,
  341. 'total' => $total,
  342. 'summary_links' => $summary_links,
  343. 'collapsed' => variable_get('uc_cart_block_collapsed', TRUE),
  344. ));
  345. return $block;
  346. }
  347. }
  348. }
  349. /**
  350. * Implements hook_block_configure().
  351. *
  352. * Builds the settings form used by the shopping cart block.
  353. */
  354. function uc_cart_block_configure($delta = '') {
  355. if ($delta == 'cart') {
  356. $form['uc_cart_block_empty_hide'] = array(
  357. '#type' => 'checkbox',
  358. '#title' => t('Hide block if cart is empty.'),
  359. '#default_value' => variable_get('uc_cart_block_empty_hide', FALSE),
  360. );
  361. $form['uc_cart_block_image'] = array(
  362. '#type' => 'checkbox',
  363. '#title' => t('Display the shopping cart icon in the block title.'),
  364. '#default_value' => variable_get('uc_cart_block_image', TRUE),
  365. );
  366. $form['uc_cart_block_collapsible'] = array(
  367. '#type' => 'checkbox',
  368. '#title' => t('Make the shopping cart block collapsible by clicking the name or arrow.'),
  369. '#default_value' => variable_get('uc_cart_block_collapsible', TRUE),
  370. );
  371. $form['uc_cart_block_collapsed'] = array(
  372. '#type' => 'checkbox',
  373. '#title' => t('Display the shopping cart block collapsed by default.'),
  374. '#default_value' => variable_get('uc_cart_block_collapsed', TRUE),
  375. );
  376. $form['uc_cart_show_help_text'] = array(
  377. '#type' => 'checkbox',
  378. '#title' => t('Display small help text in the shopping cart block.'),
  379. '#default_value' => variable_get('uc_cart_show_help_text', FALSE),
  380. );
  381. return $form;
  382. }
  383. }
  384. /**
  385. * Implements hook_block_save().
  386. *
  387. * Saves the shopping cart block settings.
  388. */
  389. function uc_cart_block_save($delta = '', $edit = array()) {
  390. if ($delta == 'cart') {
  391. variable_set('uc_cart_block_empty_hide', $edit['uc_cart_block_empty_hide']);
  392. variable_set('uc_cart_block_image', $edit['uc_cart_block_image']);
  393. variable_set('uc_cart_block_collapsible', $edit['uc_cart_block_collapsible']);
  394. variable_set('uc_cart_block_collapsed', $edit['uc_cart_block_collapsed']);
  395. variable_set('uc_cart_show_help_text', $edit['uc_cart_show_help_text']);
  396. }
  397. }
  398. /**
  399. * Preprocesses the cart block output to include the icon.
  400. */
  401. function uc_cart_preprocess_block(&$variables) {
  402. global $user;
  403. if ($variables['block']->module == 'uc_cart' && $variables['block']->delta == 0 && $variables['block']->subject) {
  404. // Build the cart image if enabled.
  405. if (variable_get('uc_cart_block_image', TRUE)) {
  406. $product_count = count(uc_cart_get_contents());
  407. if (!$product_count) {
  408. // Use the "empty" cart icon.
  409. $icon_class = 'cart-block-icon-empty';
  410. }
  411. else {
  412. // Otherwise use the "full" cart icon.
  413. $icon_class = 'cart-block-icon-full';
  414. }
  415. }
  416. else {
  417. $icon_class = FALSE;
  418. }
  419. $variables['block']->subject = theme('uc_cart_block_title', array(
  420. 'title' => $variables['block']->subject,
  421. 'icon_class' => $icon_class,
  422. 'collapsible' => variable_get('uc_cart_block_collapsible', TRUE),
  423. 'collapsed' => variable_get('uc_cart_block_collapsed', TRUE),
  424. ));
  425. }
  426. }
  427. /**
  428. * Implements hook_views_api().
  429. */
  430. function uc_cart_views_api() {
  431. return array(
  432. 'api' => '2.0',
  433. 'path' => drupal_get_path('module', 'uc_cart') . '/views',
  434. );
  435. }
  436. /**
  437. * Implements hook_uc_message().
  438. */
  439. function uc_cart_uc_message() {
  440. global $user;
  441. $messages['checkout_instructions'] = '';
  442. $messages['review_instructions'] = '<p>' . 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.") . '</p>';
  443. $messages['completion_message'] = '<p>' . t('Your order is complete! Your order number is [uc_order:order-number].') . '</p>';
  444. $messages['completion_logged_in'] = '<p>' . t('Thank you for shopping at [store:name]. While logged in, you may continue shopping or <a href="[uc_order:url]">view your current order status</a> and order history.') . '</p>';
  445. $messages['completion_existing_user'] = '<p>' . 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<br /><br />\n<a href=\"!user_url\">Login</a> 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' => '[site:login-url]')) . '</p>';
  446. $messages['completion_new_user'] = '<p>' . 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<br /><br />\n<a href=\"!user_url\">Login</a> to your new account using the following information:\n<br /><br />\n<strong>Username:</strong> !new_username<br />\n<strong>Password:</strong> !new_password", array('!user_url' => '[site:login-url]')) . '</p>';
  447. $messages['completion_new_user_logged_in'] = '<p>' . 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<br /><br />\nYour password and further instructions have been sent to your e-mail address.\n<br /><br />\nFor your convenience, you are already logged in with your newly created account.") . '</p>';
  448. $messages['continue_shopping'] = '<p>' . t('<a href="[site:url]">Return to the front page.</a>') . '</p>';
  449. return $messages;
  450. }
  451. /**
  452. * Implements hook_uc_cart_pane().
  453. */
  454. function uc_cart_uc_cart_pane($items) {
  455. $body = array();
  456. if (!is_null($items)) {
  457. $body = drupal_get_form('uc_cart_view_form', $items) + array(
  458. '#prefix' => '<div id="cart-form-pane">',
  459. '#suffix' => '</div>',
  460. );
  461. }
  462. $panes['cart_form'] = array(
  463. 'title' => t('Default cart form'),
  464. 'enabled' => TRUE,
  465. 'weight' => 0,
  466. 'body' => $body,
  467. );
  468. return $panes;
  469. }
  470. /**
  471. * Implements hook_uc_checkout_pane().
  472. */
  473. function uc_cart_uc_checkout_pane() {
  474. $panes['cart'] = array(
  475. 'callback' => 'uc_checkout_pane_cart',
  476. 'title' => t('Cart contents'),
  477. 'desc' => t("Display the contents of a customer's shopping cart."),
  478. 'weight' => 1,
  479. 'process' => FALSE,
  480. 'collapsible' => FALSE,
  481. );
  482. $panes['customer'] = array(
  483. 'callback' => 'uc_checkout_pane_customer',
  484. 'title' => t('Customer information'),
  485. 'desc' => t('Get the necessary information to create a customer on the site.'),
  486. 'weight' => 2,
  487. );
  488. $panes['delivery'] = array(
  489. 'callback' => 'uc_checkout_pane_delivery',
  490. 'title' => t('Delivery information'),
  491. 'desc' => t('Get the information for where the order needs to ship.'),
  492. 'weight' => 3,
  493. 'shippable' => TRUE,
  494. );
  495. $panes['billing'] = array(
  496. 'callback' => 'uc_checkout_pane_billing',
  497. 'title' => t('Billing information'),
  498. 'desc' => t('Get basic information needed to collect payment.'),
  499. 'weight' => 4,
  500. );
  501. $panes['comments'] = array(
  502. 'callback' => 'uc_checkout_pane_comments',
  503. 'title' => t('Order comments'),
  504. 'desc' => t('Allow a customer to put comments on an order.'),
  505. 'weight' => 7,
  506. );
  507. return $panes;
  508. }
  509. /**
  510. * Updates a user's cart to include items from their anonymous session.
  511. */
  512. function uc_cart_login_update($uid) {
  513. if (!isset($_SESSION['uc_cart_id'])) {
  514. return;
  515. }
  516. // If there are items in the anonymous cart, consolidate them.
  517. if ($items = uc_cart_get_contents($_SESSION['uc_cart_id'])) {
  518. // Remove the anonymous cart items.
  519. uc_cart_empty($_SESSION['uc_cart_id']);
  520. // Merge the anonymous items into the user cart.
  521. foreach ($items as $key => $item) {
  522. uc_cart_add_item($item->nid, $item->qty, $item->data, $uid, FALSE, FALSE, FALSE);
  523. }
  524. // Unset the anonymous cart ID, it's no longer needed.
  525. unset($_SESSION['uc_cart_id']);
  526. }
  527. }
  528. /**
  529. * Displays the contents of the customer's cart.
  530. *
  531. * Handles simple or complex objects. Some cart items may have a list of
  532. * products that they represent. These are displayed but are not able to
  533. * be changed by the customer.
  534. *
  535. * @see uc_cart_view_form_submit()
  536. * @see uc_cart_view_form_continue_shopping()
  537. * @see uc_cart_view_form_checkout()
  538. * @see theme_uc_cart_view_form()
  539. * @see uc_cart_view_table()
  540. *
  541. * @ingroup forms
  542. */
  543. function uc_cart_view_form($form, &$form_state, $items = NULL) {
  544. $form['#attached']['css'][] = drupal_get_path('module', 'uc_cart') . '/uc_cart.css';
  545. $form['items'] = array(
  546. '#type' => 'tapir_table',
  547. '#tree' => TRUE,
  548. );
  549. $i = 0;
  550. $display_items = entity_view('uc_cart_item', $items, 'cart', NULL, TRUE);
  551. foreach (element_children($display_items['uc_cart_item']) as $key) {
  552. $display_item = $display_items['uc_cart_item'][$key];
  553. if (count(element_children($display_item))) {
  554. $form['items'][$i] = $display_item;
  555. $form['items'][$i]['image'] = uc_product_get_picture($display_item['nid']['#value'], 'uc_cart');
  556. $description = $display_item['title']['#markup'] . $display_item['description']['#markup'];
  557. $form['items'][$i]['desc']['#markup'] = $description;
  558. if (isset($form['items'][$i]['remove'])) {
  559. // Backward compatibility with old checkbox method.
  560. if ($form['items'][$i]['remove']['#type'] == 'checkbox') {
  561. $form['items'][$i]['remove'] = array('#type' => 'submit', '#value' => t('Remove'));
  562. }
  563. $form['items'][$i]['remove']['#name'] = 'remove-' . $i;
  564. }
  565. $form['items'][$i]['title']['#type'] = 'value';
  566. $form['items'][$i]['description']['#type'] = 'value';
  567. if (empty($display_item['qty'])) {
  568. $form['items'][$i]['qty'] = array(
  569. '#type' => 'hidden',
  570. '#value' => 0,
  571. );
  572. }
  573. $form['items'][$i]['total'] = array(
  574. '#theme' => 'uc_price',
  575. '#price' => $display_item['#total'],
  576. );
  577. if (!empty($display_item['#suffixes'])) {
  578. $form['items'][$i]['total']['#suffixes'] = $display_item['#suffixes'];
  579. }
  580. }
  581. $i++;
  582. }
  583. $form['items'] = tapir_get_table('uc_cart_view_table', $form['items']);
  584. $form['actions'] = array('#type' => 'actions');
  585. // If the continue shopping element is enabled...
  586. if (($cs_type = variable_get('uc_continue_shopping_type', 'link')) !== 'none') {
  587. // Add the element to the form based on the element type.
  588. if (variable_get('uc_continue_shopping_type', 'link') == 'link') {
  589. $form['actions']['continue_shopping'] = array(
  590. '#markup' => l(t('Continue shopping'), uc_cart_continue_shopping_url()),
  591. );
  592. }
  593. elseif (variable_get('uc_continue_shopping_type', 'link') == 'button') {
  594. $form['actions']['continue_shopping'] = array(
  595. '#type' => 'submit',
  596. '#value' => t('Continue shopping'),
  597. '#submit' => array('uc_cart_view_form_submit', 'uc_cart_view_form_continue_shopping'),
  598. );
  599. }
  600. }
  601. // Add the empty cart button if enabled.
  602. if (variable_get('uc_cart_empty_button', FALSE)) {
  603. $form['actions']['empty'] = array(
  604. '#type' => 'submit',
  605. '#value' => t('Empty cart'),
  606. '#submit' => array('uc_cart_view_form_empty'),
  607. );
  608. }
  609. // Add the control buttons for updating and proceeding to checkout.
  610. $form['actions']['update'] = array(
  611. '#type' => 'submit',
  612. '#name' => 'update-cart',
  613. '#value' => t('Update cart'),
  614. '#submit' => array('uc_cart_view_form_submit', 'uc_cart_view_form_update_message'),
  615. );
  616. $form['actions']['checkout'] = array(
  617. '#theme' => 'uc_cart_checkout_buttons',
  618. );
  619. if (variable_get('uc_checkout_enabled', TRUE)) {
  620. $form['actions']['checkout']['checkout'] = array(
  621. '#type' => 'submit',
  622. '#value' => t('Checkout'),
  623. '#submit' => array('uc_cart_view_form_submit', 'uc_cart_view_form_checkout'),
  624. );
  625. }
  626. return $form;
  627. }
  628. /**
  629. * Default submit handler for uc_cart_view_form().
  630. *
  631. * @see uc_cart_view_form()
  632. */
  633. function uc_cart_view_form_submit($form, &$form_state) {
  634. // If a remove button was clicked, set the quantity for that item to 0.
  635. if (substr($form_state['triggering_element']['#name'], 0, 7) == 'remove-') {
  636. $item = substr($form_state['triggering_element']['#name'], 7);
  637. $form_state['values']['items'][$item]['qty'] = 0;
  638. drupal_set_message(t('<strong>!product-title</strong> removed from your shopping cart.', array('!product-title' => $form['items'][$item]['title']['#markup'])));
  639. }
  640. // Update the items in the shopping cart based on the form values, but only
  641. // if a qty has changed.
  642. foreach ($form['items'] as $key => $item) {
  643. if (isset($item['qty']['#default_value']) && $item['qty']['#default_value'] != $form_state['values']['items'][$key]['qty']) {
  644. uc_cart_update_item_object((object) $form_state['values']);
  645. }
  646. }
  647. }
  648. /**
  649. * Displays "cart updated" message for uc_cart_view_form().
  650. *
  651. * @see uc_cart_view_form()
  652. */
  653. function uc_cart_view_form_update_message($form, &$form_state) {
  654. drupal_set_message(t('Your cart has been updated.'));
  655. }
  656. /**
  657. * Continue shopping redirect for uc_cart_view_form().
  658. *
  659. * @see uc_cart_view_form()
  660. */
  661. function uc_cart_view_form_continue_shopping($form, &$form_state) {
  662. $form_state['redirect'] = uc_cart_continue_shopping_url();
  663. }
  664. /**
  665. * Empty cart redirect for uc_cart_view_form().
  666. *
  667. * @see uc_cart_view_form()
  668. */
  669. function uc_cart_view_form_empty($form, &$form_state) {
  670. $form_state['redirect'] = 'cart/empty';
  671. }
  672. /**
  673. * Checkout redirect for uc_cart_view_form().
  674. *
  675. * @see uc_cart_view_form()
  676. */
  677. function uc_cart_view_form_checkout($form, &$form_state) {
  678. $form_state['redirect'] = 'cart/checkout';
  679. }
  680. /**
  681. * Lists the products in the cart in a TAPIr table.
  682. */
  683. function uc_cart_view_table($table) {
  684. $table['#columns'] = array(
  685. 'remove' => array(
  686. 'cell' => t('Remove'),
  687. 'weight' => 0,
  688. ),
  689. 'image' => array(
  690. 'cell' => t('Products'),
  691. 'weight' => 1,
  692. ),
  693. 'desc' => array(
  694. 'cell' => '',
  695. 'weight' => 2,
  696. ),
  697. 'qty' => array(
  698. 'cell' => theme('uc_qty_label'),
  699. 'weight' => 3,
  700. ),
  701. 'total' => array(
  702. 'cell' => t('Total'),
  703. 'weight' => 4,
  704. ),
  705. );
  706. $subtotal = 0;
  707. foreach (element_children($table) as $i) {
  708. $subtotal += $table[$i]['#total'];
  709. $table[$i]['remove']['#cell_attributes'] = array('class' => array('remove'));
  710. $table[$i]['image']['#cell_attributes'] = array('class' => array('image'));
  711. $table[$i]['desc']['#cell_attributes'] = array('class' => array('desc'));
  712. $table[$i]['qty']['#cell_attributes'] = array('class' => array('qty'));
  713. $table[$i]['total']['#cell_attributes'] = array('class' => array('price'));
  714. }
  715. $table[] = array(
  716. 'total' => array(
  717. '#theme' => 'uc_price',
  718. '#prefix' => '<span id="subtotal-title">' . t('Subtotal:') . '</span> ',
  719. '#price' => $subtotal,
  720. '#cell_attributes' => array(
  721. 'colspan' => 'full',
  722. 'class' => array('subtotal'),
  723. ),
  724. ),
  725. );
  726. return $table;
  727. }
  728. /**
  729. * Returns the URL redirect for the continue shopping element on the cart page.
  730. *
  731. * @param bool $unset
  732. * TRUE or FALSE indicating whether or not to unset the last URL variable.
  733. *
  734. * @return string
  735. * The URL or Drupal path that will be used for the continue shopping element.
  736. */
  737. function uc_cart_continue_shopping_url($unset = TRUE) {
  738. $url = '';
  739. // Use the last URL if enabled and available.
  740. if (variable_get('uc_continue_shopping_use_last_url', TRUE) && isset($_SESSION['uc_cart_last_url'])) {
  741. $url = $_SESSION['uc_cart_last_url'];
  742. }
  743. // If the URL is still empty, fall back to the default.
  744. if (empty($url)) {
  745. $url = variable_get('uc_continue_shopping_url', '');
  746. }
  747. // Unset the last URL if specified.
  748. if ($unset) {
  749. unset($_SESSION['uc_cart_last_url']);
  750. }
  751. return $url;
  752. }
  753. /**
  754. * Completes a sale, including adjusting order status and creating user account.
  755. *
  756. * @param $order
  757. * The order object that has just been completed.
  758. * @param $login
  759. * Whether or not to login a new user when this function is called.
  760. *
  761. * @return
  762. * The HTML text of the default order completion page.
  763. */
  764. function uc_cart_complete_sale($order, $login = FALSE) {
  765. global $user;
  766. // Ensure we have the latest order data.
  767. $order->data = unserialize(db_query("SELECT data FROM {uc_orders} WHERE order_id = :order_id", array(':order_id' => $order->order_id))->fetchField());
  768. // Ensure that user creation and triggers are only run once.
  769. if (empty($order->data['complete_sale'])) {
  770. uc_cart_complete_sale_account($order);
  771. // Store account data.
  772. db_update('uc_orders')
  773. ->fields(array(
  774. 'uid' => $order->uid,
  775. 'data' => serialize($order->data),
  776. ))
  777. ->condition('order_id', $order->order_id)
  778. ->execute();
  779. // Move an order's status from "In checkout" to "Pending".
  780. $status = db_query("SELECT order_status FROM {uc_orders} WHERE order_id = :order_id", array(':order_id' => $order->order_id))->fetchField();
  781. if (uc_order_status_data($status, 'state') == 'in_checkout') {
  782. $status = uc_order_state_default('post_checkout');
  783. if (uc_order_update_status($order->order_id, $status)) {
  784. $order->order_status = $status;
  785. }
  786. }
  787. // Invoke the checkout complete trigger and hook.
  788. $account = user_load($order->uid);
  789. module_invoke_all('uc_checkout_complete', $order, $account);
  790. rules_invoke_event('uc_checkout_complete', $order);
  791. }
  792. $type = $order->data['complete_sale'];
  793. // Log in new users, if requested.
  794. if ($type == 'new_user' && $login && !$user->uid) {
  795. $type = 'new_user_logged_in';
  796. $user = user_load($order->uid);
  797. $edit = array();
  798. user_login_finalize($edit);
  799. }
  800. $variables['!new_username'] = isset($order->data['new_user']['name']) ? $order->data['new_user']['name'] : '';
  801. $variables['!new_password'] = isset($order->password) ? $order->password : t('Your password');
  802. $messages = array(
  803. 'uc_msg_order_submit' => uc_get_message('completion_message'),
  804. 'uc_msg_order_' . $type => uc_get_message('completion_' . $type),
  805. 'uc_msg_continue_shopping' => uc_get_message('continue_shopping'),
  806. );
  807. foreach ($messages as $id => &$message) {
  808. $message = variable_get($id, $message);
  809. $message = token_replace($message, array('uc_order' => $order));
  810. if ($id == 'uc_msg_order_' . $type) {
  811. $message = strtr($message, $variables);
  812. }
  813. }
  814. $output = filter_xss_admin(implode(' ', $messages));
  815. // Empty that cart...
  816. uc_cart_empty();
  817. return array(
  818. '#theme' => 'uc_cart_complete_sale',
  819. '#message' => $output,
  820. '#order' => $order,
  821. );
  822. }
  823. /**
  824. * Link a completed sale to a user.
  825. *
  826. * @param $order
  827. * The order object that has just been completed.
  828. */
  829. function uc_cart_complete_sale_account($order) {
  830. // Order already has a user ID, so the user was logged in during checkout.
  831. if ($order->uid) {
  832. $order->data['complete_sale'] = 'logged_in';
  833. return;
  834. }
  835. $result = db_query("SELECT uid FROM {users} WHERE mail LIKE :email", array(':email' => $order->primary_email));
  836. // Email address matches an existing account.
  837. if ($account = $result->fetchObject()) {
  838. $order->uid = $account->uid;
  839. $order->data['complete_sale'] = 'existing_user';
  840. return;
  841. }
  842. // Set up a new user.
  843. $fields = array(
  844. 'name' => uc_store_email_to_username($order->primary_email),
  845. 'mail' => $order->primary_email,
  846. 'init' => $order->primary_email,
  847. 'pass' => user_password(),
  848. 'roles' => array(),
  849. 'status' => variable_get('uc_new_customer_status_active', TRUE) ? 1 : 0,
  850. );
  851. // Override the username, if specified.
  852. if (isset($order->data['new_user']['name'])) {
  853. $fields['name'] = $order->data['new_user']['name'];
  854. }
  855. // Create the account.
  856. $account = user_save('', $fields);
  857. // Override the password, if specified.
  858. if (isset($order->data['new_user']['hash'])) {
  859. db_query("UPDATE {users} SET pass = :hash WHERE uid = :uid", array(':hash' => $order->data['new_user']['hash'], ':uid' => $account->uid));
  860. $account->password = t('Your password');
  861. }
  862. else {
  863. $account->password = $fields['pass'];
  864. $order->password = $fields['pass'];
  865. }
  866. // Send the customer their account details if enabled.
  867. if (variable_get('uc_new_customer_email', TRUE)) {
  868. $type = variable_get('uc_new_customer_status_active', TRUE) ? 'register_no_approval_required' : 'register_pending_approval';
  869. drupal_mail('user', $type, $order->primary_email, uc_store_mail_recipient_language($order->primary_email), array('account' => $account), uc_store_email_from());
  870. }
  871. $order->uid = $account->uid;
  872. $order->data['new_user']['name'] = $fields['name'];
  873. $order->data['complete_sale'] = 'new_user';
  874. }
  875. /**
  876. * Returns the unique cart_id of the user.
  877. *
  878. * @param bool $create
  879. * Toggle auto creation of cart id.
  880. *
  881. * @return
  882. * Cart id. If $create is FALSE will return FALSE if there is no cart id.
  883. */
  884. function uc_cart_get_id($create = TRUE) {
  885. global $user;
  886. if ($user->uid) {
  887. return $user->uid;
  888. }
  889. elseif (!isset($_SESSION['uc_cart_id']) && $create) {
  890. $_SESSION['uc_cart_id'] = md5(uniqid(rand(), TRUE));
  891. }
  892. return isset($_SESSION['uc_cart_id']) ? $_SESSION['uc_cart_id'] : FALSE;
  893. }
  894. /**
  895. * Grabs the items in a shopping cart for a user.
  896. *
  897. * @param $cid
  898. * (optional) The cart ID to load, or NULL to load the current user's cart.
  899. * @param $action
  900. * (optional) Carts are statically cached by default. If set to "rebuild",
  901. * the cache will be ignored and the cart reloaded from the database.
  902. *
  903. * @return array
  904. * An array of cart items.
  905. */
  906. function uc_cart_get_contents($cid = NULL, $action = NULL) {
  907. static $items = array();
  908. $cid = $cid ? $cid : uc_cart_get_id(FALSE);
  909. // If we didn't get a cid, return empty.
  910. if (!$cid) {
  911. return array();
  912. }
  913. if ($action == 'rebuild' || $action == 'empty') {
  914. unset($items[$cid]);
  915. // Mark the current cart order (if any) as needing to be rebuilt. We only
  916. // do this if the cart is being explicitly rebuilt (i.e. after an item is
  917. // added, removed or altered).
  918. $_SESSION['uc_cart_order_rebuild'] = TRUE;
  919. }
  920. if (!isset($items[$cid])) {
  921. // Find all cart items associated with this cart.
  922. if ($action != 'empty') {
  923. $efq = new EntityFieldQuery();
  924. $result = $efq->entityCondition('entity_type', 'uc_cart_item')
  925. ->propertyCondition('cart_id', $cid)
  926. ->propertyOrderBy('cart_item_id', 'ASC')
  927. ->execute();
  928. if (!empty($result['uc_cart_item'])) {
  929. $items[$cid] = entity_load('uc_cart_item', array_keys($result['uc_cart_item']), NULL, TRUE);
  930. // Create a bare order and attach it to each item as context.
  931. $order = new UcOrder($cid);
  932. foreach ($items[$cid] as $item) {
  933. $item->order = $order;
  934. }
  935. }
  936. else {
  937. $items[$cid] = array();
  938. }
  939. }
  940. else {
  941. $items[$cid] = array();
  942. }
  943. // Allow other modules a chance to alter the fully loaded cart object.
  944. drupal_alter('uc_cart', $items[$cid]);
  945. // When there are no longer any items in the cart, the anonymous cart ID is
  946. // no longer required. To guard against unsetting the session ID in the
  947. // middle of an uc_cart_add_item() call, we only do this on rebuild.
  948. // See issue 858816 for details.
  949. if (($action == 'empty' || $action == 'rebuild') && empty($items[$cid]) && isset($_SESSION['uc_cart_id']) && $_SESSION['uc_cart_id'] == $cid) {
  950. unset($_SESSION['uc_cart_id']);
  951. }
  952. }
  953. return $items[$cid];
  954. }
  955. /**
  956. * Returns the total number of items in the shopping cart.
  957. *
  958. * The total number of items in the cart isn't simply queried directly from the
  959. * database, because when the shopping cart is loaded items may in fact be
  960. * altered or removed. Hence we actually load the cart and tally up the total
  961. * number of items from the fully loaded cart instead.
  962. *
  963. * @param $cid
  964. * The cart ID of the shopping cart whose items we're totalling; defaults to
  965. * the current user's cart.
  966. *
  967. * @return int
  968. * An integer representing the total number of items in the cart.
  969. */
  970. function uc_cart_get_total_qty($cid = NULL) {
  971. $qty = 0;
  972. if (empty($cid)) {
  973. $cid = uc_cart_get_id(FALSE);
  974. }
  975. if ($cid) {
  976. foreach (uc_cart_get_contents($cid) as $item) {
  977. $qty += $item->qty;
  978. }
  979. }
  980. return $qty;
  981. }
  982. /**
  983. * Updates a cart item.
  984. *
  985. * @param $item
  986. * The loaded cart item.
  987. *
  988. * @return
  989. * unknown_type
  990. */
  991. function uc_cart_update_item($item) {
  992. $item_entity = entity_load_single('uc_cart_item', $item->cart_item_id);
  993. $item_entity->qty = $item->qty;
  994. $item_entity->data = $item->data;
  995. entity_save('uc_cart_item', $item_entity);
  996. }
  997. /**
  998. * Adds an item to a user's cart.
  999. */
  1000. function uc_cart_add_item($nid, $qty = 1, $data = NULL, $cid = NULL, $msg = TRUE, $check_redirect = TRUE, $rebuild = TRUE) {
  1001. $cid = $cid ? $cid : uc_cart_get_id();
  1002. $node = node_load($nid);
  1003. if (is_null($data)) {
  1004. $data = array('module' => 'uc_product');
  1005. }
  1006. if (!isset($data['module'])) {
  1007. $data['module'] = 'uc_product';
  1008. }
  1009. if (!uc_product_is_product($node->type)) {
  1010. drupal_set_message(t('@title is not a product. Unable to add to cart.', array('@title' => $node->title)), 'error');
  1011. return;
  1012. }
  1013. $result = module_invoke_all('uc_add_to_cart', $nid, $qty, $data);
  1014. if (is_array($result) && !empty($result)) {
  1015. foreach ($result as $row) {
  1016. if ($row['success'] === FALSE) {
  1017. if (isset($row['message']) && !empty($row['message'])) {
  1018. $message = $row['message'];
  1019. }
  1020. else {
  1021. $message = t('Sorry, that item is not available for purchase at this time.');
  1022. }
  1023. if (isset($row['silent']) && ($row['silent'] === TRUE)) {
  1024. if ($check_redirect) {
  1025. if (isset($_GET['destination'])) {
  1026. drupal_goto();
  1027. }
  1028. $_SESSION['uc_cart_last_url'] = current_path();
  1029. $redirect = variable_get('uc_add_item_redirect', 'cart');
  1030. if ($redirect != '<none>') {
  1031. return $redirect;
  1032. }
  1033. else {
  1034. return array(current_path(), array('query' => drupal_get_query_parameters()));
  1035. }
  1036. }
  1037. }
  1038. else {
  1039. drupal_set_message($message, 'error');
  1040. }
  1041. return array(current_path(), array('query' => drupal_get_query_parameters()));
  1042. }
  1043. }
  1044. }
  1045. $efq = new EntityFieldQuery();
  1046. $result = $efq->entityCondition('entity_type', 'uc_cart_item')
  1047. ->propertyCondition('cart_id', $cid)
  1048. ->propertyCondition('nid', $node->nid)
  1049. ->propertyCondition('data', serialize($data))
  1050. ->execute();
  1051. // If the item isn't in the cart yet, add it.
  1052. if (empty($result['uc_cart_item'])) {
  1053. $item_entity = entity_create('uc_cart_item', array(
  1054. 'cart_id' => $cid,
  1055. 'nid' => $node->nid,
  1056. 'qty' => $qty,
  1057. 'data' => $data,
  1058. ));
  1059. entity_save('uc_cart_item', $item_entity);
  1060. if ($msg) {
  1061. drupal_set_message(t('<strong>@product-title</strong> added to <a href="!url">your shopping cart</a>.', array('@product-title' => $node->title, '!url' => url('cart'))));
  1062. }
  1063. }
  1064. else {
  1065. // Update the item instead.
  1066. if ($msg) {
  1067. drupal_set_message(t('Your item(s) have been updated.'));
  1068. }
  1069. $item_entity = entity_load_single('uc_cart_item', current(array_keys($result['uc_cart_item'])));
  1070. $qty += $item_entity->qty;
  1071. module_invoke($data['module'], 'uc_update_cart_item', $node->nid, $data, min($qty, 999999), $cid);
  1072. }
  1073. // If specified, rebuild the cached cart items array.
  1074. if ($rebuild) {
  1075. uc_cart_get_contents($cid, 'rebuild');
  1076. }
  1077. if ($check_redirect) {
  1078. if (isset($_GET['destination'])) {
  1079. drupal_goto();
  1080. }
  1081. $_SESSION['uc_cart_last_url'] = current_path();
  1082. $redirect = variable_get('uc_add_item_redirect', 'cart');
  1083. if ($redirect != '<none>') {
  1084. return $redirect;
  1085. }
  1086. else {
  1087. return array(current_path(), array('query' => drupal_get_query_parameters()));
  1088. }
  1089. }
  1090. }
  1091. /**
  1092. * Removes an item from the cart.
  1093. *
  1094. * @param $nid
  1095. * The node ID of the item to remove.
  1096. * @param $cid
  1097. * The cart ID of the item to remove.
  1098. * @param $data
  1099. * The data array for the item to remove.
  1100. */
  1101. function uc_cart_remove_item($nid, $cid = NULL, $data = array()) {
  1102. if (empty($nid)) {
  1103. return;
  1104. }
  1105. $cart_id = !(is_null($cid) || empty($cid)) ? $cid : uc_cart_get_id();
  1106. $efq = new EntityFieldQuery();
  1107. $result = $efq->entityCondition('entity_type', 'uc_cart_item')
  1108. ->propertyCondition('cart_id', $cart_id)
  1109. ->propertyCondition('nid', $nid)
  1110. ->propertyCondition('data', serialize($data))
  1111. ->execute();
  1112. if (!empty($result['uc_cart_item'])) {
  1113. entity_delete_multiple('uc_cart_item', array_keys($result['uc_cart_item']));
  1114. }
  1115. }
  1116. /**
  1117. * Updates the quantity of all the items in a cart object.
  1118. */
  1119. function uc_cart_update_item_object($cart) {
  1120. if (is_object($cart)) {
  1121. foreach ($cart->items as $item) {
  1122. module_invoke($item['module'], 'uc_update_cart_item', $item['nid'], unserialize($item['data']), $item['qty']);
  1123. }
  1124. // Rebuild the cached cart items.
  1125. uc_cart_get_contents(NULL, 'rebuild');
  1126. }
  1127. }
  1128. /**
  1129. * Empties a cart of its contents.
  1130. *
  1131. * @param $cart_id
  1132. * The ID of the cart, or NULL to empty the current cart.
  1133. */
  1134. function uc_cart_empty($cart_id = NULL) {
  1135. $cart_id = $cart_id ? $cart_id : uc_cart_get_id(FALSE);
  1136. if (!$cart_id) {
  1137. return;
  1138. }
  1139. $efq = new EntityFieldQuery();
  1140. $result = $efq->entityCondition('entity_type', 'uc_cart_item')
  1141. ->propertyCondition('cart_id', $cart_id)
  1142. ->execute();
  1143. if (!empty($result['uc_cart_item'])) {
  1144. entity_delete_multiple('uc_cart_item', array_keys($result['uc_cart_item']));
  1145. }
  1146. // Remove cached cart.
  1147. uc_cart_get_contents($cart_id, 'empty');
  1148. }
  1149. /**
  1150. * Gets all of the enabled, sorted cart panes.
  1151. *
  1152. * @param $items
  1153. * The contents of the cart.
  1154. */
  1155. function uc_cart_cart_pane_list($items) {
  1156. $panes = array();
  1157. foreach (module_invoke_all('uc_cart_pane', $items) as $id => $pane) {
  1158. // Preserve backward compatibility for panes with no key specified.
  1159. if (is_numeric($id)) {
  1160. $id = $pane['id'];
  1161. }
  1162. // Set defaults.
  1163. $pane += array(
  1164. 'id' => $id,
  1165. 'enabled' => TRUE,
  1166. 'weight' => 0,
  1167. 'body' => array(),
  1168. );
  1169. $pane['enabled'] = variable_get('uc_cap_' . $id . '_enabled', $pane['enabled']);
  1170. $pane['weight'] = variable_get('uc_cap_' . $id . '_weight', $pane['weight']);
  1171. $panes[$id] = $pane;
  1172. }
  1173. // Allow other modules to alter the defaults.
  1174. drupal_alter('uc_cart_pane', $panes, $items);
  1175. uasort($panes, 'uc_weight_sort');
  1176. return $panes;
  1177. }
  1178. /**
  1179. * Determines whether a cart contains shippable items or not.
  1180. */
  1181. function uc_cart_is_shippable($cart_id = NULL) {
  1182. $items = uc_cart_get_contents($cart_id);
  1183. foreach ($items as $item) {
  1184. if (uc_order_product_is_shippable($item)) {
  1185. return TRUE;
  1186. }
  1187. }
  1188. return FALSE;
  1189. }
  1190. /**
  1191. * Removes panes from the list that match the given conditions.
  1192. *
  1193. * @return array
  1194. * A checkout pane array with panes filtered out that have key values
  1195. * matching the combinations in the $remove array.
  1196. */
  1197. function uc_cart_filter_checkout_panes($panes, $remove) {
  1198. foreach ($panes as $id => $pane) {
  1199. foreach ($remove as $key => $value) {
  1200. if (isset($panes[$id][$key]) && $panes[$id][$key] == $value) {
  1201. unset($panes[$id]);
  1202. }
  1203. }
  1204. }
  1205. return $panes;
  1206. }
  1207. /**
  1208. * Implements hook_entity_info().
  1209. */
  1210. function uc_cart_entity_info() {
  1211. return array(
  1212. 'uc_cart_item' => array(
  1213. 'label' => t('Cart item'),
  1214. 'module' => 'uc_cart',
  1215. 'base table' => 'uc_cart_products',
  1216. 'controller class' => 'UcCartItemController',
  1217. 'entity keys' => array(
  1218. 'id' => 'cart_item_id',
  1219. ),
  1220. 'bundles' => array(
  1221. 'uc_cart_item' => array(
  1222. 'label' => t('Cart item'),
  1223. ),
  1224. ),
  1225. ),
  1226. );
  1227. }