PageRenderTime 46ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/www/wp-content/plugins/ithemes-exchange/api/cart.php

https://github.com/ArzuA/gitwordpress
PHP | 839 lines | 387 code | 124 blank | 328 comment | 75 complexity | 21cf2b4a8bdd051b4e73a1a17e3561f2 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * This file contains functions intended for theme developers to interact with the active shopping cart plugin
  4. *
  5. * The active shopping cart plugin should add the needed hooks below within its codebase.
  6. *
  7. * @since 0.3.7
  8. * @package IT_Exchange
  9. */
  10. /**
  11. * Returns an array of all data in the cart
  12. *
  13. * @since 0.3.7
  14. * @return array
  15. */
  16. function it_exchange_get_cart_data( $key = false ) {
  17. $data = it_exchange_get_session_data( $key );
  18. return apply_filters( 'it_exchange_get_cart_data', $data );
  19. }
  20. /**
  21. * Updates the data
  22. *
  23. * @since 0.4.0
  24. * @return void
  25. */
  26. function it_exchange_update_cart_data( $key, $data ) {
  27. it_exchange_update_session_data( $key, $data );
  28. do_action( 'it_exchange_update_cart_data', $data, $key );
  29. }
  30. /**
  31. * Removes cart data by key
  32. *
  33. * @since 0.4.0
  34. */
  35. function it_exchange_remove_cart_data( $key ) {
  36. it_exchange_clear_session_data( $key );
  37. do_action( 'it_exchange_remove_cart_data', $key );
  38. }
  39. /**
  40. * Returns an array of all products in the cart
  41. *
  42. * @since 0.3.7
  43. * @return array
  44. */
  45. function it_exchange_get_cart_products() {
  46. $products = it_exchange_get_session_data( 'products' );
  47. return ( empty( $products ) || ! array( $products ) ) ? array() : $products;
  48. }
  49. /**
  50. * Inserts product into the cart session
  51. *
  52. * @since 0.4.0
  53. * @return array
  54. */
  55. function it_exchange_add_cart_product( $cart_product_id, $product ) {
  56. it_exchange_add_session_data( 'products', array( $cart_product_id => $product ) );
  57. do_action( 'it_exchange_add_cart_product', $product );
  58. }
  59. /**
  60. * Updates product into the cart session
  61. *
  62. * @since 0.4.0
  63. * @return array
  64. */
  65. function it_exchange_update_cart_product( $cart_product_id, $product ) {
  66. $products = it_exchange_get_session_data( 'products' );
  67. if ( isset( $products[$cart_product_id] ) ) {
  68. $products[$cart_product_id] = $product;
  69. it_exchange_update_session_data( 'products', $products );
  70. } else {
  71. it_exchange_add_cart_product( $cart_product_id, $product );
  72. }
  73. do_action( 'it_exchange_update_cart_product', $cart_product_id, $product, $products );
  74. }
  75. /**
  76. * Deletes product from the cart session
  77. *
  78. * @since 0.4.0
  79. * @return array
  80. */
  81. function it_exchange_delete_cart_product( $cart_product_id ) {
  82. $products = it_exchange_get_session_data( 'products' );
  83. if ( isset( $products[$cart_product_id] ) ) {
  84. unset( $products[$cart_product_id] );
  85. it_exchange_update_session_data( 'products', $products );
  86. }
  87. do_action( 'it_exchange_delete_cart_product', $cart_product_id, $products );
  88. }
  89. /**
  90. * Returns a specific product from the cart.
  91. *
  92. * The returned data is not an iThemes Exchange Product object. It is a cart-product
  93. *
  94. * @since 0.3.7
  95. * @param mixed $id id for the cart's product data
  96. * @return mixed
  97. */
  98. function it_exchange_get_cart_product( $id ) {
  99. if ( ! $products = it_exchange_get_cart_products() )
  100. return false;
  101. if ( empty( $products[$id] ) )
  102. return false;
  103. return apply_filters( 'it_exchange_get_cart_product', $products[$id], $id );
  104. }
  105. /**
  106. * Checks if the current product being viewd is in the cart
  107. *
  108. * @since 0.4.10
  109. * @return bool true if in cart|false if not
  110. */
  111. function it_exchange_is_current_product_in_cart() {
  112. $product_id = false;
  113. $cart_products = it_exchange_get_cart_products();
  114. $product = empty( $GLOBALS['post'] ) ? false : it_exchange_get_product( $GLOBALS['post'] );
  115. if ( ! empty( $product ) )
  116. $product_id = $product->ID;
  117. else if ( ! empty( $_GET['sw-product'] ) )
  118. $product_id = $_GET['sw-product'];
  119. foreach( $cart_products as $cart_product ) {
  120. if ( $product_id == $cart_product['product_id'] )
  121. return true;
  122. }
  123. return false;
  124. }
  125. /**
  126. * Adds a product to the shopping cart based on the product_id
  127. *
  128. * @since 0.3.7
  129. * @param string $product_id a valid wp post id with an iThemes Exchange product post_typp
  130. * @param int $quantity (optional) how many?
  131. * return boolean
  132. */
  133. function it_exchange_add_product_to_shopping_cart( $product_id, $quantity=1 ) {
  134. if ( ! $product_id )
  135. return;
  136. if ( ! $product = it_exchange_get_product( $product_id ) )
  137. return;
  138. $quantity = absint( (int) $quantity );
  139. if ( $quantity < 1 )
  140. $quantity = 1; //we're going to assume they want at least 1 item
  141. /**
  142. * The default shopping cart organizes products in the cart by product_id and a hash of 'itemized_data'.
  143. * Any data like product variants or pricing mods that should separate products in the cart can be passed through this filter.
  144. */
  145. $itemized_data = apply_filters( 'it_exchange_add_itemized_data_to_cart_product', array(), $product_id );
  146. if ( ! is_serialized( $itemized_data ) )
  147. $itemized_data = maybe_serialize( $itemized_data );
  148. $itemized_hash = md5( $itemized_data );
  149. /**
  150. * Any data that needs to be stored in the cart for this product but that should not trigger a new itemized row in the cart
  151. */
  152. $additional_data = apply_filters( 'it_exchange_add_additional_data_to_cart_product', array(), $product_id );
  153. if ( ! is_serialized( $additional_data ) )
  154. $additional_data = maybe_serialize( $additional_data );
  155. // Doe we have anything in the cart already?
  156. $session_products = it_exchange_get_cart_products();
  157. /**
  158. * If multi-item carts are allowed, don't do antying here.
  159. * If multi-item carts are NOT allowed and this is a different item, empty the cart before proceeding.
  160. * If item being added to cart is already in cart, preserve that item so that quanity will be bumpped.
  161. */
  162. if ( ! it_exchange_is_multi_item_cart_allowed() || ! it_exchange_is_multi_item_product_allowed( $product_id ) ) {
  163. if ( ! empty( $session_products ) ) {
  164. // Preserve the current item being added if its already in the cart
  165. if ( ! empty( $session_products[$product_id . '-' . $itemized_hash] ) )
  166. $preserve_for_quantity_bump = $session_products[$product_id . '-' . $itemized_hash];
  167. // Empty the cart to ensure only one item
  168. it_exchange_empty_shopping_cart();
  169. // Add the existing item back if found
  170. if ( ! empty( $preserve_for_quantity_bump ) )
  171. it_exchange_add_cart_product( $preserve_for_quantity_bump['product_cart_id'], $preserve_for_quantity_bump );
  172. // Reset the session products
  173. $session_products = it_exchange_get_cart_products();
  174. }
  175. }
  176. // If product is in cart already, bump the quanity. Otherwise, add it to the cart
  177. if ( ! empty ($session_products[$product_id . '-' . $itemized_hash] ) ) {
  178. $product = $session_products[$product_id . '-' . $itemized_hash];
  179. return it_exchange_update_cart_product_quantity( $product_id . '-' . $itemized_hash, $quantity );
  180. } else {
  181. // If we don't support purchase quanity, quanity will always be 1
  182. if ( it_exchange_product_supports_feature( $product_id, 'purchase-quantity' ) ) {
  183. // Get max quantity setting
  184. $max_purchase_quantity = it_exchange_get_product_feature( $product_id, 'purchase-quantity' );
  185. $max_purchase_quantity = apply_filters( 'it_exchange_max_purchase_quantity_cart_check', $max_purchase_quantity, $product_id, $itemized_data, $additional_data, $itemized_hash );
  186. $count = ( $max_purchase_quantity && $quantity > $max_purchase_quantity ) ? $max_purchase_quantity : $quantity;
  187. } else {
  188. $count = 1;
  189. }
  190. $product = array(
  191. 'product_cart_id' => $product_id . '-' . $itemized_hash,
  192. 'product_id' => $product_id,
  193. 'itemized_data' => $itemized_data,
  194. 'additional_data' => $additional_data,
  195. 'itemized_hash' => $itemized_hash,
  196. 'count' => $count,
  197. );
  198. it_exchange_add_cart_product( $product_id . '-' . $itemized_hash, $product );
  199. do_action( 'it_exchange_product_added_to_cart', $product_id );
  200. return true;
  201. }
  202. return false;
  203. }
  204. /**
  205. * Updates the quantity for a specific cart item
  206. *
  207. * @since 0.4.0
  208. * @param int $cart_product_id the product ID prepended to the itemized hash by a hyphen
  209. * @param int $quantity the incoming quantity
  210. * @param boolean $add_to_existing if set to false, it replaces the existing.
  211. * @return void
  212. */
  213. function it_exchange_update_cart_product_quantity( $cart_product_id, $quantity, $add_to_existing=true ) {
  214. // Get cart products
  215. $cart_products = it_exchange_get_cart_products();
  216. // Update Quantity
  217. if ( ! empty( $cart_products[$cart_product_id] ) && is_numeric( $quantity ) ) {
  218. $cart_product = $cart_products[$cart_product_id];
  219. if ( empty( $quantity ) || $quantity < 1 ) {
  220. it_exchange_delete_cart_product( $cart_product_id );
  221. } else {
  222. // If we don't support purchase quanity, quanity will always be 1
  223. if ( it_exchange_product_supports_feature( $cart_product['product_id'], 'purchase-quantity' ) ) {
  224. // Get max quantity setting
  225. $max_purchase_quantity = it_exchange_get_product_feature( $cart_product['product_id'], 'purchase-quantity' );
  226. // Zero out existing if we're not adding incoming quantity to it.
  227. if ( ! $add_to_existing )
  228. $cart_product['count'] = 0;
  229. // If we support it but don't have it, quantity is unlimited
  230. if ( ! $max_purchase_quantity )
  231. $cart_product['count'] = $cart_product['count'] + $quantity;
  232. else
  233. $cart_product['count'] = ( ( $cart_product['count'] + $quantity ) > $max_purchase_quantity ) ? $max_purchase_quantity : $quantity + $cart_product['count'];
  234. } else {
  235. $cart_product['count'] = 1;
  236. }
  237. it_exchange_update_cart_product( $cart_product_id, $cart_product );
  238. do_action( 'it_exchange_cart_prouduct_count_updated', $cart_product_id );
  239. return true;
  240. }
  241. }
  242. }
  243. /**
  244. * Empties the cart
  245. *
  246. * @since 0.3.7
  247. * @return boolean
  248. */
  249. function it_exchange_empty_shopping_cart() {
  250. it_exchange_clear_session_data( 'products' );
  251. do_action( 'it_exchange_empty_shopping_cart' );
  252. }
  253. /**
  254. * Caches the user's cart in user meta if they are logged in
  255. *
  256. * @since 1.9.0
  257. *
  258. * @return void
  259. */
  260. function it_exchange_cache_customer_cart( $customer_id=false ) {
  261. // Grab the current customer
  262. $customer = empty( $customer_id ) ? it_exchange_get_current_customer() : new IT_Exchange_Customer( $customer_id );
  263. // Abort if we don't have a logged in customer
  264. if ( empty( $customer->id ) )
  265. return;
  266. $cart_data = it_exchange_get_cart_data();
  267. update_user_meta( $customer->id, '_it_exchange_cached_cart', $cart_data );
  268. do_action( 'it_exchange_cache_customer_cart', $customer, $cart_data );
  269. }
  270. /**
  271. * Get a customer's cached cart if they are logged in
  272. *
  273. * @since 1.9.0
  274. *
  275. * @return void
  276. */
  277. function it_exchange_get_cached_customer_cart( $id=false ) {
  278. // Grab the current customer
  279. $customer = empty( $id ) ? it_exchange_get_current_customer() : new IT_Exchange_Customer( $id );
  280. // Abort if we don't have a logged in customer
  281. if ( empty( $customer->id ) )
  282. return false;
  283. // Grab the data
  284. $cart = get_user_meta( $customer->id, '_it_exchange_cached_cart', true );
  285. return apply_filters( 'it_exchange_get_chached_customer_cart', $cart, $customer->id );
  286. }
  287. /**
  288. * Add a session ID to the list of active customer cart sessions
  289. *
  290. * @since 1.9.0
  291. *
  292. * @return void
  293. */
  294. function it_exchange_add_current_session_to_customer_active_carts( $customer_id=false ) {
  295. $customer_id = empty( $customer_id ) ? it_exchange_get_current_customer_id() : $customer_id;
  296. // Grab the current customer
  297. $customer = it_exchange_get_customer( $customer_id );
  298. // Abort if we don't have a logged in customer
  299. if ( empty( $customer->id ) )
  300. return false;
  301. // Get the current customer's session ID
  302. $current_session_string = it_exchange_get_session_id();
  303. $current_session_parts = explode( '||', $current_session_string );
  304. $current_session_id = empty( $current_session_parts[0] ) ? false : $current_session_parts[0];
  305. $current_session_expires = empty( $current_session_parts[1] ) ? false : $current_session_parts[1];
  306. // Get all active carts for customer (across devices / browsers )
  307. $active_carts = it_exchange_get_active_carts_for_customer( false, $customer->id );
  308. // Add or update current session data to active sessions
  309. if ( ! isset( $active_carts[$current_session_id] ) || ( isset( $active_carts[$current_session_id] ) && $active_carts[$current_session_id] < time() ) ) {
  310. $active_carts[$current_session_id] = $current_session_expires;
  311. // Update user meta
  312. if ( empty( $_GLOBALS['it_exchange']['logging_out_user'] ) ) {
  313. update_user_meta( $customer->id, '_it_exchange_active_user_carts', $active_carts );
  314. }
  315. }
  316. }
  317. /**
  318. * Remove session from a customer's active carts
  319. *
  320. * @since 1.9.0
  321. *
  322. * @param int $customer_id the customer id
  323. * @param string $session_id the session id to remove
  324. * @return void
  325. */
  326. function it_exchange_remove_current_session_from_customer_active_carts() {
  327. // This works because it doesn't return the current cart in the list of active carts
  328. $active_carts = it_exchange_get_active_carts_for_customer();
  329. update_user_meta( it_exchange_get_current_customer_id(), '_it_exchange_active_user_carts', $active_carts );
  330. }
  331. /**
  332. * Grabs current active Users carts
  333. *
  334. * @since @1.9.0
  335. *
  336. * @param boolean $include_current_cart defaultst to false
  337. * @param int $customer_id optional. uses current customer id if null
  338. * @return array
  339. */
  340. function it_exchange_get_active_carts_for_customer( $include_current_cart=false, $customer_id=null ) {
  341. // Get the customer
  342. $customer = is_null( $customer_id ) ? it_exchange_get_current_customer() : it_exchange_get_customer( $customer_id );
  343. // Abort if we don't have a logged in customer
  344. if ( empty( $customer->id ) )
  345. return apply_filters( 'it_exchange_get_active_carts_for_customer', array(), $customer_id );
  346. // Get current session ID
  347. $current_session_string = it_exchange_get_session_id();
  348. $current_session_parts = explode( '||', $current_session_string );
  349. $current_session_id = empty( $current_session_parts[0] ) ? false : $current_session_parts[0];
  350. $current_session_exp = empty( $current_session_parts[1] ) ? false : $current_session_parts[1];
  351. // Grab saved active sessions from user meta
  352. $active_carts = get_user_meta( $customer->id, '_it_exchange_active_user_carts', true );
  353. // If active_carts is false, this is probably the first call with no previously active carts, so add the current one.
  354. if ( empty( $active_carts ) )
  355. $active_carts = array( $current_session_id => $current_session_exp );
  356. // Current time
  357. $time = time();
  358. // Loop through active sessions
  359. foreach( (array) $active_carts as $session_id => $expires ) {
  360. // Remove expired carts
  361. if ( $time > $expires )
  362. unset( $active_carts[$session_id] );
  363. }
  364. // Remove current cart if not needed
  365. if ( empty( $include_current_cart ) && isset( $active_carts[$current_session_id] ) )
  366. unset( $active_carts[$current_session_id] );
  367. return apply_filters( 'it_exchange_get_active_carts_for_customer', $active_carts, $customer_id );
  368. }
  369. /**
  370. * Loads a cached cart into active session
  371. *
  372. * @since 1.9.0
  373. *
  374. * @return void
  375. */
  376. function it_exchange_merge_cached_customer_cart_into_current_session( $user_login, $user ) {
  377. // Grab the current customer
  378. $customer = it_exchange_get_customer( $user->ID );
  379. // Abort if we don't have a logged in customer
  380. if ( empty( $customer->id ) )
  381. return false;
  382. // Current Cart Products prior to merge
  383. $current_products = it_exchange_get_cart_products();
  384. // Grab cached cart data and insert into current sessio
  385. $cached_cart = it_exchange_get_cached_customer_cart( $customer->id );
  386. /**
  387. * Loop through data. Override non-product data.
  388. * If product exists in current cart, bump the quantity
  389. */
  390. foreach( (array) $cached_cart as $key => $data ) {
  391. if ( 'products' != $key || empty( $current_products ) ) {
  392. it_exchange_update_cart_data( $key, $data );
  393. } else {
  394. foreach( (array) $data as $product_id => $product_data ) {
  395. if ( ! empty( $current_products[$product_id]['count'] ) ) {
  396. $data[$product_id]['count'] = $data[$product_id]['count'] + $current_products[$product_id]['count'];
  397. unset( $current_products[$product_id] );
  398. }
  399. }
  400. // If current products hasn't been absored into cached cart, tack it to cached cart and load cached cart into current session
  401. if ( is_array( $current_products ) && ! empty ( $current_products ) ) {
  402. foreach( $current_products as $product_id => $product_atts ) {
  403. $data[$product_id] = $product_atts;
  404. }
  405. }
  406. it_exchange_update_cart_data( 'products', $data );
  407. }
  408. }
  409. // This is a new customer session after loggin in so add this session to active carts
  410. it_exchange_add_current_session_to_customer_active_carts( $customer->id );
  411. // If there are items in the cart, cache and sync
  412. if ( it_exchange_get_cart_products() ) {
  413. it_exchange_cache_customer_cart( $customer->id );
  414. it_exchange_sync_current_cart_with_all_active_customer_carts();
  415. }
  416. }
  417. /**
  418. * Syncs the current cart with all other active carts
  419. *
  420. * @since 1.9.0
  421. *
  422. * @return void
  423. */
  424. function it_exchange_sync_current_cart_with_all_active_customer_carts() {
  425. $active_carts = it_exchange_get_active_carts_for_customer();
  426. $current_cart_data = it_exchange_get_cart_data();
  427. // Sync across browsers and devices
  428. foreach( (array) $active_carts as $session_id => $expiration ) {
  429. update_option( '_it_exchange_db_session_' . $session_id, $current_cart_data );
  430. }
  431. }
  432. /**
  433. * Are multi item carts allowed?
  434. *
  435. * Default is no. Addons must tell us yes as well as provide any pages needed for a cart / checkout / etc.
  436. *
  437. * @since 0.4.0
  438. * @return boolean
  439. */
  440. function it_exchange_is_multi_item_cart_allowed() {
  441. return apply_filters( 'it_exchange_multi_item_cart_allowed', false );
  442. }
  443. /**
  444. * Is this product allowed to be added to a multi-item cart?
  445. *
  446. * Default is true.
  447. *
  448. * @since 1.3.0
  449. * @return boolean
  450. */
  451. function it_exchange_is_multi_item_product_allowed( $product_id ) {
  452. return apply_filters( 'it_exchange_multi_item_product_allowed', true, $product_id );
  453. }
  454. /**
  455. * Returns the title for a cart product
  456. *
  457. * Other add-ons may need to modify the DB title to reflect variants / etc
  458. *
  459. * @since 0.3.7
  460. * @param array $product cart product
  461. * @return string product title
  462. */
  463. function it_exchange_get_cart_product_title( $product ) {
  464. if ( ! $db_product = it_exchange_get_product( $product['product_id'] ) )
  465. return false;
  466. $title = get_the_title( $db_product->ID );
  467. return apply_filters( 'it_exchange_get_cart_product_title', $title, $product );
  468. }
  469. /**
  470. * Returns the quantity for a cart product
  471. *
  472. * @since 0.3.7
  473. * @param array $product cart product
  474. * @return integer quantity
  475. */
  476. function it_exchange_get_cart_product_quantity( $product ) {
  477. $count = empty( $product['count'] ) ? 0 : $product['count'];
  478. return apply_filters( 'it_exchange_get_cart_product_quantity', $count, $product );
  479. }
  480. /**
  481. * Returns the quantity for a cart product
  482. *
  483. * @since 0.4.4
  484. * @param int $product ID
  485. * @return integer quantity
  486. */
  487. function it_exchange_get_cart_product_quantity_by_product_id( $product_id ) {
  488. $products = it_exchange_get_cart_products();
  489. foreach ( $products as $product ) {
  490. if ( $product['product_id'] == $product_id )
  491. return $product['count'];
  492. }
  493. return 0;
  494. }
  495. /**
  496. * Returns the number of items in the cart
  497. * Now including quantity for individual items w/ true_count flag
  498. *
  499. * @since 0.4.0
  500. *
  501. * @param bool $true_count Whether or not to traverse cart products to get true count of items
  502. * @return integer
  503. */
  504. function it_exchange_get_cart_products_count( $true_count=false ) {
  505. if ( $true_count ) {
  506. $count = 0;
  507. $products = it_exchange_get_cart_products();
  508. foreach( $products as $product ) {
  509. $count += $product['count'];
  510. }
  511. return absint( $count );
  512. }
  513. return absint( count( it_exchange_get_cart_products() ) );
  514. }
  515. /**
  516. * Returns the base_price for the cart product
  517. *
  518. * Other add-ons may modify this on the fly based on the product's itemized_data and additional_data arrays
  519. *
  520. * @since 0.3.7
  521. * @param array $product cart product
  522. * @return integer quantity
  523. */
  524. function it_exchange_get_cart_product_base_price( $product, $format=true ) {
  525. if ( ! $db_product = it_exchange_get_product( $product['product_id'] ) )
  526. return false;
  527. // Get the price from the DB
  528. $db_base_price = it_exchange_get_product_feature( $db_product->ID, 'base-price' );
  529. if ( $format )
  530. $db_base_price = it_exchange_format_price( $db_base_price );
  531. return apply_filters( 'it_exchange_get_cart_product_base_price', $db_base_price, $product, $format );
  532. }
  533. /**
  534. * Returns the subtotal for a cart product
  535. *
  536. * Base price multiplied by quantity and then passed through a filter
  537. *
  538. * @since 0.3.7
  539. * @param array $product cart product
  540. * @return mixed subtotal
  541. */
  542. function it_exchange_get_cart_product_subtotal( $product, $format=true ) {
  543. $base_price = it_exchange_get_cart_product_base_price( $product, false );
  544. $subtotal_price = apply_filters( 'it_exchange_get_cart_product_subtotal', $base_price * $product['count'], $product );
  545. if ( $format )
  546. $subtotal_price = it_exchange_format_price( $subtotal_price );
  547. return $subtotal_price;
  548. }
  549. /**
  550. * Returns the cart subtotal
  551. *
  552. * @since 0.3.7
  553. * @return mixed subtotal of cart
  554. */
  555. function it_exchange_get_cart_subtotal( $format=true ) {
  556. $subtotal = 0;
  557. if ( ! $products = it_exchange_get_cart_products() )
  558. return 0;
  559. foreach( (array) $products as $product ) {
  560. $subtotal += it_exchange_get_cart_product_subtotal( $product, false );
  561. }
  562. $subtotal = apply_filters( 'it_exchange_get_cart_subtotal', $subtotal );
  563. if ( $format )
  564. $subtotal = it_exchange_format_price( $subtotal );
  565. return $subtotal;
  566. }
  567. /**
  568. * Returns the cart total
  569. *
  570. * The cart total is essentailly going to be the sub_total plus whatever motifications other add-ons make to it.
  571. * eg: taxes, shipping, discounts, etc.
  572. *
  573. * @since 0.3.7
  574. * @return mixed total of cart
  575. */
  576. function it_exchange_get_cart_total( $format=true ) {
  577. $total = apply_filters( 'it_exchange_get_cart_total', it_exchange_get_cart_subtotal( false ) );
  578. if ( 0 > $total )
  579. $total = 0;
  580. if ( $format )
  581. $total = it_exchange_format_price( $total );
  582. return $total;
  583. }
  584. /**
  585. * Returns the cart description
  586. *
  587. * The cart description is essentailly going to be a list of all products being purchased
  588. *
  589. * @since 0.4.0
  590. * @return mixed total of cart
  591. */
  592. function it_exchange_get_cart_description() {
  593. $description = array();
  594. if ( ! $products = it_exchange_get_cart_products() )
  595. return 0;
  596. foreach( (array) $products as $product ) {
  597. $string = it_exchange_get_cart_product_title( $product );
  598. if ( 1 < $count = it_exchange_get_cart_product_quantity( $product ) )
  599. $string .= ' (' . $count . ')';
  600. $description[] = $string;
  601. }
  602. $description = apply_filters( 'it_exchange_get_cart_description', implode( ', ', $description ), $description );
  603. return $description;
  604. }
  605. /**
  606. * Redirect to confirmation page after successfull transaction
  607. *
  608. * @since 0.3.7
  609. * @param integer $transaction_id the transaction id
  610. * @return void
  611. */
  612. function it_exchange_do_confirmation_redirect( $transaction_id ) {
  613. $confirmation_url = it_exchange_get_page_url( 'confirmation' );
  614. $transaction_var = it_exchange_get_field_name( 'transaction_id' );
  615. $confirmation_url = add_query_arg( array( $transaction_var => $transaction_id ), $confirmation_url );
  616. $redirect_options = array( 'transaction_id' => $transaction_id, 'transaction_var' => $transaction_var );
  617. it_exchange_redirect( $confirmation_url, 'confirmation-redirect', $redirect_options );
  618. die();
  619. }
  620. /**
  621. * Returns the nonce field for the cart
  622. *
  623. * @since 0.4.0
  624. *
  625. * @return string
  626. */
  627. function it_exchange_get_cart_nonce_field() {
  628. $var = apply_filters( 'it_exchange_cart_action_nonce_var', '_wpnonce' );
  629. return wp_nonce_field( 'it-exchange-cart-action-' . it_exchange_get_session_id(), $var, true, false );
  630. }
  631. /**
  632. * Returns the shipping address values for the cart
  633. *
  634. * @since 1.4.0
  635. *
  636. * @return array
  637. */
  638. function it_exchange_get_cart_shipping_address() {
  639. // If user is logged in, grab their data
  640. $customer = it_exchange_get_current_customer();
  641. $customer_data = empty( $customer->data ) ? new stdClass() : $customer->data;
  642. // Default values for first time use.
  643. $defaults = array(
  644. 'first-name' => empty( $customer_data->first_name ) ? '' : $customer_data->first_name,
  645. 'last-name' => empty( $customer_data->last_name ) ? '' : $customer_data->last_name,
  646. 'company-name' => '',
  647. 'address1' => '',
  648. 'address2' => '',
  649. 'city' => '',
  650. 'state' => '',
  651. 'zip' => '',
  652. 'country' => '',
  653. 'email' => empty( $customer_data->user_email ) ? '' : $customer_data->user_email,
  654. 'phone' => '',
  655. );
  656. // See if the customer has a shipping address saved. If so, overwrite defaults with saved shipping address
  657. if ( ! empty( $customer_data->shipping_address ) )
  658. $defaults = ITUtility::merge_defaults( $customer_data->shipping_address, $defaults );
  659. // If data exists in the session, use that as the most recent
  660. $session_data = it_exchange_get_cart_data( 'shipping-address' );
  661. $cart_shipping = ITUtility::merge_defaults( $session_data, $defaults );
  662. // If shipping error and form was submitted, use POST values as most recent
  663. if ( ! empty( $_REQUEST['it-exchange-update-shipping-address'] ) && ! empty( $GLOBALS['it_exchange']['shipping-address-error'] ) ) {
  664. $keys = array_keys( $defaults );
  665. $post_shipping = array();
  666. foreach( $keys as $key ) {
  667. $post_shipping[$key] = empty( $_REQUEST['it-exchange-shipping-address-' . $key] ) ? '' : $_REQUEST['it-exchange-shipping-address-' . $key];
  668. }
  669. $cart_shipping = ITUtility::merge_defaults( $post_shipping, $cart_shipping );
  670. }
  671. return apply_filters( 'it_exchange_get_cart_shipping_address', $cart_shipping );
  672. }
  673. /**
  674. * Returns the billing address values for the cart
  675. *
  676. * @since 1.3.0
  677. *
  678. * @return array
  679. */
  680. function it_exchange_get_cart_billing_address() {
  681. // If user is logged in, grab their data
  682. $customer = it_exchange_get_current_customer();
  683. $customer_data = empty( $customer->data ) ? new stdClass() : $customer->data;
  684. // Default values for first time use.
  685. $defaults = array(
  686. 'first-name' => empty( $customer_data->first_name ) ? '' : $customer_data->first_name,
  687. 'last-name' => empty( $customer_data->last_name ) ? '' : $customer_data->last_name,
  688. 'company-name' => '',
  689. 'address1' => '',
  690. 'address2' => '',
  691. 'city' => '',
  692. 'state' => '',
  693. 'zip' => '',
  694. 'country' => '',
  695. 'email' => empty( $customer_data->user_email ) ? '' : $customer_data->user_email,
  696. 'phone' => '',
  697. );
  698. // See if the customer has a billing address saved. If so, overwrite defaults with saved billing address
  699. if ( ! empty( $customer_data->billing_address ) )
  700. $defaults = ITUtility::merge_defaults( $customer_data->billing_address, $defaults );
  701. // If data exists in the session, use that as the most recent
  702. $session_data = it_exchange_get_cart_data( 'billing-address' );
  703. $cart_billing = ITUtility::merge_defaults( $session_data, $defaults );
  704. // If billing error and form was submitted, use POST values as most recent
  705. if ( ! empty( $_REQUEST['it-exchange-update-billing-address'] ) && ! empty( $GLOBALS['it_exchange']['billing-address-error'] ) ) {
  706. $keys = array_keys( $defaults );
  707. $post_billing = array();
  708. foreach( $keys as $key ) {
  709. $post_billing[$key] = empty( $_REQUEST['it-exchange-billing-address-' . $key] ) ? '' : $_REQUEST['it-exchange-billing-address-' . $key];
  710. }
  711. $cart_billing = ITUtility::merge_defaults( $post_billing, $cart_billing );
  712. }
  713. return apply_filters( 'it_exchange_get_cart_billing_address', $cart_billing );
  714. }