Нет описания

wc-coupon-functions.php 2.7KB

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