PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/www/wp-content/plugins/ithemes-exchange/core-addons/coupons/basic-coupons/init.php

https://github.com/ArzuA/gitwordpress
PHP | 611 lines | 323 code | 79 blank | 209 comment | 58 complexity | 7ebd21bbfbb4af45bb417ceaf99f4e14 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Basic Coupons
  4. * @package IT_Exchange
  5. * @since 0.4.0
  6. */
  7. if ( is_admin() ) {
  8. include( dirname( __FILE__) . '/admin.php' );
  9. }
  10. /**
  11. * Register the cart coupon type
  12. *
  13. * @since 0.4.0
  14. *
  15. * @return void
  16. */
  17. function it_exchange_basic_coupons_register_coupon_type() {
  18. it_exchange_register_coupon_type( 'cart' );
  19. }
  20. add_action( 'it_exchange_enabled_addons_loaded', 'it_exchange_basic_coupons_register_coupon_type' );
  21. /**
  22. * Adds meta data for Basic Coupons to the coupon object
  23. *
  24. * @since 0.4.0
  25. *
  26. * @return array
  27. */
  28. function it_exchange_basic_coupons_add_meta_data_to_coupon_object( $data, $object ) {
  29. // Set post meta keys used in basic coupons
  30. $post_meta_keys = array(
  31. 'code' => '_it-basic-code',
  32. 'amount_number' => '_it-basic-amount-number',
  33. 'amount_type' => '_it-basic-amount-type',
  34. 'start_date' => '_it-basic-start-date',
  35. 'end_date' => '_it-basic-end-date',
  36. 'limit_quantity' => '_it-basic-limit-quantity',
  37. 'quantity' => '_it-basic-quantity',
  38. 'limit_product' => '_it-basic-limit-product',
  39. 'product_id' => '_it-basic-product-id',
  40. 'limit_frequency' => '_it-basic-limit-frequency',
  41. 'frequency_times' => '_it-basic-frequency-times',
  42. 'frequency_length' => '_it-basic-frequency-length',
  43. 'frequency_units' => '_it-basic-frequency-units',
  44. );
  45. // Loop through and add them to the data that will be added as properties to coupon object
  46. foreach( $post_meta_keys as $property => $key ) {
  47. $data[$property] = get_post_meta( $object->ID, $key, true );
  48. }
  49. // Return data
  50. return $data;
  51. }
  52. add_filter( 'it_exchange_coupon_additional_data', 'it_exchange_basic_coupons_add_meta_data_to_coupon_object', 9, 2 );
  53. /**
  54. * Add field names
  55. *
  56. * @since 0.4.0
  57. *
  58. * @param array $names Incoming core vars => values
  59. * @return array
  60. */
  61. function it_exchange_basic_coupons_register_field_names( $names ) {
  62. $names['apply_coupon'] = 'it-exchange-basic-coupons-apply-coupon';
  63. $names['remove_coupon'] = 'it-exchange-basic-coupons-remove-coupon';
  64. return $names;
  65. }
  66. add_filter( 'it_exchange_default_field_names', 'it_exchange_basic_coupons_register_field_names' );
  67. /**
  68. * Returns applied cart coupons
  69. *
  70. * @since 0.4.0
  71. *
  72. * @param mixed $incoming sent from WP filter. Discarded here.
  73. * @return boolean
  74. */
  75. function it_exchange_basic_coupons_applied_cart_coupons( $incoming=false ) {
  76. $cart_data = it_exchange_get_cart_data( 'basic_coupons' );
  77. return empty( $cart_data ) ? false : $cart_data;
  78. }
  79. add_filter( 'it_exchange_get_applied_cart_coupons', 'it_exchange_basic_coupons_applied_cart_coupons' );
  80. /**
  81. * Determines if we are currently accepting more coupons
  82. *
  83. * Basic coupons only allows one coupon applied to each cart
  84. *
  85. * @since 0.4.0
  86. *
  87. * @param mixed $incoming sent from WP filter. Discarded here.
  88. * @return boolean
  89. */
  90. function it_exchange_basic_coupons_accepting_cart_coupons( $incoming=false ) {
  91. return ! (boolean) it_exchange_get_applied_coupons( 'cart' );
  92. }
  93. add_filter( 'it_exchange_accepting_cart_coupons', 'it_exchange_basic_coupons_accepting_cart_coupons' );
  94. /**
  95. * Return the form field for applying a coupon code to a cart
  96. *
  97. * @since 0.4.0
  98. *
  99. * @param mixed $incoming sent from WP filter. Discarded here.
  100. * @return string
  101. */
  102. function it_exchange_base_coupons_apply_cart_coupon_field( $incoming=false, $options=array() ) {
  103. $defaults = array(
  104. 'class' => 'apply-coupon',
  105. 'placeholder' => __( 'Coupon Code', 'it-l10n-ithemes-exchange' ),
  106. );
  107. $options = ITUtility::merge_defaults( $options, $defaults );
  108. $var = it_exchange_get_field_name( 'apply_coupon' ) . '-cart';
  109. return '<input type="text" class="' . esc_attr( $options['class'] ) . '" name="' . esc_attr( $var ) . '" placeholder="' . esc_attr( $options['placeholder'] ) . '" value="" />';
  110. }
  111. add_filter( 'it_exchange_apply_cart_coupon_field', 'it_exchange_base_coupons_apply_cart_coupon_field', 10, 2 );
  112. /**
  113. * Apply a coupon to a cart on update
  114. *
  115. * @since 0.4.0
  116. *
  117. * @return void
  118. */
  119. function it_exchange_basic_coupons_handle_coupon_on_cart_update() {
  120. $var = it_exchange_get_field_name( 'apply_coupon' ) . '-cart';
  121. // Abort if no coupon code was added
  122. if ( ! $coupon_code = empty( $_REQUEST[$var] ) ? false : $_REQUEST[$var] )
  123. return;
  124. it_exchange_apply_coupon( 'cart', $coupon_code );
  125. }
  126. add_action( 'it_exchange_update_cart', 'it_exchange_basic_coupons_handle_coupon_on_cart_update' );
  127. /**
  128. * Applies a coupon code to a cart if it exists and is valid
  129. *
  130. * @since 0.4.0
  131. *
  132. * @param boolean $result this is default to false. gets set by apply_filters
  133. * @param array $options - must contain coupon key
  134. * @return boolean
  135. */
  136. function it_exchange_basic_coupons_apply_to_cart( $result, $options=array() ) {
  137. // Set coupon code. Return false if one is not available
  138. $coupon_code = empty( $options['code'] ) ? false : $options['code'];
  139. if ( empty( $coupon_code ) ) {
  140. it_exchange_add_message( 'error', __( 'Invalid coupon', 'it-l10n-ithemes-exchange' ) );
  141. return false;
  142. }
  143. // Abort if no coupons are found for submitted code matches and falls within dates
  144. $args = array(
  145. 'meta_query' => array(
  146. array(
  147. 'key' => '_it-basic-code',
  148. 'value' => $coupon_code,
  149. ),
  150. ),
  151. );
  152. if ( ! $coupons = it_exchange_get_coupons( $args ) ) {
  153. it_exchange_add_message( 'error', __( 'Invalid coupon', 'it-l10n-ithemes-exchange' ) );
  154. return false;
  155. }
  156. $coupon = reset( $coupons );
  157. // Abort if coupon limit has been reached
  158. if ( ! empty( $coupon->limit_quantity ) && empty( $coupon->quantity ) ) {
  159. it_exchange_add_message( 'error', __( 'Invalid coupon', 'it-l10n-ithemes-exchange' ) );
  160. return false;
  161. }
  162. // Abort if product not in cart
  163. if ( ! empty( $coupon->limit_product ) && ( it_exchange_get_cart_product_quantity_by_product_id( $coupon->product_id ) < 1 ) ) {
  164. it_exchange_add_message( 'error', __( 'Invalid coupon', 'it-l10n-ithemes-exchange' ) );
  165. return false;
  166. }
  167. // Abort if not within start and end dates
  168. $start_okay = empty( $coupon->start_date ) || strtotime( $coupon->start_date ) <= strtotime( date( 'Y-m-d' ) );
  169. $end_okay = empty( $coupon->end_date ) || strtotime( $coupon->end_date ) >= strtotime( date( 'Y-m-d' ) );
  170. if ( ! $start_okay || ! $end_okay ) {
  171. it_exchange_add_message( 'error', __( 'Invalid coupon', 'it-l10n-ithemes-exchange' ) );
  172. return false;
  173. }
  174. // Get previous uses. Returns array of timestamps
  175. if ( it_exchange_basic_coupon_frequency_limit_met_by_customer( $coupon->ID ) ) {
  176. it_exchange_add_message( 'error', __( 'Invalid coupon', 'it-l10n-ithemes-exchange' ) );
  177. return false;
  178. }
  179. // Format data for session
  180. $coupon = array(
  181. 'id' => $coupon->ID,
  182. 'title' => $coupon->post_title,
  183. 'code' => $coupon->code,
  184. 'amount_number' => it_exchange_convert_from_database_number( $coupon->amount_number ),
  185. 'amount_type' => $coupon->amount_type,
  186. 'start_date' => $coupon->start_date,
  187. 'end_date' => $coupon->end_date,
  188. );
  189. // Add to session data
  190. $data = array( $coupon['code'] => $coupon );
  191. it_exchange_update_cart_data( 'basic_coupons', $data );
  192. it_exchange_add_message( 'notice', __( 'Coupon applied', 'it-l10n-ithemes-exchange' ) );
  193. return true;
  194. }
  195. add_action( 'it_exchange_apply_coupon_to_cart', 'it_exchange_basic_coupons_apply_to_cart', 10, 2 );
  196. /**
  197. * Is this coupon available to this customer?
  198. *
  199. * Grabs array of timestamps specified (or current) user has used the specific coupon.
  200. * Determines # of seconds before now to count uses
  201. * Makes sure that customer has not met limit of use in calculated time period
  202. *
  203. * @since 1.9.2
  204. *
  205. * @param integer $coupon_id wp post id for the coupon
  206. * @param integer $customer_id wp user id of customer
  207. * @return boolean
  208. */
  209. function it_exchange_basic_coupon_frequency_limit_met_by_customer( $coupon_id, $customer_id=false ) {
  210. $customer_id = empty( $customer_id ) ? it_exchange_get_current_customer_id() : $customer_id;
  211. $coupon = it_exchange_get_coupon( $coupon_id );
  212. if ( empty( $coupon->limit_frequency ) || empty( $customer_id ) )
  213. return false;
  214. $current_frequencies = it_exchange_basic_coupons_get_customer_coupon_frequency( $coupon_id, $customer_id );
  215. if ( ! empty( $coupon->limit_frequency ) ) {
  216. // Set the base unit
  217. switch ( $coupon->frequency_units ) {
  218. case 'years' :
  219. $base = YEAR_IN_SECONDS;
  220. break;
  221. case 'months' :
  222. $base = DAY_IN_SECONDS * date_i18n( 't' ); // Not perfect for < PHP 5.3
  223. break;
  224. case 'weeks' :
  225. $base = WEEK_IN_SECONDS;
  226. break;
  227. case 'days' :
  228. default :
  229. $base = DAY_IN_SECONDS;
  230. break;
  231. }
  232. // Multiply the length times the units to get seconds for set frequency
  233. $frequency_seconds = $coupon->frequency_length * $base;
  234. $earliest_limit = date_i18n( 'U' ) - $frequency_seconds;
  235. // Loop through current frequencies and total uses since last limit
  236. $relevant_uses = 0;
  237. foreach( (array) $current_frequencies as $date ) {
  238. if ( $date > $earliest_limit )
  239. $relevant_uses++;
  240. }
  241. // If relevant uses is greater than limit, return error message
  242. if ( $relevant_uses >= $coupon->frequency_times ) {
  243. return true;
  244. }
  245. }
  246. return false;
  247. }
  248. /**
  249. * Gets all coupon uses or all uses for a specific coupon for a user
  250. *
  251. * @since 1.9.2
  252. *
  253. * @param integer $coupon_id the coupon code. optional
  254. * @param integer $customer_id the customer id. defaults to current customer
  255. * @return array
  256. */
  257. function it_exchange_basic_coupons_get_customer_coupon_frequency( $coupon_id=false, $customer_id=false ) {
  258. $customer_id = empty( $customer_id ) ? it_exchange_get_current_customer_id() : $customer_id;
  259. $coupon_history = get_user_meta( $customer_id, '_it_exchagne_basic_coupon_history', true );
  260. if ( empty( $coupon_id ) )
  261. $validated_history = $coupon_history;
  262. else
  263. $validated_history = empty( $coupon_history[$coupon_id] ) ? array() : $coupon_history[$coupon_id];
  264. return apply_filters( 'it_exchange_basic_coupons_get_customer_coupon_frequency', $validated_history, $coupon_id, $customer_id, $coupon_history );
  265. }
  266. /**
  267. * Increments coupon use for a specific coupon for a user
  268. *
  269. * @since 1.9.2
  270. *
  271. * @param integer $coupon_id the coupon code.
  272. * @param integer $customer_id the customer id. defaults to current customer
  273. * @return array
  274. */
  275. function it_exchange_basic_coupons_bump_customer_coupon_frequency( $coupon_id, $customer_id=false ) {
  276. $customer_id = empty( $customer_id ) ? it_exchange_get_current_customer_id() : $customer_id;
  277. $coupon_history = it_exchange_basic_coupons_get_customer_coupon_frequency( false, $customer_id );
  278. if ( empty( $coupon_history[$coupon_id] ) )
  279. $coupon_history[$coupon_id] = array( date_i18n( 'U' ) );
  280. else
  281. $coupon_history[$coupon_id][] = date_i18n('U');
  282. update_user_meta( $customer_id, '_it_exchagne_basic_coupon_history', $coupon_history );
  283. }
  284. /**
  285. * Clear cart coupons when cart is emptied
  286. *
  287. * @since 0.4.0
  288. *
  289. * @return void
  290. */
  291. function it_exchange_clear_cart_coupons_on_empty() {
  292. it_exchange_remove_cart_data( 'basic_coupons' );
  293. }
  294. add_action( 'it_exchange_empty_shopping_cart', 'it_exchange_clear_cart_coupons_on_empty' );
  295. /**
  296. * Return the form checkbox for removing a coupon code to a cart
  297. *
  298. * @since 0.4.0
  299. *
  300. * @param mixed $incoming sent from WP filter. Discarded here.
  301. * @return string
  302. */
  303. function it_exchange_base_coupons_remove_cart_coupon_html( $incoming=false, $code, $options=array() ) {
  304. $defaults = array(
  305. 'class' => 'remove-coupon',
  306. 'format' => 'link',
  307. 'label' => __( '&times;', 'it-l10n-ithemes-exchange' ),
  308. );
  309. $options = ITUtility::merge_defaults( $options, $defaults );
  310. $var = it_exchange_get_field_name( 'remove_coupon' ) . '-cart';
  311. if ( 'checkbox' == $options['format'] ) {
  312. return '<input type="checkbox" class="' . esc_attr( $options['class'] ) . '" name="' . esc_attr( $var ) . '[]" value="' . esc_attr( $options['code'] ) . '" />&nbsp;' . esc_attr( $options['label'] );
  313. } else {
  314. $url = it_exchange_clean_query_args( array( it_exchange_get_field_name( 'sw_cart_focus' ) ) );
  315. $url = add_query_arg( $var . '[]', $options['code'] );
  316. return '<a data-coupon-code="' . esc_attr( $options['code'] ) . '" class="' . esc_attr( $options['class'] ) . '" href="' . $url . '">' . esc_attr( $options['label'] ) . '</a>';
  317. }
  318. }
  319. add_filter( 'it_exchange_remove_cart_coupon_html', 'it_exchange_base_coupons_remove_cart_coupon_html', 10, 3 );
  320. /**
  321. * Modify the cart total to reflect coupons
  322. *
  323. * @since 0.4.0
  324. *
  325. * @return price
  326. */
  327. function it_exchange_basic_coupons_apply_discount_to_cart_total( $total ) {
  328. $coupons = it_exchange_get_applied_coupons( 'cart' );
  329. $total_discount = it_exchange_get_total_coupons_discount( 'cart', array( 'format_price' => false ) );
  330. $total = $total - $total_discount;
  331. return $total;
  332. }
  333. add_filter( 'it_exchange_get_cart_total', 'it_exchange_basic_coupons_apply_discount_to_cart_total' );
  334. /**
  335. * Returns the total discount from applied coupons
  336. *
  337. * @since 0.4.0
  338. *
  339. * @param string $total existing value passed in by WP filter
  340. * @return string
  341. */
  342. function it_exchange_basic_coupons_get_total_discount_for_cart( $discount=false, $options=array() ) {
  343. $defaults = array(
  344. 'format_price' => true,
  345. );
  346. $options = ITUtility::merge_defaults( $options, $defaults );
  347. $coupons = it_exchange_get_applied_coupons( 'cart' );
  348. $subtotal = it_exchange_get_cart_subtotal( false );
  349. foreach( (array) $coupons as $coupon ) {
  350. if ( empty( $coupon ) )
  351. continue;
  352. $coupon = it_exchange_get_coupon( $coupon['id'] );
  353. $coupon->amount_number = empty( $coupon->amount_number ) ? false : it_exchange_convert_from_database_number( $coupon->amount_number );
  354. if ( ! empty( $coupon->product_id ) ) {
  355. $cart_products = it_exchange_get_cart_products();
  356. foreach( (array) it_exchange_get_cart_products() as $cart_product ) {
  357. if ( ! empty( $cart_product['product_id'] ) && ( empty( $coupon->limit_product ) || ( ! empty( $coupon->limit_product ) && $cart_product['product_id'] == $coupon->product_id ) ) ) {
  358. $base_price = it_exchange_get_cart_product_base_price( $cart_product, false );
  359. $product_discount = ( '%' == $coupon->amount_type ) ? $discount + ( ( $coupon->amount_number / 100 ) * $base_price ) : $discount + $coupon->amount_number;
  360. $product_discount = $product_discount * $cart_product['count'];
  361. $discount = $discount + $product_discount;
  362. }
  363. }
  364. } else {
  365. $discount = ( '%' == $coupon->amount_type ) ? $discount + ( ( $coupon->amount_number / 100 ) * $subtotal ) : $discount + $coupon->amount_number;
  366. }
  367. }
  368. $discount = round( $discount, 2 );
  369. if ( $options['format_price'] )
  370. $discount = it_exchange_format_price( $discount );
  371. return $discount;
  372. }
  373. add_filter( 'it_exchange_get_total_discount_for_cart', 'it_exchange_basic_coupons_get_total_discount_for_cart', 10, 2 );
  374. /**
  375. * Reduces coupon quanity by 1 if coupon was applied to transaction and coupon is tracking usage
  376. *
  377. * @since 1.0.2
  378. *
  379. * @param integer $transaction_id
  380. * @return void
  381. */
  382. function it_exchange_basic_coupons_modify_coupon_quantity_on_transaction( $transaction_id ) {
  383. if ( ! $transaction = it_exchange_get_transaction( $transaction_id ) )
  384. return false;
  385. if ( ! $coupons = it_exchange_get_transaction_coupons( $transaction ) )
  386. return;
  387. // Do we have a cart coupon?
  388. if ( isset( $coupons['cart'] ) && ! empty( $coupons['cart'] ) ) {
  389. $coupon = reset( $coupons['cart'] );
  390. // Does this coupon have unlimited quantity
  391. if ( ! $limited = get_post_meta( $coupon['id'], '_it-basic-limit-quantity', true ) )
  392. return;
  393. // Does this coupon have a quantity?
  394. if ( ! $quantity = get_post_meta( $coupon['id'], '_it-basic-quantity', true ) )
  395. return;
  396. // Decrease quantity by one
  397. $quantity = absint( $quantity );
  398. $quantity--;
  399. update_post_meta( $coupon['id'], '_it-basic-quantity', $quantity );
  400. }
  401. }
  402. add_action( 'it_exchange_add_transaction_success', 'it_exchange_basic_coupons_modify_coupon_quantity_on_transaction' );
  403. /**
  404. * Track the customer's use of this coupon on checkout
  405. *
  406. * @since 1.9.2
  407. *
  408. * @param integer $transaction_id
  409. * @return void
  410. */
  411. function it_exchange_basic_coupons_bump_for_customer_on_checkout( $transaction_id ) {
  412. if ( ! $transaction = it_exchange_get_transaction( $transaction_id ) )
  413. return false;
  414. if ( ! $coupons = it_exchange_get_transaction_coupons( $transaction ) )
  415. return;
  416. // Do we have a cart coupon?
  417. if ( isset( $coupons['cart'] ) && ! empty( $coupons['cart'] ) ) {
  418. $coupon = reset( $coupons['cart'] );
  419. $coupon_id = $coupon['id'];
  420. $customer_id = $transaction->customer_id;
  421. it_exchange_basic_coupons_bump_customer_coupon_frequency( $coupon_id, $customer_id );
  422. }
  423. }
  424. add_action( 'it_exchange_add_transaction_success', 'it_exchange_basic_coupons_bump_for_customer_on_checkout' );
  425. /**
  426. * Returns the coupon discount label
  427. *
  428. * @since 0.4.0
  429. *
  430. * @param string $label incoming from WP filter. Not used here.
  431. * @param array $options $options['coupon'] should have the coupon object
  432. * @return string
  433. */
  434. function it_exchange_basic_coupons_get_discount_label( $label, $options=array() ) {
  435. $coupon = empty( $options['coupon']->ID ) ? false : $options['coupon'];
  436. if ( ! $coupon )
  437. return '';
  438. if( 'amount' == $coupon->amount_type )
  439. return it_exchange_format_price( it_exchange_convert_from_database_number( $coupon->amount_number ) );
  440. else
  441. return it_exchange_convert_from_database_number( $coupon->amount_number ) . $coupon->amount_type;
  442. }
  443. add_filter( 'it_exchange_get_coupon_discount_label', 'it_exchange_basic_coupons_get_discount_label', 10, 2 );
  444. /**
  445. * Remove coupon from cart
  446. *
  447. * @since 0.4.0
  448. *
  449. * @return void
  450. */
  451. function it_exchange_basic_coupons_handle_remove_coupon_from_cart_request() {
  452. $var = it_exchange_get_field_name( 'remove_coupon' ) . '-cart';
  453. if ( empty( $_REQUEST[$var] ) )
  454. return;
  455. foreach( (array) $_REQUEST[$var] as $code ) {
  456. it_exchange_remove_coupon( 'cart', $code );
  457. }
  458. if ( it_exchange_is_multi_item_cart_allowed() )
  459. $url = it_exchange_get_page_url( 'cart' );
  460. else
  461. $url = it_exchange_clean_query_args( array( it_exchange_get_field_name( 'sw_cart_focus' ) ) );
  462. it_exchange_add_message( 'notice', __( 'Coupon removed', 'it-l10n-ithemes-exchange' ) );
  463. wp_redirect( $url );
  464. die();
  465. }
  466. add_action( 'template_redirect', 'it_exchange_basic_coupons_handle_remove_coupon_from_cart_request', 9 );
  467. /**
  468. * Removes a coupon from the cart
  469. *
  470. * @param boolean $result default result passed by apply_filters
  471. * @param string $coupon_code code of coupon to be removed
  472. * @return boolean
  473. */
  474. function it_exchange_basic_coupons_remove_coupon_from_cart( $result, $options=array() ) {
  475. $coupon_code = empty( $options['code'] ) ? false : $options['code'];
  476. if ( empty( $coupon_code ) )
  477. return false;
  478. $coupons = it_exchange_get_applied_coupons( 'cart' );
  479. if ( isset( $coupons[$coupon_code] ) )
  480. unset( $coupons[$coupon_code] );
  481. // Unset coupons
  482. it_exchange_update_cart_data( 'basic_coupons', $coupons );
  483. return true;
  484. }
  485. add_filter( 'it_exchange_remove_coupon_for_cart', 'it_exchange_basic_coupons_remove_coupon_from_cart', 10, 2 );
  486. /**
  487. * Returns the summary needed for a transaction
  488. *
  489. * @since 0.4.0
  490. *
  491. * @param string $summary passed in by WP filter. Ignored here.
  492. * @param mixed $transaction_coupon the coupon data stored in the transaction
  493. * @return string summary
  494. */
  495. function it_exchange_basic_coupons_transaction_summary( $summary, $transaction_coupon ) {
  496. $transaction_coupon = reset( $transaction_coupon );
  497. $id = empty( $transaction_coupon['id'] ) ? false : $transaction_coupon['id'];
  498. $title = empty( $transaction_coupon['title'] ) ? false : $transaction_coupon['title'];
  499. $code = empty( $transaction_coupon['code'] ) ? false : $transaction_coupon['code'];
  500. $number = empty( $transaction_coupon['amount_number'] ) ? false : $transaction_coupon['amount_number'];
  501. $type = empty( $transaction_coupon['amount_type'] ) ? false : $transaction_coupon['amount_type'];
  502. $start = empty( $transaction_coupon['start_date'] ) ? false : $transaction_coupon['start_date'];
  503. $end = empty( $transaction_coupon['end_date'] ) ? false : $transaction_coupon['end_date'];
  504. $url = trailingslashit( get_admin_url() ) . 'admin.php';
  505. $url = add_query_arg( array( 'page' => 'it-exchange-edit-basic-coupon', 'post' => $id ), $url );
  506. $link = '<a href="' . $url . '">' . __( 'View Coupon', 'it-l10n-ithemes-exchange' ) . '</a>';
  507. $string = '';
  508. if ( $title )
  509. $string .= $title . ': ';
  510. if ( $code )
  511. $string .= $code . ' | ';
  512. if ( $number && $type )
  513. $string .= implode( '', array( $number, $type ) ) . ' | ';
  514. $string .= ' ' . $link;
  515. return $string;
  516. }
  517. add_filter( 'it_exchange_get_transaction_cart_coupon_summary', 'it_exchange_basic_coupons_transaction_summary', 10, 2 );
  518. /**
  519. * Returns the coupon discount type
  520. *
  521. * @since 0.4.0
  522. *
  523. * @param string $method default type passed by WP filters. Not used here.
  524. * @param array $options includes the ID we're looking for.
  525. * @return string
  526. */
  527. function it_exchange_basic_coupons_get_discount_method( $method, $options=array() ) {
  528. if ( empty( $options['id'] ) || ! $coupon = it_exchange_get_coupon( $options['id'] ) )
  529. return false;
  530. return empty( $coupon->amount_type ) ? false : $coupon->amount_type;
  531. }
  532. add_filter( 'it_exchange_get_coupon_discount_method', 'it_exchange_basic_coupons_get_discount_method', 10, 2 );