uc_coupon.module 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306
  1. <?php
  2. /**
  3. * @file
  4. * Provides discount codes and gift certificates for Ubercart.
  5. *
  6. * Version: 2.x
  7. * Drupal Core: 7.x
  8. * Ubercart Core: 3.x
  9. *
  10. * Original code by Blake Lucchesi (www.boldsource.com)
  11. *
  12. * Maintained by
  13. * Chris Oden (wodenx@gmail.com)
  14. * David Long (dave@longwaveconsulting.com)
  15. *
  16. * Please submit issues, questions or feedback to the issue queue at
  17. * http://drupal.org/project/uc_coupon
  18. */
  19. /**
  20. * Implements hook_menu().
  21. */
  22. function uc_coupon_menu() {
  23. $items = array();
  24. $items['admin/store/coupons'] = array(
  25. 'title' => 'Coupons',
  26. 'description' => 'Manage store discount coupons.',
  27. 'page callback' => 'uc_coupon_display',
  28. 'page arguments' => array('active'),
  29. 'access arguments' => array('view store coupons'),
  30. 'type' => MENU_NORMAL_ITEM,
  31. 'file' => 'uc_coupon.admin.inc',
  32. );
  33. $items['admin/store/coupons/list'] = array(
  34. 'title' => 'Active coupons',
  35. 'description' => 'View active coupons.',
  36. 'page callback' => 'uc_coupon_display',
  37. 'page arguments' => array('active'),
  38. 'access arguments' => array('view store coupons'),
  39. 'type' => MENU_NORMAL_ITEM,
  40. 'file' => 'uc_coupon.admin.inc',
  41. 'weight' => 0,
  42. );
  43. $items['admin/store/coupons/inactive'] = array(
  44. 'title' => 'Inactive coupons',
  45. 'description' => 'View inactive coupons.',
  46. 'page callback' => 'uc_coupon_display',
  47. 'page arguments' => array('inactive'),
  48. 'access arguments' => array('view store coupons'),
  49. 'type' => MENU_NORMAL_ITEM,
  50. 'file' => 'uc_coupon.admin.inc',
  51. 'weight' => 1,
  52. );
  53. $items['admin/store/coupons/add'] = array(
  54. 'title' => 'Add new coupon',
  55. 'description' => 'Add a new coupon.',
  56. 'page callback' => 'drupal_get_form',
  57. 'page arguments' => array('uc_coupon_add_form'),
  58. 'access arguments' => array('manage store coupons'),
  59. 'type' => MENU_NORMAL_ITEM,
  60. 'file' => 'uc_coupon.admin.inc',
  61. 'weight' => 2,
  62. );
  63. $items['admin/store/coupons/%uc_coupon'] = array(
  64. 'title callback' => 'uc_coupon_title',
  65. 'title arguments' => array(3),
  66. 'description' => 'View coupon details.',
  67. 'page callback' => 'uc_coupon_view',
  68. 'page arguments' => array(3),
  69. 'access arguments' => array('view store coupons'),
  70. 'type' => MENU_CALLBACK,
  71. 'file' => 'uc_coupon.admin.inc',
  72. 'weight' => 3,
  73. );
  74. $items['admin/store/coupons/%uc_coupon/view'] = array(
  75. 'title' => 'View',
  76. 'description' => 'View coupon details.',
  77. 'access arguments' => array('view store coupons'),
  78. 'type' => MENU_DEFAULT_LOCAL_TASK,
  79. 'file' => 'uc_coupon.admin.inc',
  80. 'weight' => 0,
  81. );
  82. $items['admin/store/coupons/%uc_coupon/print'] = array(
  83. 'title' => 'Print',
  84. 'description' => 'Print coupon.',
  85. 'page callback' => 'uc_coupon_print',
  86. 'page arguments' => array(3, 5, 'print'),
  87. 'access arguments' => array('view store coupons'),
  88. 'type' => MENU_LOCAL_TASK,
  89. 'file' => 'uc_coupon.admin.inc',
  90. 'weight' => 1,
  91. );
  92. $items['admin/store/coupons/%uc_coupon/edit'] = array(
  93. 'title' => 'Edit',
  94. 'description' => 'Edit an existing coupon.',
  95. 'page callback' => 'drupal_get_form',
  96. 'page arguments' => array('uc_coupon_add_form', 3),
  97. 'access arguments' => array('manage store coupons'),
  98. 'type' => MENU_LOCAL_TASK,
  99. 'file' => 'uc_coupon.admin.inc',
  100. 'weight' => 2,
  101. );
  102. $items['admin/store/coupons/%uc_coupon/delete'] = array(
  103. 'title' => 'Delete',
  104. 'description' => 'Delete a coupon.',
  105. 'page callback' => 'drupal_get_form',
  106. 'page arguments' => array('uc_coupon_delete_confirm', 3),
  107. 'access arguments' => array('manage store coupons'),
  108. 'type' => MENU_LOCAL_TASK,
  109. 'file' => 'uc_coupon.admin.inc',
  110. 'weight' => 3,
  111. );
  112. $items['admin/store/coupons/%uc_coupon/codes'] = array(
  113. 'title' => 'Download bulk coupon codes',
  114. 'description' => 'Download the list of bulk coupon codes as a CSV file.',
  115. 'page callback' => 'uc_coupon_codes_csv',
  116. 'page arguments' => array(3),
  117. 'access arguments' => array('view store coupons'),
  118. 'file' => 'uc_coupon.admin.inc',
  119. 'type' => MENU_CALLBACK,
  120. );
  121. $items['admin/store/coupons/autocomplete/node'] = array(
  122. 'title' => 'Node autocomplete',
  123. 'page callback' => 'uc_coupon_autocomplete_node',
  124. 'access arguments' => array('manage store coupons'),
  125. 'type' => MENU_CALLBACK,
  126. 'file' => 'uc_coupon.admin.inc',
  127. );
  128. $items['admin/store/coupons/autocomplete/term'] = array(
  129. 'title' => 'Term autocomplete',
  130. 'page callback' => 'uc_coupon_autocomplete_term',
  131. 'access arguments' => array('manage store coupons'),
  132. 'type' => MENU_CALLBACK,
  133. 'file' => 'uc_coupon.admin.inc',
  134. );
  135. $items['admin/store/coupons/autocomplete/user'] = array(
  136. 'title' => 'User autocomplete',
  137. 'page callback' => 'uc_coupon_autocomplete_user',
  138. 'access arguments' => array('manage store coupons'),
  139. 'type' => MENU_CALLBACK,
  140. 'file' => 'uc_coupon.admin.inc',
  141. );
  142. $items['admin/store/coupons/autocomplete/role'] = array(
  143. 'title' => 'Role autocomplete',
  144. 'page callback' => 'uc_coupon_autocomplete_role',
  145. 'access arguments' => array('manage store coupons'),
  146. 'type' => MENU_CALLBACK,
  147. 'file' => 'uc_coupon.admin.inc',
  148. );
  149. $items['admin/store/settings/coupon'] = array(
  150. 'title' => 'Coupon module settings',
  151. 'description' => 'Configure the discount coupon module settings.',
  152. 'page callback' => 'drupal_get_form',
  153. 'page arguments' => array('uc_coupon_settings_form'),
  154. 'access arguments' => array('administer store'),
  155. 'file' => 'uc_coupon.admin.inc',
  156. 'type' => MENU_NORMAL_ITEM,
  157. );
  158. $items['admin/store/settings/coupon/settings'] = array(
  159. 'title' => 'Settings',
  160. 'description' => 'Edit the basic coupon settings.',
  161. 'type' => MENU_DEFAULT_LOCAL_TASK,
  162. 'weight' => -10,
  163. );
  164. $items['admin/store/reports/coupon'] = array(
  165. 'title' => 'Coupon usage reports',
  166. 'description' => 'View coupon usage reports.',
  167. 'page callback' => 'uc_coupon_reports',
  168. 'access arguments' => array('view reports'),
  169. 'file' => 'uc_coupon.reports.inc',
  170. 'type' => MENU_NORMAL_ITEM,
  171. );
  172. return $items;
  173. }
  174. /**
  175. * Properly handle %uc_coupon wildcard.
  176. * (Necessary to prevent PHP runtime notice.)
  177. */
  178. function uc_coupon_to_arg($arg) {
  179. return $arg;
  180. }
  181. /**
  182. * Title callback for coupon print preview.
  183. */
  184. function uc_coupon_title($coupon) {
  185. return $coupon->name;
  186. }
  187. /**
  188. * Implements hook_permission().
  189. */
  190. function uc_coupon_permission() {
  191. $perms = array(
  192. 'view store coupons' => array(
  193. 'title' => t('view store coupons'),
  194. 'description' => t('Display information about discount coupons.'),
  195. ),
  196. 'manage store coupons' => array(
  197. 'title' => t('manage store coupons'),
  198. 'description' => t('Create, edit and delete discoutn coupons.'),
  199. ),
  200. );
  201. if (!module_exists('uc_reports')) {
  202. $perms['view reports'] = array(
  203. 'title' => t('view reports'),
  204. 'description' => t('Display coupon usage reports.')
  205. );
  206. }
  207. return $perms;
  208. }
  209. /**
  210. * Implements hook_init().
  211. */
  212. function uc_coupon_init() {
  213. global $conf;
  214. $conf['i18n_variables'][] = 'uc_coupon_pane_description';
  215. // Auto apply coupon from query string, if configured.
  216. if ($param = variable_get('uc_coupon_querystring', '')) {
  217. if (isset($_GET[$param]) && $_GET[$param]) {
  218. // We retain the querystring coupon so that it will validate if/when appropriate conditions are met.
  219. uc_coupon_session_add($_GET[$param], 'retain');
  220. }
  221. }
  222. }
  223. /**
  224. * Implements hook_theme().
  225. */
  226. function uc_coupon_theme() {
  227. return array(
  228. 'uc_coupon_automatic_discounts' => array(
  229. 'render element' => 'form',
  230. ),
  231. 'uc_coupon_form' => array(
  232. 'render element' => 'form',
  233. ),
  234. 'uc_coupon_actions' => array(
  235. 'variables' => array('coupon' => NULL),
  236. 'file' => 'uc_coupon.admin.inc',
  237. ),
  238. 'uc_coupon_code' => array(
  239. 'variables' => array('coupon' => NULL),
  240. 'file' => 'uc_coupon.admin.inc',
  241. ),
  242. 'uc_coupon_discount' => array(
  243. 'variables' => array('coupon' => NULL, 'currency' => TRUE),
  244. ),
  245. 'uc_coupon_certificate' => array(
  246. 'variables' => array('coupon' => NULL, 'code' => NULL),
  247. 'template' => 'uc-coupon-certificate',
  248. 'path' => drupal_get_path('module', 'uc_coupon') . '/theme',
  249. ),
  250. 'uc_coupon_page' => array(
  251. 'variables' => array('content' => NULL),
  252. 'template' => 'uc-coupon-page',
  253. 'path' => drupal_get_path('module', 'uc_coupon') . '/theme',
  254. ),
  255. );
  256. }
  257. /**
  258. * Implements hook_theme_registry_alter().
  259. */
  260. function uc_coupon_theme_registry_alter(&$registry) {
  261. // Override the default theme for the cart block content - but only if not already overridden.
  262. if ($registry['uc_cart_block_content']['function'] == 'theme_uc_cart_block_content') {
  263. $registry['uc_cart_block_content']['function'] = 'uc_coupon_theme_uc_cart_block_content';
  264. }
  265. }
  266. /**
  267. * Count usage of a coupon.
  268. *
  269. * @param $cid
  270. * The coupon id to count.
  271. * @param $uid
  272. * (optional) The user id to count. Defaults to the current user.
  273. * @param array $exclude_oids
  274. * (optional) If supplied, will exclude usage for the specified order ids.
  275. *
  276. * @return
  277. * An associative array containing:
  278. * - codes: An associative array of code => usage count.
  279. * - user: The usage count by the specified (or current) user.
  280. */
  281. function uc_coupon_count_usage($cid, $uid = NULL, $exclude_oids = array()) {
  282. global $user;
  283. $weight = uc_order_status_data(variable_get('uc_coupon_used_order_status', 'processing'), 'weight');
  284. $usage = array('codes' => array(), 'value' => array('codes' => array()));
  285. $exclude_where = empty($exclude_oids) ? '' : 'AND uo.order_id NOT IN (:oids)';
  286. $result = db_query("SELECT uco.code, COUNT(*) AS uses, SUM(uco.value) AS value FROM {uc_coupons_orders} AS uco
  287. LEFT JOIN {uc_orders} AS uo ON uco.oid = uo.order_id
  288. LEFT JOIN {uc_order_statuses} AS uos ON uo.order_status = uos.order_status_id
  289. WHERE uos.weight >= :weight AND uco.cid = :cid $exclude_where GROUP BY uco.code",
  290. array( ':weight' => $weight, ':cid' => $cid, ':oids' => $exclude_oids));
  291. foreach ($result as $row) {
  292. $usage['codes'][$row->code] = $row->uses;
  293. $usage['value']['codes'][$row->code] = $row->value;
  294. }
  295. if (is_null($uid)) {
  296. $uid = $user->uid;
  297. }
  298. $usage['user'] = db_query("SELECT COUNT(*) FROM {uc_coupons_orders} AS uco
  299. LEFT JOIN {uc_orders} AS uo ON uco.oid = uo.order_id
  300. LEFT JOIN {uc_order_statuses} AS uos ON uo.order_status = uos.order_status_id
  301. WHERE uos.weight >= :weight AND uco.cid = :cid AND uo.uid = :uid",
  302. array( ':weight' => $weight, ':cid' => $cid, ':uid' => $uid))->fetchField();
  303. // Allow other modules to implement usage counts.
  304. drupal_alter('uc_coupon_usage', $usage, $cid, $uid);
  305. return $usage;
  306. }
  307. /**
  308. * Theme for a coupon discount.
  309. * @param $variables
  310. * 'coupon' => The coupon whose discount is to be themed.
  311. * 'currency' => TRUE to include currency symbols.
  312. */
  313. function theme_uc_coupon_discount($variables) {
  314. $coupon = $variables['coupon'];
  315. $currency = isset($variables['currency']) ? $variables['currency'] : TRUE;
  316. return _uc_coupon_format_discount($coupon, $currency);
  317. }
  318. /**
  319. * Format a coupon's value depending on the type, optionally including currency symbols.
  320. */
  321. function _uc_coupon_format_discount($coupon, $currency = TRUE) {
  322. switch ($coupon->type) {
  323. case 'price':
  324. case 'credit':
  325. return $currency ? uc_currency_format($coupon->value) : $coupon->value;
  326. case 'percentage':
  327. return (float) $coupon->value . '%';
  328. case 'set_price':
  329. return '=' . ($currency ? uc_currency_format($coupon->value) : $coupon->value);
  330. }
  331. }
  332. /**
  333. * Generate a single bulk coupon code.
  334. */
  335. function uc_coupon_get_bulk_code($coupon, $id) {
  336. // If this coupon has been validated, then $coupon->code is already a bulk code.
  337. if (isset($coupon->valid)) {
  338. $prefix = drupal_substr($coupon->code, 0, strlen($coupon->code) - $coupon->data['bulk_length']);
  339. }
  340. else {
  341. $prefix = $coupon->code;
  342. }
  343. $id = str_pad(dechex($id), strlen(dechex($coupon->data['bulk_number'])), '0', STR_PAD_LEFT);
  344. $length = strlen($prefix) + $coupon->data['bulk_length'];
  345. return strtoupper(substr($prefix . $id . md5($coupon->bulk_seed . $id), 0, $length));
  346. }
  347. /**
  348. * Load a coupon (single or bulk) from the supplied code.
  349. * @param $code
  350. * The coupon code to search for.
  351. * @param $reset
  352. * If TRUE the cache of codes for this request will be purged. Any function which modifies
  353. * a coupon should purge the cache.
  354. */
  355. function uc_coupon_find($code, $reset = FALSE) {
  356. // This is expensive and can be called many times during coupon processing, so we
  357. // use a simple static cache.
  358. static $cached = array();
  359. if ($reset) {
  360. $cached = array();
  361. }
  362. if (!$code) {
  363. return FALSE;
  364. }
  365. elseif (array_key_exists($code, $cached)) {
  366. return $cached[$code];
  367. }
  368. // Look for matching single coupon first.
  369. $coupon = db_query("SELECT cid FROM {uc_coupons}
  370. WHERE code = :code AND status = 1 AND bulk = 0 AND valid_from < :now AND (valid_until = 0 OR valid_until > :now)",
  371. array(':code' => $code, ':now' => REQUEST_TIME))
  372. ->fetchObject();
  373. if ($coupon) {
  374. $cached[$code] = uc_coupon_load($coupon->cid);
  375. return $cached[$code];
  376. }
  377. // Look through bulk coupons.
  378. $result = db_query("SELECT cid, code, data, bulk_seed FROM {uc_coupons}
  379. WHERE status = 1 AND bulk = 1 AND valid_from < :now AND (valid_until = 0 OR valid_until > :now)",
  380. array(':now' => REQUEST_TIME));
  381. foreach ($result as $coupon) {
  382. // Check coupon prefix.
  383. $prefix_length = strlen($coupon->code);
  384. if (substr($code, 0, $prefix_length) != $coupon->code) {
  385. continue;
  386. }
  387. if ($coupon->data) {
  388. $coupon->data = unserialize($coupon->data);
  389. }
  390. // Check coupon sequence ID.
  391. $id = substr($code, $prefix_length, strlen(dechex($coupon->data['bulk_number'])));
  392. if (!preg_match("/^[0-9A-F]+$/", $id)) {
  393. continue;
  394. }
  395. $id = hexdec($id);
  396. if ($id < 0 || $id > $coupon->data['bulk_number']) {
  397. continue;
  398. }
  399. // Check complete coupon code.
  400. if ($code == uc_coupon_get_bulk_code($coupon, $id)) {
  401. $cached[$code] = uc_coupon_load($coupon->cid);
  402. return $cached[$code];
  403. }
  404. }
  405. $cached[$code] = FALSE;
  406. return $cached[$code];
  407. }
  408. /**
  409. * Adds or updates a coupon code for the current session.
  410. *
  411. * @param $code
  412. * The code to add or update.
  413. * @param $op
  414. * Specifies the way the code should be handled the next time session codes are validated.
  415. * - 'submit' - If the code fails validation it is removed from the session; otherwise it is retained.
  416. * A success or failure message is displayed. This is the default operation performed when
  417. * a customer enters a coupon code manually.
  418. * - 'retain' - The code remains in the session whether or not it passes validation. A success
  419. * message is displayed the first time the coupon passes. This is useful for
  420. * codes which are added automatically in response to events occurring before any products
  421. * have been added to the cart (e.g. via the querystring).
  422. * - 'auto' - The code is removed from the session whether or not it passes validation. However, if
  423. * it does validate, the corresponding coupon will be considered valid for the current request only.
  424. * This is useful for modules implementing hook_uc_coupon_revalidate(), which can decide whether or not
  425. * to add their codes each time the valid coupon cache is rebuilt (e.g. automatic discounts based on
  426. * additional conditions).
  427. */
  428. function uc_coupon_session_add($code, $op = 'submit') {
  429. if (!variable_get('uc_coupon_allow_multiple', FALSE)) {
  430. $_SESSION['uc_coupons'] = array($code => $op);
  431. }
  432. else {
  433. $_SESSION['uc_coupons'][$code] = $op;
  434. }
  435. }
  436. /**
  437. * Removes one (or all) coupon codes from the session.
  438. *
  439. * @param $code
  440. * The code to remove, or NULL to remove all codes.
  441. * @param $is_update
  442. * TRUE (the default) if removing this code represents an update of the session; that is, if the code
  443. * was previously validated. FALSE otherwise (e.g. for removal of automatic discounts). Ignored if
  444. * a specific code is not specified.
  445. */
  446. function uc_coupon_session_clear($code = NULL) {
  447. if (isset($code)) {
  448. unset($_SESSION['uc_coupons'][$code]);
  449. }
  450. else {
  451. unset($_SESSION['uc_coupons']);
  452. }
  453. }
  454. /**
  455. * Checks to see if a given code is present in the session, or returns an associative array
  456. * of all codes in the session.
  457. *
  458. * @param $code
  459. * (optional) The code to chec for. If not specified, will return all codes.
  460. *
  461. * @return
  462. * If a code is specified, returns TRUE if that code exists in the session, FALSE otherwise. If no
  463. * code is specified, returns an array of the form $code=>$op for all codes in the session.
  464. */
  465. function uc_coupon_session_get($code = NULL) {
  466. if (isset($code)) {
  467. return isset($_SESSION['uc_coupons'][$code]);
  468. }
  469. elseif (isset($_SESSION['uc_coupons'])) {
  470. return $_SESSION['uc_coupons'];
  471. }
  472. else {
  473. return array();
  474. }
  475. }
  476. /**
  477. * Validates all coupons in the current session. The validated coupons are statically
  478. * cached for each request. The cache is rebuilt the first time this function is called,
  479. * or every time the cart contents are rebuilt.
  480. *
  481. * @param $order
  482. * An order against which to validate the currently applied codes. If specified
  483. * the cached list of valid coupons is rebuilt by revalidating all the codes in
  484. * the session against that order.
  485. *
  486. * @return
  487. * An array of fully validated coupon objects, indexed by code.
  488. */
  489. function uc_coupon_session_validate($order = NULL) {
  490. static $valids = NULL;
  491. // If a list of products is specified, then rebuild the list.
  492. if (isset($order)) {
  493. $valids = array();
  494. $order = clone $order; // We don't want to modify the order passed in.
  495. // Allow modules an opportunity to add or remove coupons from the session.
  496. module_invoke_all('uc_coupon_revalidate', $order);
  497. // Fetch all codes in the session.
  498. $session = uc_coupon_session_get();
  499. if (!empty($session)) {
  500. // Process all coupons in the session.
  501. global $user;
  502. $order->data['coupons'] = array();
  503. foreach ($session as $code => $op) {
  504. $coupon = uc_coupon_validate($code, $order, $user);
  505. if ($coupon->valid) { // Process valid coupons.
  506. $valids[$code] = $coupon;
  507. $order->data['coupons'][$code] = $coupon->discounts;
  508. switch ($op) {
  509. case 'submit':
  510. case 'retain':
  511. // For coupons which were not valid (new submissions) we notify user and modules.
  512. drupal_set_message($coupon->message);
  513. module_invoke_all('uc_coupon_apply', $coupon);
  514. // And we mark them for revalidation.
  515. uc_coupon_session_add($code, 'revalidate');
  516. break;
  517. case 'auto':
  518. // Automatic coupons are never added to the session.
  519. uc_coupon_session_clear($code);
  520. break;
  521. }
  522. }
  523. else { // Process invalid coupons.
  524. switch ($op) {
  525. case 'submit':
  526. // For new coupon submissions, just issue an error and remove from session.
  527. drupal_set_message($coupon->message, 'error');
  528. uc_coupon_session_clear($code);
  529. break;
  530. case 'revalidate':
  531. if (!empty($products)) { // Only issue a message if the cart is not empty.
  532. drupal_set_message(t('%title is no longer applicable to your order', array('%title' => $coupon->title)));
  533. }
  534. module_invoke_all('uc_coupon_remove', $coupon);
  535. // Keep code in the session in case it becomes valid again.
  536. uc_coupon_session_add($code, 'retain');
  537. break;
  538. case 'auto':
  539. // Automatic coupons are never added to the session.
  540. uc_coupon_session_clear($code);
  541. break;
  542. }
  543. }
  544. }
  545. }
  546. }
  547. // If no argument specified and the cache has not been built, we rebuild the cart to force validation.
  548. elseif (!isset($valids)) {
  549. uc_cart_get_contents();
  550. }
  551. return $valids;
  552. }
  553. /**
  554. * Validates a list of coupon codes against a specified order and account.
  555. *
  556. * @param $codes
  557. * The codes to be validated.
  558. * @param $order
  559. * The order that the coupon is being applied to.
  560. * If NULL, the current cart contents will be used.
  561. * If FALSE, product and order validation will be bypassed.
  562. * @param $account
  563. * The user who is attempting to use the coupon.
  564. * If NULL, the current user will be assumed.
  565. * If FALSE, user validation will be bypassed.
  566. *
  567. * @see uc_coupon_validate()
  568. * @see uc_coupon_session_validate()
  569. */
  570. function uc_coupon_validate_multiple($codes, $order, $account) {
  571. $order = clone $order; // We don't want to modify the order passed in.
  572. $order->data['coupons'] = array();
  573. $valids = array();
  574. $invalids = array();
  575. foreach ($codes as $code) {
  576. $coupon = uc_coupon_validate($code, $order, $account);
  577. if ($coupon->valid) { // Process valid coupons.
  578. $valids[$code] = $coupon;
  579. $order->data['coupons'][$code] = $coupon->discounts;
  580. }
  581. else {
  582. $invalids[$code] = $code;
  583. }
  584. }
  585. return array('valid' => $valids, 'invalid' => $invalids);
  586. }
  587. /**
  588. * Validate a coupon, and optionally calculate the order discount.
  589. *
  590. * @param $code
  591. * The coupon code entered at the checkout screen.
  592. * @param $order
  593. * The order that the coupon is being applied to.
  594. * If NULL, the current cart contents will be used.
  595. * If FALSE, product and order validation will be bypassed.
  596. * @param $account
  597. * The user who is attempting to use the coupon.
  598. * If NULL, the current user will be assumed.
  599. * If FALSE, user validation will be bypassed.
  600. *
  601. * @return
  602. * A coupon object with extended information about the validation:
  603. * - $coupon->valid: TRUE if the code was valid, FALSE otherwise.
  604. * - $coupon->code: The specific code to be applied (even for bulk coupons).
  605. * - $coupon->title: The line item title for the discount.
  606. * - $coupon->message: A message to be displayed accepting the acceptance or rejection of this coupon.
  607. * - $coupon->amount: If $order !== FALSE, the discount that should be applied.
  608. * - $coupon->discounts: if $order !== FALSE, an array discounts on individual products indexed by nid,
  609. * containing the following fields:
  610. * -> 'discount' = The full value of the discount on that item.
  611. * -> 'pretax_discount' => The actual pre-tax discount. For fixed discounts to products with
  612. * taxes included, we apply the face value of the coupon tax-inclusively also; that is,
  613. * the actual discount is calculated so that the face value is correct after taxes.
  614. */
  615. function uc_coupon_validate($code, $order = NULL, $account = NULL) {
  616. global $user;
  617. if (is_null($order)) {
  618. $order = new stdClass();
  619. $order->products = uc_cart_get_contents();
  620. }
  621. if (is_null($account)) {
  622. $account = $user;
  623. }
  624. // Look for an active coupon matching the code.
  625. $code = trim(strtoupper($code));
  626. $coupon = uc_coupon_find($code);
  627. if (!$coupon) {
  628. $coupon = new stdClass();
  629. $coupon->valid = FALSE;
  630. $coupon->message = t('This coupon code is invalid or has expired.');
  631. $coupon->title = t('Unknown');
  632. return $coupon;
  633. }
  634. // Count usage for this coupon.
  635. $uid = !empty($account) ? $account->uid : NULL;
  636. // If the order exists, don't count it towards coupon usage.
  637. $oids = !empty($order->order_id) ? array($order->order_id) : array();
  638. $coupon->usage = uc_coupon_count_usage($coupon->cid, $uid, $oids);
  639. // Calculate the discounts (if any).
  640. uc_coupon_prepare($coupon, $code, uc_coupon_calculate_discounts($coupon, $order));
  641. // Invoke validation hook.
  642. foreach (module_implements('uc_coupon_validate') as $module) {
  643. $callback = $module . '_uc_coupon_validate';
  644. $result = $callback($coupon, $order, $account);
  645. if ($result === TRUE) {
  646. // This module wishes the coupon to be accepted.
  647. $coupon->valid = TRUE;
  648. }
  649. elseif (!is_null($result)) {
  650. // This module wishes the coupon to be rejected.
  651. $coupon->valid = FALSE;
  652. $coupon->message = $result;
  653. }
  654. }
  655. // Create a success message.
  656. if ($coupon->valid && !isset($coupon->message)) {
  657. if (isset($coupon->data['apply_message'])) {
  658. $coupon->message = token_replace(check_plain($coupon->data['apply_message']), array('uc_coupon' => $coupon));
  659. }
  660. else {
  661. $amount = theme('uc_price', array('price' => $coupon->amount));
  662. if (isset($order) || variable_get('uc_coupon_show_in_cart', TRUE)) {
  663. $coupon->message = t('A discount of !amount has been applied to your order.', array('!amount' => $amount));
  664. }
  665. else {
  666. $coupon->message = t('A discount of !amount will be applied at checkout.', array('!amount' => $amount));
  667. }
  668. }
  669. }
  670. return $coupon;
  671. }
  672. /**
  673. * Prepares a coupon for validation and application to an order.
  674. *
  675. * @param $coupon
  676. * A raw coupon object.
  677. * @param $discounts
  678. * An associative array of the discounts to be applied, keyed by nid or -lid. Or a string
  679. * containing a message indicating why there are no discounts available.
  680. *
  681. * @return
  682. * A fully validated coupon object with all additional properties set. This is returned
  683. * for convenience, as the $coupon provided is passed by reference and modified directly.
  684. *
  685. * @see uc_coupon_validate().
  686. */
  687. function uc_coupon_prepare($coupon, $code, $discounts) {
  688. $coupon->code = $code;
  689. $coupon->valid = TRUE;
  690. $coupon->amount = 0;
  691. $coupon->pretax_amount = 0;
  692. if (!is_array($discounts)) {
  693. $coupon->discounts = array();
  694. $coupon->message = $discounts;
  695. }
  696. else {
  697. $coupon->discounts = $discounts;
  698. foreach ($coupon->discounts as $item) {
  699. $coupon->amount += $item->discount;
  700. $coupon->pretax_amount += isset($item->pretax_discount) ? $item->pretax_discount : $item->discount;
  701. }
  702. $coupon->amount = round($coupon->amount, variable_get('uc_currency_prec', 2));
  703. unset($coupon->message);
  704. }
  705. // Create the line item title for this coupon.
  706. $format = !empty($coupon->data['line_item_format']) ? $coupon->data['line_item_format'] :
  707. variable_get('uc_coupon_line_item_format', t('Coupon !code', array('!code' => '[uc_coupon:code]')));
  708. $coupon->title = token_replace(check_plain($format), array('uc_coupon' => $coupon));
  709. return $coupon;
  710. }
  711. /**
  712. * Implements hook_uc_coupon_validate().
  713. *
  714. * We implement our own hook to allow other modules a chance to run before us.
  715. *
  716. * @param $coupon
  717. * The coupon object to validate, with special fields set as follows:
  718. * - $coupon->code: The specific code to be applied (even for bulk coupons).
  719. * - $coupon->amount: If $order !== FALSE, the discount that should be applied.
  720. * - $coupon->usage: Coupon usage data from uc_coupon_count_usage().
  721. * @param $order
  722. * The order against which this coupon is to be applied, or FALSE to bypass
  723. * order validation.
  724. * @param $account
  725. * The account of the user trying to use the coupon, or FALSE to bypass user
  726. * validation.
  727. *
  728. * @return
  729. * TRUE if the coupon should be accepted.
  730. * NULL to allow other modules to determine validation.
  731. * Otherwise, a string describing the reason for failure.
  732. */
  733. function uc_coupon_uc_coupon_validate(&$coupon, $order, $account) {
  734. // Coupons which produce no discount are not valid unless they are store credit
  735. // type, or have no face value (e.g. free shipping).
  736. if ($coupon->type !== 'credit' && $coupon->value != 0 && $coupon->amount == 0) {
  737. $coupon->valid = FALSE;
  738. return !empty($coupon->message) ? $coupon->message : t('This coupon is not applicable to your order.');
  739. }
  740. // Check for allowed combinations.
  741. if (!empty($order->data['coupons'])) {
  742. foreach (array_keys($order->data['coupons']) as $code) {
  743. $other = uc_coupon_find($code);
  744. $other_listed = !empty($coupon->data['combinations']) && in_array($other->cid, $coupon->data['combinations']);
  745. $this_ok = (isset($coupon->data['negate_combinations']) xor $other_listed);
  746. $this_listed = !empty($other->data['combinations']) && in_array($coupon->cid, $other->data['combinations']);
  747. $other_ok = (isset($other->data['negate_combinations']) xor $this_listed);
  748. if (!$this_ok || !$other_ok) {
  749. return t('This coupon combination is not allowed.');
  750. }
  751. }
  752. }
  753. if ($coupon->type !== 'credit') {
  754. // Check maximum usage per code.
  755. if ($coupon->max_uses > 0 && !empty($coupon->usage['codes'][$coupon->code]) && $coupon->usage['codes'][$coupon->code] >= $coupon->max_uses) {
  756. return t('This coupon has reached the maximum redemption limit.');
  757. }
  758. // Check maximum usage per user.
  759. if ($account && isset($coupon->data['max_uses_per_user']) && $coupon->usage['user'] >= $coupon->data['max_uses_per_user']) {
  760. return t('This coupon has reached the maximum redemption limit.');
  761. }
  762. }
  763. else {
  764. if (!empty($coupon->usage['value']['codes'][$coupon->code]) && $coupon->usage['value']['codes'][$coupon->code] >= $coupon->value) {
  765. return t('This coupon has reached the maximum redemption limit.');
  766. }
  767. }
  768. // Check user ID.
  769. if ($account && isset($coupon->data['users'])) {
  770. if (in_array("$account->uid", $coupon->data['users'], TRUE) xor !isset($coupon->data['negate_users'])) {
  771. return t('Your user ID is not allowed to use this coupon.');
  772. }
  773. }
  774. // Check roles.
  775. if ($account && isset($coupon->data['roles'])) {
  776. $role_found = FALSE;
  777. foreach ($coupon->data['roles'] as $role) {
  778. if (in_array($role, $account->roles)) {
  779. $role_found = TRUE;
  780. break;
  781. }
  782. }
  783. if ($role_found xor !isset($coupon->data['negate_roles'])) {
  784. return t('You do not have the correct permission to use this coupon.');
  785. }
  786. }
  787. }
  788. /**
  789. * Find items that a coupon will apply to and calculate the discounts.
  790. *
  791. * @param $coupon
  792. * A coupon object to apply, or a coupon code as a string.
  793. * @param $order
  794. * The order object to which the coupon should be applied.
  795. *
  796. * @return
  797. * An array of discounts.
  798. */
  799. function uc_coupon_calculate_discounts($coupon, $order) {
  800. // Can only calculate discounts if an order is provided.
  801. if (empty($order)) {
  802. return array();
  803. }
  804. if (!is_object($coupon)) {
  805. // If argument is a code, load the corresponding coupon.
  806. $coupon = uc_coupon_find($coupon);
  807. }
  808. // Discover if any items match the restrictions, and which items the discount should be calculated against.
  809. $restricted = isset($coupon->data['products']) || isset($coupon->data['skus']) || isset($coupon->data['terms']) || isset($coupon->data['product_types']);
  810. $matched = 0;
  811. $matched_price = 0;
  812. $total_qty = 0;
  813. $total_price = 0;
  814. $items = array();
  815. foreach ($order->products as $item) {
  816. if (isset($item->module) && $item->module == 'uc_coupon') {
  817. continue;
  818. }
  819. $node = node_load($item->nid);
  820. $qty = $item->qty;
  821. if (!$restricted) {
  822. // Coupons with no restrictions apply to all products.
  823. $include = TRUE;
  824. }
  825. else {
  826. // Other coupons only apply to matching products.
  827. $include = FALSE;
  828. $terms = _uc_coupon_list_terms($node);
  829. if (isset($coupon->data['products']) && isset($item->data['kit_id'])) {
  830. // Items that are part of product kits must be included or excluded all together, so we pre-empt other restrictions.
  831. $include = (isset($coupon->data['negate_products']) xor in_array($item->data['kit_id'], $coupon->data['products']));
  832. }
  833. else if (isset($coupon->data['products']) && (isset($coupon->data['negate_products']) xor in_array($item->nid, $coupon->data['products']))) {
  834. $include = TRUE;
  835. }
  836. elseif (isset($coupon->data['products']) && isset($coupon->data['negate_products']) && in_array($item->nid, $coupon->data['products'])) {
  837. // always exclude if in list of negated products
  838. }
  839. elseif (isset($coupon->data['terms']) && (isset($coupon->data['negate_terms']) xor count(array_intersect($terms, $coupon->data['terms'])))) {
  840. $include = TRUE;
  841. }
  842. elseif (isset($coupon->data['terms']) && isset($coupon->data['negate_terms']) && count(array_intersect($terms, $coupon->data['terms']))) {
  843. // always exclude if one of the terms is in the list of negated terms
  844. }
  845. elseif (isset($coupon->data['skus']) && _uc_coupon_match_sku($item->model, $coupon->data['skus'])) {
  846. $include = TRUE;
  847. }
  848. elseif (isset($coupon->data['product_types']) && in_array($node->type, $coupon->data['product_types'])) {
  849. $include = TRUE;
  850. }
  851. }
  852. // A matching product was found.
  853. if ($include) {
  854. $matched += $qty;
  855. $matched_price += $item->price * $qty;
  856. }
  857. $total_qty += $qty;
  858. $total_price += $item->price * $qty;
  859. // Include this item. Coupons that apply to the order subtotal affect all products.
  860. if ($include || $coupon->data['apply_to'] == 'subtotal') {
  861. $clone = clone $item;
  862. $clone->type = $node->type;
  863. $items = array_pad($items, count($items) + $qty, $clone);
  864. }
  865. }
  866. // If no matches were found, there are no discounts to calculate.
  867. if ($matched == 0) {
  868. return t('You do not have any applicable products in your cart.');
  869. }
  870. $use_matched = (isset($coupon->data['minimum_qty_restrict']) && $coupon->data['minimum_qty_restrict'] != FALSE);
  871. // Make sure the minimum quantity restriction (if any) is met.
  872. if (isset($coupon->data['minimum_qty'])) {
  873. if (($use_matched ? $matched : $total_qty) < (int)$coupon->data['minimum_qty']) {
  874. return t('You do not have enough applicable products in your cart.');
  875. }
  876. }
  877. // Make sure the minimum order total restriction (if any) is met.
  878. if ($coupon->minimum_order > 0) {
  879. if (($use_matched ? $matched_price : $total_price) < $coupon->minimum_order) {
  880. return $use_matched ?
  881. t('You have not reached the minimum total of applicable products for this coupon.') :
  882. t('You have not reached the minimum order total for this coupon.');
  883. }
  884. }
  885. // Ensure that all products match, if specified.
  886. if (isset($coupon->data['require_match_all']) && $matched < $total_qty) {
  887. return t('You have non-applicable products in your cart');
  888. }
  889. // Slice off applicable products if a limit was set.
  890. switch ($coupon->data['apply_to']) {
  891. case 'cheapest':
  892. usort($items, '_uc_coupon_sort_products');
  893. $items = array_slice($items, 0, $coupon->data['apply_count']);
  894. break;
  895. case 'expensive':
  896. usort($items, '_uc_coupon_sort_products');
  897. $items = array_slice($items, -$coupon->data['apply_count']);
  898. break;
  899. }
  900. // Build the discounts array and get the order total.
  901. $total = 0;
  902. $discounts = array();
  903. $included_rates = array();
  904. foreach ($items as $item) {
  905. if (!isset($discounts[$item->nid])) { // First entry for this product.
  906. // Calculate the pre-tax discount proportion for this item.
  907. // For fixed discounts to products with taxes included, we apply the face value of the coupon
  908. // tax-inclusively also; that is, the actual discount is reduced so that the face value is
  909. // realized after taxes. (This already happens automatically for percentage based coupons).
  910. $included_rate = 1;
  911. if (module_exists('uc_taxes')) {
  912. foreach (uc_taxes_rate_load() as $tax) {
  913. if ($tax->display_include
  914. && is_array($tax->taxed_line_items) && in_array('coupon', $tax->taxed_line_items)
  915. && in_array($item->type, $tax->taxed_product_types)
  916. && ($tax->shippable == 0 || $item->data['shippable'] == 1)) {
  917. $included_rate += $tax->rate;
  918. }
  919. }
  920. }
  921. // Adjust the price for any stacked coupons.
  922. $prior_discount = 0;
  923. if (!empty($order->data['coupons'])) {
  924. foreach ($order->data['coupons'] as $stacked) {
  925. if (isset($stacked[$item->nid])) {
  926. $prior_discount += $stacked[$item->nid]->pretax_discount;
  927. }
  928. }
  929. }
  930. $total -= $prior_discount * $included_rate;
  931. $discounts[$item->nid] = (object) array(
  932. 'qty' => 1,
  933. 'price' => $item->price - $prior_discount,
  934. );
  935. $included_rates[$item->nid] = $included_rate;
  936. unset($item->type);
  937. }
  938. else { // An entry for this product already exists.
  939. // Add this item to the total for the product.
  940. $discounts[$item->nid]->price += $item->price;
  941. $discounts[$item->nid]->qty++;
  942. }
  943. $total += $item->price * $included_rate;
  944. }
  945. // Add in discounts for any included line items.
  946. $items = uc_order_load_line_items($order);
  947. if (!empty($order->line_items) && !empty($coupon->data['line_items'])) {
  948. foreach ($order->line_items as $line_item) {
  949. if (in_array($line_item['type'], $coupon->data['line_items'])) {
  950. // Use a negative id to distinguish this from a product discount.
  951. $lid = $line_item['line_item_id'];
  952. $lid = is_numeric($lid) ? -$lid : $lid;
  953. // No tax-inclusive line items in ubercart (yet).
  954. $included_rate = 1;
  955. // Adjust the price for any stacked coupons.
  956. $prior_discount = 0;
  957. if (!empty($order->data['coupons'])) {
  958. foreach ($order->data['coupons'] as $stacked) {
  959. if (isset($stacked[$lid])) {
  960. $prior_discount += $stacked[$lid]->pretax_discount;
  961. }
  962. }
  963. }
  964. $discounts[$lid] = (object) array(
  965. 'qty' => 1,
  966. 'price' => $line_item['amount'] - $prior_discount,
  967. );
  968. $included_rates[$lid] = $included_rate;
  969. $total += $discounts[$lid]->price * $included_rate;
  970. }
  971. }
  972. }
  973. // Calculate the discounts per item.
  974. $value = $coupon->value;
  975. if ($coupon->type === 'credit' && !empty($coupon->usage['value']['codes'][$coupon->code])) {
  976. $value -= $coupon->usage['value']['codes'][$coupon->code];
  977. }
  978. foreach ($discounts as $id => $discount) {
  979. $inclusive_price = $discount->price * $included_rates[$id];
  980. switch ($coupon->type) {
  981. case 'percentage':
  982. $discount->discount = $inclusive_price * $coupon->value / 100;
  983. break;
  984. case 'set_price':
  985. $discount->discount = max($inclusive_price - ($coupon->value * $discount->qty), 0);
  986. break;
  987. default:
  988. if ($coupon->type === 'credit' || $coupon->data['apply_to'] == 'subtotal' || $coupon->data['apply_to'] == 'products_total') {
  989. // Apply single discount proportionally across all matching items.
  990. $discount->discount = $total == 0 ? 0 : min($value * ($inclusive_price / $total), $inclusive_price);
  991. }
  992. else {
  993. // Apply full discount value to each matching item.
  994. $discount->discount = min($value * $discount->qty, $inclusive_price);
  995. }
  996. }
  997. $discount->pretax_discount = $discount->discount / $included_rates[$id];
  998. unset($discount->price);
  999. unset($discount->qty);
  1000. }
  1001. return $discounts;
  1002. }
  1003. function _uc_coupon_match_sku($model, $skus) {
  1004. foreach ($skus as $match) {
  1005. if (preg_match('/^' . str_replace('\*', '.*?', preg_quote($match, '/')) . '$/', $model)) {
  1006. return TRUE;
  1007. }
  1008. }
  1009. return FALSE;
  1010. }
  1011. function _uc_coupon_sort_products($a, $b) {
  1012. if ($a->price == $b->price) {
  1013. return 0;
  1014. }
  1015. return $a->price > $b->price ? 1 : -1;
  1016. }
  1017. /**
  1018. * Lists all taxonomy terms contained in 'taxonomy_term_reference' fields for a given node.
  1019. * @param $node
  1020. * The node whose terms should be listed;
  1021. * @return
  1022. * An array of any taxonomy term id's.
  1023. */
  1024. function _uc_coupon_list_terms($node) {
  1025. $terms = array();
  1026. foreach(array_keys(field_info_instances('node', $node->type)) as $field_name) {
  1027. $field_info = field_info_field($field_name);
  1028. if ($field_info['type'] == 'taxonomy_term_reference') {
  1029. if ($field_values = field_get_items('node', $node, $field_name)) {
  1030. foreach ($field_values as $field_value) {
  1031. $terms[] = $field_value['tid'];
  1032. }
  1033. }
  1034. }
  1035. }
  1036. return $terms;
  1037. }
  1038. /**
  1039. * Implements hook_block_info().
  1040. */
  1041. function uc_coupon_block_info() {
  1042. $blocks = array();
  1043. $blocks['coupon-discount'] = array(
  1044. 'info' => t('Coupon discount form'),
  1045. );
  1046. return $blocks;
  1047. }
  1048. /**
  1049. * Implements hook_block_view().
  1050. */
  1051. function uc_coupon_block_view($delta) {
  1052. if ($delta == 'coupon-discount') {
  1053. $block = array(
  1054. 'subject' => t('Coupon discount'),
  1055. 'content' => drupal_get_form('uc_coupon_form', 'block'),
  1056. );
  1057. return $block;
  1058. }
  1059. }
  1060. /**
  1061. * Default theme implementation for the coupon submit form.
  1062. */
  1063. function theme_uc_coupon_form($variables) {
  1064. $form = $variables['form'];
  1065. $output = '';
  1066. if ($form['#uc_coupon_form_context'] == 'cart') {
  1067. $output .= '<h3>' . t('Coupon discounts') . '</h3>';
  1068. }
  1069. elseif ($form['#uc_coupon_form_context'] == 'block') {
  1070. if (isset($form['code'])) {
  1071. $form['code']['#size'] = 15;
  1072. }
  1073. }
  1074. $output .= drupal_render_children($form);
  1075. return $output;
  1076. }
  1077. /**
  1078. * Implements hook_uc_cart_pane().
  1079. */
  1080. function uc_coupon_uc_cart_pane($items) {
  1081. drupal_add_css(drupal_get_path('module', 'uc_coupon') . '/uc_coupon.css');
  1082. // The coupon entry cart pane.
  1083. $body = drupal_get_form('uc_coupon_form', 'cart') + array(
  1084. '#prefix' => '<div id="uc-cart-pane-coupon">',
  1085. '#suffix' => '</div>'
  1086. );
  1087. $panes[] = array(
  1088. 'id' => 'coupon',
  1089. 'body' => $body,
  1090. 'title' => t('Coupon discount'),
  1091. 'desc' => t('Allows shoppers to use a coupon during checkout for order discounts.'),
  1092. 'weight' => 1,
  1093. 'enabled' => TRUE,
  1094. );
  1095. // The "Special Discounts" cart pane.
  1096. $body = array();
  1097. $discounts = _uc_coupon_options_list(uc_coupon_session_validate(), FALSE);
  1098. if (!empty($discounts)) {
  1099. $body = array(
  1100. '#theme' => 'uc_coupon_automatic_discounts',
  1101. '#prefix' => '<div id="uc-cart-pane-coupon-automatic">',
  1102. '#title' => t('Special discounts'),
  1103. '#suffix' => '</div>',
  1104. 'discounts' => array(
  1105. '#theme' => 'item_list',
  1106. '#items' => _uc_coupon_options_list(uc_coupon_session_validate(), FALSE),
  1107. '#title' => t('Special discounts'),
  1108. )
  1109. );
  1110. }
  1111. $panes[] = array(
  1112. 'id' => 'coupon_auto',
  1113. 'body' => $body,
  1114. 'title' => t('Special Discounts'),
  1115. 'desc' => t('Displays a list of automatic discounts.'),
  1116. 'weight' => 1,
  1117. 'enabled' => TRUE,
  1118. );
  1119. return $panes;
  1120. }
  1121. function theme_uc_coupon_automatic_discounts($variables) {
  1122. $form = $variables['form'];
  1123. /*
  1124. $items = $form['discounts']['#items'];
  1125. $title = isset($form['discounts']['#title']) ? $form['discounts']['#title'] : NULL;
  1126. $rows = array();
  1127. foreach($items as $item) {
  1128. $rows[] = array($item);
  1129. }
  1130. $form['discounts'] = array(
  1131. '#theme' => 'table',
  1132. '#rows' => $rows,
  1133. );
  1134. if (isset($title)) {
  1135. $form['discounts']['#header'] = array($title);
  1136. }
  1137. */
  1138. return drupal_render_children($form);
  1139. }
  1140. /**
  1141. * Create a tapir table of validated coupons with a "Remove" button for each.
  1142. *
  1143. * @param $coupons
  1144. * An array of coupon options of the form code => title
  1145. * @param $submit
  1146. * An array of additional options to attach to the form's submit elements (e.g. #ajax, #submit)
  1147. */
  1148. function uc_coupon_table($coupons, $submit = FALSE) {
  1149. $table = array(
  1150. '#type' => 'tapir_table',
  1151. );
  1152. $table['#columns'] = array(
  1153. 'title' => array(
  1154. 'cell' => t('Active coupons'),
  1155. 'weight' => 0,
  1156. ),
  1157. 'remove' => array(
  1158. 'cell' => t('Remove'),
  1159. 'weight' => 1,
  1160. ),
  1161. );
  1162. $i = 0;
  1163. foreach ($coupons as $code => $title) {
  1164. $table[$i] = array(
  1165. 'title' => array('#markup' => $title),
  1166. 'remove' => array(
  1167. '#type' => 'submit',
  1168. '#value' => t('Remove'),
  1169. '#name' => 'uc-coupon-remove-' . $code,
  1170. ),
  1171. );
  1172. if ($submit) {
  1173. // Add ajax functionality to this table.
  1174. $table[$i]['remove'] += $submit;
  1175. }
  1176. $i++;
  1177. }
  1178. return $table;
  1179. }
  1180. /**
  1181. * Helper function to create a list of coupons for form elements.
  1182. * @param $coupons
  1183. * An array of validated coupon objects to include in the list.
  1184. * @param $in_session
  1185. * TRUE to include only coupons currently added to the session.
  1186. * FALSE to include those not in the session (e.g. automatic coupons).
  1187. * @return
  1188. * An associative array mapping coupon code to coupon title.
  1189. */
  1190. function _uc_coupon_options_list($coupons, $in_session = TRUE) {
  1191. $options = array();
  1192. if (!empty($coupons)) {
  1193. foreach ($coupons as $coupon) {
  1194. if (!$in_session xor uc_coupon_session_get($coupon->code)) {
  1195. $options[$coupon->code] = $coupon->title;
  1196. if ($coupon->type === 'credit') {
  1197. $credit = empty($coupon->usage['value']['codes'][$coupon->code]) ? 0 : $coupon->usage['value']['codes'][$coupon->code];
  1198. $credit += empty($coupon->amount) ? 0 : $coupon->amount;
  1199. $credit = $credit > $coupon->value ? 0 : $coupon->value - $credit;
  1200. $options[$coupon->code] .= ' (' . t('@credit credit remaining', array('@credit' => uc_currency_format($credit))) . ')';
  1201. }
  1202. }
  1203. }
  1204. }
  1205. return $options;
  1206. }
  1207. function uc_coupon_checkout_submit($form, &$form_state) {
  1208. $form_state['rebuild'] = TRUE;
  1209. unset($form_state['checkout_valid']);
  1210. $form_state['redirect'] = 'cart/checkout';
  1211. }
  1212. function uc_coupon_order_submit($form, &$form_state) {
  1213. $form_state['rebuild'] = TRUE;
  1214. uc_coupon_form_submit($form, $form_state);
  1215. $coupons = uc_coupon_get_order_coupons($form_state['order']);
  1216. uc_coupon_apply_to_order($form_state['order'], uc_coupon_get_order_coupons($form_state['order']));
  1217. // !TODO: Shouldn't save the order here because we prevent reverting changes
  1218. // made by other panes?
  1219. uc_order_save($form_state['order']);
  1220. }
  1221. /**
  1222. * Form builder for the uc_coupon form.
  1223. *
  1224. * @param $context
  1225. * Where the form is to appear: 'cart', 'block' or 'checkout'
  1226. * @param $submit
  1227. * An array of additional options to attach to the form's submit elements (e.g. #ajax, #submit)
  1228. */
  1229. function uc_coupon_form($form, $form_state, $context = 'block', $submit = FALSE) {
  1230. //dpm($form_state['order'], 'order build');
  1231. $coupons = ($context == 'order') ? uc_coupon_get_order_coupons($form_state['order']) : uc_coupon_session_validate();
  1232. //dpm($coupons, 'coupons build');
  1233. $components = variable_get('uc_coupon_form_components',
  1234. drupal_map_assoc(variable_get('uc_coupon_allow_multiple', FALSE) ? array('entry') : array('entry', 'list')));
  1235. // Show the coupon code entry component
  1236. if (!empty($components['entry'])) {
  1237. $form['code'] = array(
  1238. '#type' => 'textfield',
  1239. '#size' => 25,
  1240. '#title' => t('Coupon Code'),
  1241. '#description' => t('Enter a coupon code and click "Apply to order" below.'),
  1242. );
  1243. $form['apply'] = array(
  1244. '#type' => 'submit',
  1245. '#value' => t('Apply to order'),
  1246. '#name' => 'uc-coupon-apply',
  1247. );
  1248. if (!variable_get('uc_coupon_allow_multiple', FALSE) && count(uc_coupon_session_get()) > 0) {
  1249. $form['code']['#description'] .= ' ' . t('Apply a blank code to remove the currently applied coupon.');
  1250. }
  1251. drupal_add_css('#uc-coupon-active-coupons, #uc-coupon-other-discounts { clear: left; }', array('type' => 'inline', 'group' => CSS_DEFAULT));
  1252. if ($submit) {
  1253. $form['apply'] += $submit;
  1254. }
  1255. }
  1256. // Add active coupons components (table and/or list).
  1257. $options = _uc_coupon_options_list($coupons, $context != 'order');
  1258. if (!empty($options)) {
  1259. if (!empty($components['table'])) {
  1260. $form['coupons_table'] = tapir_get_table('uc_coupon_table', $options, $submit);
  1261. }
  1262. if (!empty($components['list'])) {
  1263. $form['coupons'] = array(
  1264. '#prefix' => '<div id="uc-coupon-active-coupons">',
  1265. '#suffix' => '</div>',
  1266. '#type' => 'checkboxes',
  1267. '#title' => t('Active Coupons'),
  1268. '#options' => $options,
  1269. '#default_value' => array_keys($options),
  1270. '#description' => t('These coupons have been applied to your order. To remove one, uncheck the box and click "Remove coupons" below.'),
  1271. );
  1272. $form['coupons']['#description'] = t('These coupons have been applied to your order. To remove one, uncheck the box next to the coupon name and click "Update order" below.');
  1273. $form['remove'] = array(
  1274. '#type' => 'submit',
  1275. '#value' => t('Update order'),
  1276. '#name' => 'uc-coupon-remove',
  1277. );
  1278. if ($submit) {
  1279. $form['remove'] += $submit;
  1280. }
  1281. }
  1282. }
  1283. // Add context to help out themers.
  1284. $form['#uc_coupon_form_context'] = $context;
  1285. return $form;
  1286. }
  1287. /**
  1288. * Implements hook_uc_order().
  1289. */
  1290. function uc_coupon_uc_order($op, &$order) {
  1291. if ($op == 'presave') {
  1292. // Apply any session coupons to the current cart order.
  1293. if (_uc_coupon_is_checkout_order($order)) {
  1294. $coupons = uc_coupon_session_validate($order);
  1295. uc_coupon_apply_to_order($order, $coupons);
  1296. }
  1297. // Make sure any fake cart items don't get saved with the order if the checkout page is skipped
  1298. // (e.g. Paypal Express Checkout, Google Checkout)
  1299. foreach ($order->products as $key => $product) {
  1300. if (isset($product->module) && $product->module == 'uc_coupon') {
  1301. unset($order->products[$key]);
  1302. }
  1303. }
  1304. }
  1305. }
  1306. /**
  1307. * Apply a set of coupons to an order.
  1308. *
  1309. * Line items and entries in the uc_coupons_orders table will be added for each coupon. Any coupon line items
  1310. * or entries which are not in the list of coupons will be removed. Additionally, the coupons' discount
  1311. * arrays will be added to the order object's data array.
  1312. *
  1313. * @param $order
  1314. * The order to which the coupons should be applied.
  1315. * @param $coupons
  1316. * An associative array of fully validated coupon objects, keyed by the coupon code.
  1317. */
  1318. function uc_coupon_apply_to_order($order, $coupons) {
  1319. // Index existing line items by coupon code.
  1320. $items = array();
  1321. foreach ($order->line_items as $index => $line) {
  1322. if ($line['type'] == 'coupon') {
  1323. // For orders created before multi-coupons were enabled, the code was not saved with the line item.
  1324. // In this case, we retreive it from the uc_coupons_orders table.
  1325. $code = isset($line['data']['code']) ? $line['data']['code'] :
  1326. db_query('SELECT code FROM {uc_coupons_orders} WHERE oid = :oid', array(':oid' => $order_id))->fetchField();
  1327. $items[$code] = $index;
  1328. }
  1329. }
  1330. // Index existing entries in {uc_coupons_orders} by coupon code.
  1331. $entries = db_query('SELECT code, cuid FROM {uc_coupons_orders} WHERE oid = :oid',
  1332. array(':oid' => $order->order_id))->fetchAllKeyed(0,1);
  1333. // Update, insert or delete line items and entries in uc_coupons_orders.
  1334. $insert = array();
  1335. $order->data['coupons'] = array();
  1336. foreach($coupons as $coupon) {
  1337. $order->data['coupons'][$coupon->code] = $coupon->discounts;
  1338. // Handle entries in {uc_coupons_orders}.
  1339. if (isset($entries[$coupon->code])) {
  1340. db_update('uc_coupons_orders')->condition('cuid', $entries[$coupon->code])
  1341. ->fields(array('cid' => $coupon->cid, 'value' => $coupon->amount))
  1342. ->execute();
  1343. unset($entries[$coupon->code]);
  1344. }
  1345. else {
  1346. $insert[] = array($coupon->cid, $order->order_id, $coupon->code, $coupon->value);
  1347. }
  1348. // Handle line items.
  1349. if (isset($items[$coupon->code])) {
  1350. $line =& $order->line_items[$items[$coupon->code]];
  1351. $line['title'] = $coupon->title;
  1352. $line['amount'] = -$coupon->pretax_amount;
  1353. $line['data']['code'] = $coupon->code;
  1354. uc_order_update_line_item($line['line_item_id'], $line['title'], $line['amount'], $line['data']);
  1355. unset($items[$coupon->code]);
  1356. }
  1357. else {
  1358. // Create a new line item.
  1359. $order->line_items[] = uc_order_line_item_add($order->order_id, 'coupon',
  1360. $coupon->title,
  1361. -$coupon->pretax_amount,
  1362. _uc_line_item_data('coupon', 'weight'),
  1363. array('code' => $coupon->code)
  1364. );
  1365. }
  1366. }
  1367. // Insert new entries in {uc_coupons_orders}
  1368. if (!empty($insert)) {
  1369. $query = db_insert('uc_coupons_orders');
  1370. $query->fields(array('cid', 'oid', 'code', 'value'));
  1371. foreach ($insert as $fields) {
  1372. $query->values($fields);
  1373. }
  1374. $query->execute();
  1375. }
  1376. // Delete orphaned entries in {uc_coupons_orders}
  1377. if (!empty($entries)) {
  1378. db_delete('uc_coupons_orders')->condition('cuid', $entries)->execute();
  1379. }
  1380. // Remove orphaned line-items.
  1381. foreach ($items as $index) {
  1382. uc_order_delete_line_item($order->line_items[$index]['line_item_id']);
  1383. unset($order->line_items[$index]);
  1384. }
  1385. usort($order->line_items, 'uc_weight_sort');
  1386. }
  1387. /**
  1388. * Implements hook_uc_checkout_pane().
  1389. *
  1390. * Show a pane just above the order total that allows shoppers to enter a coupon
  1391. * for a discount.
  1392. */
  1393. function uc_coupon_uc_checkout_pane() {
  1394. $panes[] = array(
  1395. 'id' => 'coupon',
  1396. 'callback' => 'uc_checkout_pane_coupon',
  1397. 'title' => t('Coupon discount'),
  1398. 'desc' => t('Allows shoppers to use a coupon during checkout for order discounts.'),
  1399. 'weight' => 5,
  1400. 'process' => TRUE,
  1401. );
  1402. $panes[] = array(
  1403. 'id' => 'coupon_automatic',
  1404. 'callback' => 'uc_checkout_pane_coupon_automatic',
  1405. 'title' => t('Special Discounts'),
  1406. 'desc' => t('Displays a list of all automatic coupon discounts.'),
  1407. 'weight' => 5,
  1408. 'process' => FALSE,
  1409. );
  1410. return $panes;
  1411. }
  1412. /**
  1413. * Ajax callback for checkout form.
  1414. */
  1415. function uc_coupon_checkout_update($form, $form_state) {
  1416. $commands[] = ajax_command_replace('#coupon-pane', trim(drupal_render($form['panes']['coupon'])));
  1417. if (isset($form['panes']['coupon_automatic'])) {
  1418. $commands[] = ajax_command_replace('#coupon_automatic-pane', trim(drupal_render($form['panes']['coupon_automatic'])));
  1419. }
  1420. if (isset($form['panes']['quotes'])) {
  1421. $commands[] = ajax_command_replace('#quotes-pane', drupal_render($form['panes']['quotes']));
  1422. }
  1423. if (isset($form['panes']['payment'])) {
  1424. $commands[] = ajax_command_replace('#payment-pane', trim(drupal_render($form['panes']['payment'])));
  1425. }
  1426. if (variable_get('uc_coupon_show_in_cart', TRUE) && isset($form['panes']['cart']['cart_review_table'])) {
  1427. $commands[] = ajax_command_html('#cart-pane>div', drupal_render($form['panes']['cart']['cart_review_table']));
  1428. }
  1429. // Clear the coupon code, but only if the submission was successful.
  1430. if (count(drupal_get_messages('error', FALSE)) == 0) {
  1431. $commands[] = ajax_command_invoke('#coupon-pane input[type=text]', 'val', array(''));
  1432. }
  1433. // Make sure all checkboxes are checked.
  1434. $commands[] = ajax_command_invoke('#coupon-pane input[type=checkbox]', 'attr', array('checked', 'true'));
  1435. // Show any messages.
  1436. $commands[] = ajax_command_html('#coupon-messages', theme('status_messages'));
  1437. return array('#type' => 'ajax', '#commands' => $commands);
  1438. }
  1439. /**
  1440. * A checkout pane listing any automatic discounts.
  1441. */
  1442. function uc_checkout_pane_coupon_automatic($op, &$order, $form = NULL, &$form_state = NULL) {
  1443. if ($op == 'view') {
  1444. $discounts = _uc_coupon_options_list(uc_coupon_session_validate(), FALSE);
  1445. if (empty($discounts)) {
  1446. $inner_contents = array(
  1447. '#markup' => t('None.'),
  1448. );
  1449. }
  1450. else {
  1451. $inner_contents = array(
  1452. '#theme' => 'item_list',
  1453. '#items' => _uc_coupon_options_list(uc_coupon_session_validate(), FALSE)
  1454. );
  1455. }
  1456. return array(
  1457. 'theme' => 'uc_coupon_automatic_discounts',
  1458. 'contents' => array(
  1459. 'discounts' => $inner_contents,
  1460. ),
  1461. );
  1462. }
  1463. }
  1464. /**
  1465. * Checkout Pane callback function.
  1466. *
  1467. * Used to display a form in the checkout process so that customers
  1468. * can enter discount coupons.
  1469. */
  1470. function uc_checkout_pane_coupon($op, &$order, $form = NULL, &$form_state = NULL) {
  1471. switch ($op) {
  1472. case 'prepare':
  1473. // Remove fake cart items from the order.
  1474. foreach ($order->products as $key => $product) {
  1475. if (isset($product->module) && $product->module == 'uc_coupon') {
  1476. unset($order->products[$key]);
  1477. }
  1478. }
  1479. break;
  1480. case 'view':
  1481. // Revalidate the session coupons against the actual order.
  1482. drupal_add_css('#coupon-messages { clear: both; }', array('type' => 'inline', 'group' => CSS_DEFAULT));
  1483. $description = variable_get('uc_coupon_pane_description', t('Enter a coupon code for this order.'));
  1484. $submit = array(
  1485. '#limit_validation_errors' => array(),
  1486. '#ajax' => array(
  1487. 'callback' => 'uc_coupon_checkout_update',
  1488. ),
  1489. '#submit' => array('uc_coupon_checkout_submit'),
  1490. );
  1491. $contents = uc_coupon_form(array(), $form_state, 'checkout', $submit);
  1492. $contents['message'] = array(
  1493. '#markup' => '<div id="coupon-messages"></div>',
  1494. '#weight' => 2,
  1495. );
  1496. return array(
  1497. 'description' => $description,
  1498. 'contents' => $contents,
  1499. 'theme' => 'uc_coupon_form',
  1500. );
  1501. case 'process':
  1502. $trigger = $form_state['triggering_element']['#name'];
  1503. if (substr($trigger, 0, 9) == 'uc-coupon') {
  1504. $form_state['rebuild'] = TRUE;
  1505. uc_coupon_form_submit($form['panes']['coupon'], $form_state);
  1506. return FALSE; // Prevent redirection.
  1507. }
  1508. else {
  1509. // !TODO Coupon will not be submitted if "Apply to order" is not clicked. Is this what we want?
  1510. return TRUE;
  1511. }
  1512. case 'settings':
  1513. $form['uc_coupon_collapse_pane'] = array(
  1514. '#type' => 'checkbox',
  1515. '#title' => t('Collapse checkout pane by default.'),
  1516. '#default_value' => variable_get('uc_coupon_collapse_pane', FALSE),
  1517. );
  1518. $form['uc_coupon_pane_description'] = array(
  1519. '#type' => 'textarea',
  1520. '#title' => t('Checkout pane message'),
  1521. '#default_value' => variable_get('uc_coupon_pane_description', t('Enter a coupon code for this order.')),
  1522. );
  1523. return $form;
  1524. }
  1525. }
  1526. /**
  1527. * Submit handler for the uc_coupon form.
  1528. */
  1529. function uc_coupon_form_submit($form, &$form_state) {
  1530. $trigger = $form_state['triggering_element']['#name'];
  1531. // Determine where the values are (they will be in a subarray if called from checkout or order page).
  1532. switch ($context = $form['#uc_coupon_form_context']) {
  1533. case 'checkout':
  1534. $values = $form_state['values']['panes']['coupon'];
  1535. break;
  1536. case 'order':
  1537. $values = $form_state['values']['coupon'];
  1538. break;
  1539. default:
  1540. $values = $form_state['values'];
  1541. }
  1542. // If this was the result of a 'remove' submission.
  1543. if (substr($trigger, 0, 16) == 'uc-coupon-remove') {
  1544. // See if there was an individual remove button clicked.
  1545. $code = substr($trigger, 17);
  1546. if (!empty($code)) {
  1547. if ($context == 'order') {
  1548. unset($form_state['order']->data['coupons']['code']);
  1549. }
  1550. else {
  1551. uc_coupon_session_clear($code);
  1552. drupal_set_message(t('Coupon "%code" has been removed from your order', array('%code' => $code)));
  1553. module_invoke_all('uc_coupon_remove', uc_coupon_find($code));
  1554. }
  1555. }
  1556. // Otherwise see if it's a checkbox submission.
  1557. elseif (isset($values['coupons'])) {
  1558. $removed = array();
  1559. foreach ($values['coupons'] as $code => $selected) {
  1560. if (!$selected) {
  1561. $removed[] = $code;
  1562. if ($context == 'order') {
  1563. unset($form_state['order']->data['coupons'][$code]);
  1564. }
  1565. else {
  1566. uc_coupon_session_clear($code);
  1567. module_invoke_all('uc_coupon_remove', uc_coupon_find($code));
  1568. }
  1569. }
  1570. }
  1571. $n = count($removed);
  1572. if ($n > 1) {
  1573. $last = $removed[$n - 1];
  1574. $rest = implode(', ', array_slice($removed, 0, $n - 1));
  1575. drupal_set_message(t('Coupons %rest and %last have been removed from your order.', array('%rest' => $rest, '%last' => $last)));
  1576. }
  1577. elseif (!empty($removed)) {
  1578. drupal_set_message(t('Coupon %code has been removed from your order', array('%code' => $removed[0])));
  1579. }
  1580. }
  1581. }
  1582. // Otherwise try to apply.
  1583. else {
  1584. $code = empty($values['code']) ? '' : strtoupper(trim($values['code']));
  1585. $removed = FALSE;
  1586. // If multiple codes are not enabled, then remove any codes currently applied.
  1587. if (!variable_get('uc_coupon_allow_multiple', FALSE) && count($session = uc_coupon_session_get()) > 0) {
  1588. if ($context == 'order') {
  1589. unset($form_state['order']->data['coupons']);
  1590. }
  1591. else {
  1592. foreach (array_keys($session) as $remove_code) {
  1593. uc_coupon_session_clear($remove_code);
  1594. drupal_set_message(t('Coupon "%code" has been removed from your order', array('%code' => $remove_code)));
  1595. module_invoke_all('uc_coupon_remove', uc_coupon_find($remove_code));
  1596. }
  1597. $removed = TRUE;
  1598. }
  1599. }
  1600. if (!empty($code)) {
  1601. if ($context == 'order') {
  1602. $coupon = uc_coupon_validate($code, $form_state['order'], user_load($form_state['order']->uid));
  1603. if ($coupon->valid) {
  1604. $form_state['order']->data['coupons'][$code] = $coupon->discounts;
  1605. }
  1606. else {
  1607. drupal_set_message($coupon->message, 'error');
  1608. }
  1609. }
  1610. else {
  1611. uc_coupon_session_add($code, 'submit');
  1612. }
  1613. }
  1614. elseif (!$removed) {
  1615. drupal_set_message(t("You must enter a valid coupon code."), 'error');
  1616. }
  1617. }
  1618. }
  1619. /**
  1620. * Implements hook_uc_line_item().
  1621. */
  1622. function uc_coupon_uc_line_item() {
  1623. $items[] = array(
  1624. 'id' => 'coupon',
  1625. 'title' => t('Coupon discount'),
  1626. 'tax_adjustment' => 'uc_coupon_tax_adjustment',
  1627. 'weight' => 0,
  1628. 'default' => FALSE,
  1629. 'stored' => TRUE,
  1630. 'add_list' => FALSE,
  1631. 'calculated' => TRUE,
  1632. );
  1633. return $items;
  1634. }
  1635. /**
  1636. * Handle tax on coupons by calculating tax for individual discounted prices.
  1637. */
  1638. function uc_coupon_tax_adjustment($price, $order, $tax) {
  1639. $amount = 0;
  1640. if (isset($order->data['coupons'])) {
  1641. foreach ($order->data['coupons'] as $discounts) {
  1642. foreach ($discounts as $id => $item) {
  1643. if (is_numeric($id) && $id > 0) {
  1644. // This is a product discount, so see if the product is taxable.
  1645. $node = node_load($id);
  1646. $adjust = in_array($node->type, $tax->taxed_product_types) && ($tax->shippable == 0 || $node->shippable == 1);
  1647. }
  1648. else {
  1649. // This is a line-item discount, so find the corresponding line item.
  1650. $lid = is_numeric($id) ? -$id : $id; // Convert id to a line item id.
  1651. foreach ($order->line_items as $line_item) {
  1652. if ($line_item['line_item_id'] == $lid) {
  1653. $adjust = in_array($line_item['type'], $tax->taxed_line_items);
  1654. break;
  1655. }
  1656. }
  1657. }
  1658. if ($adjust) {
  1659. $amount += (isset($item->pretax_discount) ? $item->pretax_discount : $item->discount) * ($price > 0 ? 1 : -1);
  1660. }
  1661. }
  1662. }
  1663. }
  1664. return $amount;
  1665. }
  1666. /**
  1667. * Show a message if PayPal is enabled and "itemized order" is selected.
  1668. */
  1669. function _uc_coupon_paypal_check() {
  1670. if (variable_get('uc_payment_method_paypal_wps_checkout', 0) && variable_get('uc_paypal_wps_submit_method', 'single') == 'itemized') {
  1671. drupal_set_message(t('To use coupons with PayPal you must select "Submit the whole order as a single line item". <a href="!url">Click here to change this setting</a>.', array('!url' => url('admin/store/settings/payment/edit/methods'))));
  1672. }
  1673. }
  1674. /**
  1675. * Implements hook_uc_store_status().
  1676. */
  1677. function uc_coupon_uc_store_status() {
  1678. $statuses = array();
  1679. if (variable_get('uc_payment_method_paypal_wps_checkout', 0) && variable_get('uc_paypal_wps_submit_method', 'single') == 'itemized') {
  1680. $statuses[] = array(
  1681. 'status' => 'warning',
  1682. 'title' => t('Coupons'),
  1683. 'desc' => t('To use coupons with PayPal you must select "Submit the whole order as a single line item". <a href="!url">Click here to change this setting</a>.', array('!url' => url('admin/store/settings/payment/edit/methods'))),
  1684. );
  1685. }
  1686. return $statuses;
  1687. }
  1688. /**
  1689. * Implements hook_uc_cart_alter().
  1690. *
  1691. * This is called every time the cart is rebuild (e.g. when products are added), so it's a good place
  1692. * to revalidate our session coupons. We also add a fake cart item (if configured to show in cart)
  1693. * for each coupon. These will be removed at checkout.
  1694. */
  1695. function uc_coupon_uc_cart_alter(&$items) {
  1696. // Validate all codes in the session against the cart contents.
  1697. $order = new UcOrder();
  1698. $order->products = $items;
  1699. $order->data = array();
  1700. $coupons = uc_coupon_session_validate($order);
  1701. if (variable_get('uc_coupon_show_in_cart', TRUE) && !empty($coupons)) {
  1702. // If there are some valid coupons, then add them to the cart (but only if
  1703. // they have a non-zero value.
  1704. foreach ($coupons as $code => $coupon) {
  1705. if ($coupon->amount != 0) {
  1706. $items[] = _uc_coupon_cart_item($coupon);
  1707. }
  1708. }
  1709. }
  1710. }
  1711. /**
  1712. * Creates a fake cart-item corrresponding to this coupon, allowing this coupon to be displayed in the cart.
  1713. *
  1714. * @param $coupon
  1715. * The coupon to be displayed in the cart.
  1716. */
  1717. function _uc_coupon_cart_item($coupon) {
  1718. // Exclude any line-item discounts from the amount shown in the cart.
  1719. $amount = 0;
  1720. foreach ($coupon->discounts as $id => $discount) {
  1721. if (is_numeric($id) && $id > 0) {
  1722. $amount += $discount->discount;
  1723. }
  1724. }
  1725. // Assign this a unique cart_item_id so it will be keyed properly by entity_view().
  1726. $id = -hexdec(substr(sha1($coupon->code), -8));
  1727. return (object) array(
  1728. 'cart_item_id' => $id,
  1729. 'module' => 'uc_coupon',
  1730. 'title' => $coupon->title,
  1731. 'nid' => 0,
  1732. 'qty' => 1,
  1733. 'price' => -$amount,
  1734. 'data' => array('module' => 'uc_coupon', 'shippable' => FALSE, 'code' => $coupon->code, 'remove' => uc_coupon_session_get($coupon->code)),
  1735. 'model' => 0,
  1736. 'weight' => 0
  1737. );
  1738. }
  1739. /**
  1740. * Implements hook_uc_cart_display().
  1741. */
  1742. function uc_coupon_uc_cart_display($item) {
  1743. $display_item = array(
  1744. 'module' => array('#type' => 'value', '#value' => 'uc_coupon'),
  1745. 'nid' => array('#type' => 'value', '#value' => 0),
  1746. 'title' => array('#markup' => $item->title),
  1747. 'description' => array('#markup' => ''),
  1748. 'qty' => array('#type' => 'hidden', '#value' => 1, '#default_value' => 1),
  1749. '#total' => $item->price,
  1750. 'data' => array('#type' => 'hidden', '#value' => serialize($item->data)),
  1751. '#suffixes' => array(),
  1752. );
  1753. if ($item->data['remove']) {
  1754. $display_item['remove'] = array('#type' => 'submit', '#value' => t('Remove'));
  1755. }
  1756. return $display_item;
  1757. }
  1758. /**
  1759. * Implements hook_uc_update_cart_item().
  1760. * Remove a coupon from the order when the "Remove" button is clicked.
  1761. */
  1762. function uc_coupon_uc_update_cart_item($nid, $data, $qty) {
  1763. if (isset($data['code']) && $qty == 0) {
  1764. uc_coupon_session_clear($data['code']);
  1765. module_invoke_all('uc_coupon_remove', uc_coupon_find($data['code']));
  1766. }
  1767. }
  1768. /**
  1769. * Theme override for the default cart block content.
  1770. * Removes coupons from the total number of items.
  1771. */
  1772. function uc_coupon_theme_uc_cart_block_content($variables) {
  1773. if (variable_get('uc_coupon_show_in_cart', TRUE) && !empty($variables['items'])) {
  1774. foreach ($variables['items'] as &$item) {
  1775. if ($item['nid'] == 0 && $item['price'] <= 0) {
  1776. $item['qty'] = '';
  1777. $variables['item_count']--;
  1778. }
  1779. }
  1780. $variables['item_text'] = format_plural($variables['item_count'], '<span class="num-items">1</span> Item', '<span class="num-items">@count</span> Items');
  1781. }
  1782. return theme_uc_cart_block_content($variables);
  1783. }
  1784. /**
  1785. * Implements hook_form_FORM_ID_alter() for uc_cart_checkout_form().
  1786. *
  1787. * Remove any coupon cart items from the serialized cart contents and payment-pane
  1788. * order, as coupons will be handled as line items during checkout.
  1789. *
  1790. * Collapse coupon checkout pane, if configured to do so.
  1791. */
  1792. function uc_coupon_form_uc_cart_checkout_form_alter(&$form, $form_state) {
  1793. if (variable_get('uc_coupon_collapse_pane', FALSE) && isset($form['panes']['coupon'])) {
  1794. $form['panes']['coupon']['#collapsed'] = TRUE;
  1795. }
  1796. // Show current session coupons in the cart pane (since now they will have been removed from the order).
  1797. if (variable_get('uc_coupon_show_in_cart', TRUE) && isset($form['panes']['cart'])) {
  1798. $coupons = uc_coupon_session_validate();
  1799. // If there are some valid coupons, then add them to the cart.
  1800. foreach ($coupons as $code => $coupon) {
  1801. if ($coupon->amount != 0) {
  1802. $item = _uc_coupon_cart_item($coupon);
  1803. $item->order_product_id = $item->cart_item_id;
  1804. $form['panes']['cart']['cart_review_table']['#items'][] = $item;
  1805. }
  1806. }
  1807. }
  1808. }
  1809. /**
  1810. * Implements hook_uc_checkout_complete().
  1811. *
  1812. * Ensure the stored coupon code is reset after checkout.
  1813. */
  1814. function uc_coupon_uc_checkout_complete($order, $account) {
  1815. uc_coupon_session_clear();
  1816. }
  1817. /**
  1818. * Preprocess template for a printed coupon certificate.
  1819. * @see uc_coupon-certificate.tpl.php
  1820. */
  1821. function template_preprocess_uc_coupon_certificate(&$variables) {
  1822. $coupon = $variables['coupon'];
  1823. // Create variables for each user-added field.
  1824. $fields = field_info_fields();
  1825. foreach ($fields as $name => $field) {
  1826. if (in_array('uc_coupon', array_keys($field['bundles']))) {
  1827. $items = field_get_items('uc_coupon', $coupon, $name);
  1828. $variables[$name] = $items;
  1829. }
  1830. }
  1831. $variables['value'] = theme('uc_coupon_discount', array('coupon' => $coupon));
  1832. $variables['display_name'] = check_plain($coupon->name);
  1833. $n = stripos($variables['display_name'], 'purchased by');
  1834. if ($n) {
  1835. $variables['display_name'] = substr($variables['display_name'], 0, $n -1);
  1836. }
  1837. if ($coupon->valid_until) {
  1838. $variables['not_yet_valid'] = $coupon->valid_from > REQUEST_TIME;
  1839. $variables['valid_from'] = format_date($coupon->valid_from, 'custom', variable_get('date_format_uc_store', 'm/d/Y'));
  1840. $variables['valid_until'] = format_date($coupon->valid_until, 'custom', variable_get('date_format_uc_store', 'm/d/Y'));
  1841. }
  1842. else {
  1843. $variables['not_yet_valid'] = FALSE;
  1844. $variables['valid_from'] = FALSE;
  1845. $variables['valid_until'] = FALSE;
  1846. }
  1847. $variables['max_uses_per_user'] = isset($coupon->data['max_uses_per_user']) ? $coupon->data['max_uses_per_user'] : NULL;
  1848. $variables['include'] = array();
  1849. $variables['exclude'] = array();
  1850. if (isset($coupon->data['product_types'])) {
  1851. foreach ($coupon->data['product_types'] as $type) {
  1852. $variables['include'][] = node_type_get_name($type);
  1853. }
  1854. }
  1855. if (isset($coupon->data['products'])) {
  1856. $key = isset($coupon->data['negate_products']) ? 'exclude' : 'include';
  1857. foreach ($coupon->data['products'] as $nid) {
  1858. $node = node_load($nid);
  1859. $variables[$key][] = $node->title;
  1860. }
  1861. }
  1862. if (isset($coupon->data['skus'])) {
  1863. foreach ($coupon->data['skus'] as $sku) {
  1864. $variables['include'][] = t('SKU') . ' ' . $sku;
  1865. }
  1866. }
  1867. if (isset($coupon->data['terms'])) {
  1868. $key = isset($coupon->data['negate_terms']) ? 'exclude' : 'include';
  1869. foreach ($coupon->data['terms'] as $tid) {
  1870. $term = taxonomy_term_load($tid);
  1871. $variables[$key][] = $term->name;
  1872. }
  1873. }
  1874. // Merge in global tokens.
  1875. $info = token_info();
  1876. foreach ($info['types'] as $type => $type_info) {
  1877. if (empty($type_info['needs-data']) && $type != 'current-user' && $type != 'current-date') {
  1878. $type_key = !empty($type_info['type']) ? $type_info['type'] : $type;
  1879. if (!empty($info['tokens'][$type_key])) {
  1880. foreach (array_keys($info['tokens'][$type_key]) as $token) {
  1881. $variables[str_replace('-', '_', $type_key) . '_' . str_replace('-', '_', $token)] = token_replace("[$type_key:$token]");
  1882. }
  1883. }
  1884. }
  1885. }
  1886. if (isset($variables['coupon']->data['base_cid'])) {
  1887. $variables['theme_hook_suggestions'][] = 'uc_coupon_certificate__base_' . $variables['coupon']->data['base_cid'];
  1888. }
  1889. $variables['theme_hook_suggestions'][] = 'uc_coupon_certificate__' . $variables['coupon']->cid;
  1890. }
  1891. /**
  1892. * Page template for printed coupons.
  1893. * @see uc_coupon-page.tpl.php
  1894. */
  1895. function template_preprocess_uc_coupon_page(&$variables) {
  1896. $variables['styles'] = drupal_get_css();
  1897. }
  1898. /**
  1899. * Implements hook_uc_coupon_actions().
  1900. */
  1901. function uc_coupon_uc_coupon_actions($coupon) {
  1902. $actions = array();
  1903. if (user_access('view store coupons')) {
  1904. $actions[] = array(
  1905. 'url' => 'admin/store/coupons/' . $coupon->cid,
  1906. 'icon' => drupal_get_path('module', 'uc_store') . '/images/order_view.gif',
  1907. 'title' => t('View coupon: @name', array('@name' => $coupon->name)),
  1908. );
  1909. $actions[] = array(
  1910. 'url' => 'admin/store/coupons/' . $coupon->cid . '/print',
  1911. 'icon' => drupal_get_path('module', 'uc_store') . '/images/print.gif',
  1912. 'title' => t('Print coupon: @name', array('@name' => $coupon->name)),
  1913. );
  1914. if ($coupon->bulk) {
  1915. $actions[] = array(
  1916. 'url' => 'admin/store/coupons/' . $coupon->cid . '/codes',
  1917. 'icon' => drupal_get_path('module', 'uc_store') . '/images/menu_reports_small.gif',
  1918. 'title' => t('Download codes as CSV: @name', array('@name' => $coupon->name)),
  1919. );
  1920. }
  1921. }
  1922. if (user_access('manage store coupons')) {
  1923. $actions[] = array(
  1924. 'url' => 'admin/store/coupons/' . $coupon->cid . '/edit',
  1925. 'icon' => drupal_get_path('module', 'uc_store') . '/images/order_edit.gif',
  1926. 'title' => t('Edit coupon: @name', array('@name' => $coupon->name)),
  1927. );
  1928. $actions[] = array(
  1929. 'url' => 'admin/store/coupons/' . $coupon->cid . '/delete',
  1930. 'icon' => drupal_get_path('module', 'uc_store') . '/images/order_delete.gif',
  1931. 'title' => t('Delete coupon: @name', array('@name' => $coupon->name)),
  1932. );
  1933. }
  1934. return $actions;
  1935. }
  1936. /**
  1937. * Implements hook_views_api().
  1938. */
  1939. function uc_coupon_views_api() {
  1940. return array(
  1941. 'api' => '2.0',
  1942. 'path' => drupal_get_path('module', 'uc_coupon') . '/views',
  1943. );
  1944. }
  1945. /**
  1946. * Check whether an order is the order being checked out by the current user.
  1947. * @param $order
  1948. */
  1949. function _uc_coupon_is_checkout_order($order) {
  1950. global $user;
  1951. return isset($_SESSION['cart_order'])
  1952. && isset($order->order_id)
  1953. && $order->order_id == $_SESSION['cart_order']
  1954. && uc_order_status_data($order->order_status, 'state') == 'in_checkout'
  1955. && $user->uid == $order->uid;
  1956. }
  1957. /**
  1958. * Implements hook_entity_info();
  1959. */
  1960. function uc_coupon_entity_info() {
  1961. return array(
  1962. 'uc_coupon' => array(
  1963. 'label' => t('Coupon'),
  1964. 'controller class' => 'UcCouponController',
  1965. 'metadata controller class' => 'UcCouponMetadataController',
  1966. 'base table' => 'uc_coupons',
  1967. 'fieldable' => TRUE,
  1968. 'entity keys' => array(
  1969. 'id' => 'cid',
  1970. ),
  1971. 'bundles' => array(
  1972. 'uc_coupon' => array(
  1973. 'label' => t('Coupon'),
  1974. 'admin' => array(
  1975. 'path' => 'admin/store/settings/coupon',
  1976. 'access arguments' => array('manage store coupons'),
  1977. ),
  1978. ),
  1979. ),
  1980. 'view modes' => array(
  1981. 'full' => array(
  1982. 'label' => t('Administrative view'),
  1983. ),
  1984. ),
  1985. ),
  1986. );
  1987. }
  1988. /**
  1989. * Loads one coupon entity from the database.
  1990. */
  1991. function uc_coupon_load($cid, $reset = FALSE) {
  1992. if (is_null($cid) || $cid < 1) {
  1993. return FALSE;
  1994. }
  1995. $coupons = uc_coupon_load_multiple(array($cid), array(), $reset);
  1996. return $coupons ? reset($coupons) : FALSE;
  1997. }
  1998. /**
  1999. * Loads one or more coupon entities from the database.
  2000. *
  2001. * @param $ids
  2002. * An array of coupon IDs.
  2003. * @param $conditions
  2004. * An array of conditions on the {uc_coupons} table in the form
  2005. * 'field' => $value.
  2006. *
  2007. * @return
  2008. * An array of order objects indexed by order_id.
  2009. */
  2010. function uc_coupon_load_multiple($ids, $conditions = array(), $reset = FALSE) {
  2011. return entity_load('uc_coupon', $ids, $conditions, $reset);
  2012. }
  2013. /**
  2014. * Save a coupon object.
  2015. *
  2016. * If the 'cid' field is set, then this will update an existing coupon.
  2017. * Otherwise, a new bulk seed will be generated, the coupon will be
  2018. * inserted into the database, and $coupon->cid will be set.
  2019. *
  2020. * @param $coupon
  2021. * The coupon to save.
  2022. *
  2023. * @param $edit
  2024. * An optional array of extra data that other modules may need to save.
  2025. */
  2026. function uc_coupon_save(&$coupon, $edit = array()) {
  2027. entity_save('uc_coupon', $coupon);
  2028. }
  2029. /**
  2030. * Delete a coupon object.
  2031. *
  2032. * @param $cid
  2033. * The id of the coupon to delete.
  2034. */
  2035. function uc_coupon_delete($cid) {
  2036. entity_delete('uc_coupon', $cid);
  2037. }
  2038. /**
  2039. * Implements hook_field_extra_fields().
  2040. */
  2041. function uc_coupon_field_extra_fields() {
  2042. $extra = array();
  2043. $extra['uc_coupon']['uc_coupon']['display']['admin_summary'] = array(
  2044. 'label' => t('Administrative Summary'),
  2045. 'description' => t('A summary of all coupon details.'),
  2046. 'weight' => 0,
  2047. );
  2048. return $extra;
  2049. }
  2050. /**
  2051. * Implements hook_uc_order_pane().
  2052. *
  2053. * Defines the shipping quote order pane.
  2054. */
  2055. function uc_coupon_uc_order_pane() {
  2056. $panes['coupon'] = array(
  2057. 'callback' => 'uc_order_pane_coupon',
  2058. 'title' => t('Coupon, Credit or Discount Codes'),
  2059. 'desc' => t('Apply a coupon or discount code to the current order.'),
  2060. 'class' => 'pos-left',
  2061. 'weight' => 7,
  2062. 'show' => array('edit'),
  2063. );
  2064. return $panes;
  2065. }
  2066. /**
  2067. * Coupon order pane callback.
  2068. *
  2069. * @see uc_quote_order_pane_quotes_submit()
  2070. * @see uc_quote_apply_quote_to_order()
  2071. */
  2072. function uc_order_pane_coupon($op, $order, &$form = NULL, &$form_state = NULL) {
  2073. switch ($op) {
  2074. case 'edit-form':
  2075. $submit = array(
  2076. '#limit_validation_errors' => array(array('coupon')),
  2077. '#submit' => array('uc_coupon_order_submit'),
  2078. );
  2079. $form['coupon'] = uc_coupon_form(array(), $form_state, 'order', $submit);
  2080. $form['#uc_coupon_form_context'] = 'order';
  2081. $form['coupon']['#theme'] = 'uc_coupon_form';
  2082. $form['coupon']['#tree'] = TRUE;
  2083. break;
  2084. case 'edit-theme':
  2085. return drupal_render($form['coupon']);
  2086. }
  2087. }
  2088. /**
  2089. * Implements hook_form_uc_order_edit_form_alter().
  2090. */
  2091. function uc_coupon_form_uc_order_edit_form_alter(&$form, &$form_state) {
  2092. $order = $form_state['order'];
  2093. $line_items = $order->line_items;
  2094. foreach ($line_items as $item) {
  2095. // Coupon line items should be changed using the coupon order-edit pane.
  2096. if ($item['type'] == 'coupon') {
  2097. $form['line_items'][$item['line_item_id']]['title'] = array(
  2098. '#markup' => check_plain($item['title']),
  2099. );
  2100. $form['line_items'][$item['line_item_id']]['remove']['#access'] = FALSE;
  2101. $form['line_items'][$item['line_item_id']]['amount'] = array(
  2102. '#theme' => 'uc_price',
  2103. '#price' => $item['amount'],
  2104. );
  2105. }
  2106. }
  2107. }
  2108. /**
  2109. * Gets the fully validated coupon objects that have been applied to this order.
  2110. *
  2111. * @param $order
  2112. * The order in question.
  2113. * @param $recalculate
  2114. * If TRUE, the value of each coupon will be recalculated using the current state
  2115. * of the order and current coupon settings.
  2116. * If FALSE (default), the original coupon values will be preserved.
  2117. */
  2118. function uc_coupon_get_order_coupons($order, $recalculate = FALSE) {
  2119. $coupons = array();
  2120. if (!empty($order->data['coupons'])) {
  2121. if ($recalculate) {
  2122. $dummy_order = clone $order;
  2123. $dummy_order->data['coupons'] = array();
  2124. }
  2125. foreach ($order->data['coupons'] as $code => $discounts) {
  2126. $coupon = uc_coupon_find($code);
  2127. if (!empty($coupon->cid)) {
  2128. if ($recalculate) {
  2129. $discounts = uc_coupon_calculate_discounts($coupon, $dummy_order);
  2130. $dummy_order->data['coupons'][$code] = $coupon->discounts;
  2131. }
  2132. $coupons[] = uc_coupon_prepare($coupon, $code, $discounts);
  2133. }
  2134. }
  2135. }
  2136. return $coupons;
  2137. }