wc-coupon-functions.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * WooCommerce Coupons Functions
  4. *
  5. * Functions for coupon specific things.
  6. *
  7. * @package WooCommerce/Functions
  8. * @version 3.0.0
  9. */
  10. defined( 'ABSPATH' ) || exit;
  11. /**
  12. * Get coupon types.
  13. *
  14. * @return array
  15. */
  16. function wc_get_coupon_types() {
  17. return (array) apply_filters(
  18. 'woocommerce_coupon_discount_types', array(
  19. 'percent' => __( 'Percentage discount', 'woocommerce' ),
  20. 'fixed_cart' => __( 'Fixed cart discount', 'woocommerce' ),
  21. 'fixed_product' => __( 'Fixed product discount', 'woocommerce' ),
  22. )
  23. );
  24. }
  25. /**
  26. * Get a coupon type's name.
  27. *
  28. * @param string $type Coupon type.
  29. * @return string
  30. */
  31. function wc_get_coupon_type( $type = '' ) {
  32. $types = wc_get_coupon_types();
  33. return isset( $types[ $type ] ) ? $types[ $type ] : '';
  34. }
  35. /**
  36. * Coupon types that apply to individual products. Controls which validation rules will apply.
  37. *
  38. * @since 2.5.0
  39. * @return array
  40. */
  41. function wc_get_product_coupon_types() {
  42. return (array) apply_filters( 'woocommerce_product_coupon_types', array( 'fixed_product', 'percent' ) );
  43. }
  44. /**
  45. * Coupon types that apply to the cart as a whole. Controls which validation rules will apply.
  46. *
  47. * @since 2.5.0
  48. * @return array
  49. */
  50. function wc_get_cart_coupon_types() {
  51. return (array) apply_filters( 'woocommerce_cart_coupon_types', array( 'fixed_cart' ) );
  52. }
  53. /**
  54. * Check if coupons are enabled.
  55. * Filterable.
  56. *
  57. * @since 2.5.0
  58. *
  59. * @return bool
  60. */
  61. function wc_coupons_enabled() {
  62. return apply_filters( 'woocommerce_coupons_enabled', 'yes' === get_option( 'woocommerce_enable_coupons' ) );
  63. }
  64. /**
  65. * Get coupon code by ID.
  66. *
  67. * @since 3.0.0
  68. * @param int $id Coupon ID.
  69. * @return string
  70. */
  71. function wc_get_coupon_code_by_id( $id ) {
  72. $data_store = WC_Data_Store::load( 'coupon' );
  73. return (string) $data_store->get_code_by_id( $id );
  74. }
  75. /**
  76. * Get coupon code by ID.
  77. *
  78. * @since 3.0.0
  79. * @param string $code Coupon code.
  80. * @param int $exclude Used to exclude an ID from the check if you're checking existence.
  81. * @return int
  82. */
  83. function wc_get_coupon_id_by_code( $code, $exclude = 0 ) {
  84. $data_store = WC_Data_Store::load( 'coupon' );
  85. $ids = wp_cache_get( WC_Cache_Helper::get_cache_prefix( 'coupons' ) . 'coupon_id_from_code_' . $code, 'coupons' );
  86. if ( false === $ids ) {
  87. $ids = $data_store->get_ids_by_code( $code );
  88. if ( $ids ) {
  89. wp_cache_set( WC_Cache_Helper::get_cache_prefix( 'coupons' ) . 'coupon_id_from_code_' . $code, $ids, 'coupons' );
  90. }
  91. }
  92. $ids = array_diff( array_filter( array_map( 'absint', (array) $ids ) ), array( $exclude ) );
  93. return apply_filters( 'woocommerce_get_coupon_id_from_code', absint( current( $ids ) ), $code, $exclude );
  94. }