class-wc-legacy-coupon.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit;
  4. }
  5. /**
  6. * Legacy Coupon.
  7. *
  8. * Legacy and deprecated functions are here to keep the WC_Legacy_Coupon class clean.
  9. * This class will be removed in future versions.
  10. *
  11. * @class WC_Legacy_Coupon
  12. * @version 3.0.0
  13. * @package WooCommerce/Classes
  14. * @category Class
  15. * @author WooThemes
  16. */
  17. abstract class WC_Legacy_Coupon extends WC_Data {
  18. /**
  19. * Magic __isset method for backwards compatibility. Legacy properties which could be accessed directly in the past.
  20. * @param string $key
  21. * @return bool
  22. */
  23. public function __isset( $key ) {
  24. $legacy_keys = array(
  25. 'id',
  26. 'exists',
  27. 'coupon_custom_fields',
  28. 'type',
  29. 'discount_type',
  30. 'amount',
  31. 'coupon_amount',
  32. 'code',
  33. 'individual_use',
  34. 'product_ids',
  35. 'exclude_product_ids',
  36. 'usage_limit',
  37. 'usage_limit_per_user',
  38. 'limit_usage_to_x_items',
  39. 'usage_count',
  40. 'expiry_date',
  41. 'product_categories',
  42. 'exclude_product_categories',
  43. 'minimum_amount',
  44. 'maximum_amount',
  45. 'customer_email',
  46. );
  47. if ( in_array( $key, $legacy_keys ) ) {
  48. return true;
  49. }
  50. return false;
  51. }
  52. /**
  53. * Magic __get method for backwards compatibility. Maps legacy vars to new getters.
  54. * @param string $key
  55. * @return mixed
  56. */
  57. public function __get( $key ) {
  58. wc_doing_it_wrong( $key, 'Coupon properties should not be accessed directly.', '3.0' );
  59. switch ( $key ) {
  60. case 'id' :
  61. $value = $this->get_id();
  62. break;
  63. case 'exists' :
  64. $value = $this->get_id() > 0;
  65. break;
  66. case 'coupon_custom_fields' :
  67. $legacy_custom_fields = array();
  68. $custom_fields = $this->get_id() ? $this->get_meta_data() : array();
  69. if ( ! empty( $custom_fields ) ) {
  70. foreach ( $custom_fields as $cf_value ) {
  71. // legacy only supports 1 key
  72. $legacy_custom_fields[ $cf_value->key ][0] = $cf_value->value;
  73. }
  74. }
  75. $value = $legacy_custom_fields;
  76. break;
  77. case 'type' :
  78. case 'discount_type' :
  79. $value = $this->get_discount_type();
  80. break;
  81. case 'amount' :
  82. case 'coupon_amount' :
  83. $value = $this->get_amount();
  84. break;
  85. case 'code' :
  86. $value = $this->get_code();
  87. break;
  88. case 'individual_use' :
  89. $value = ( true === $this->get_individual_use() ) ? 'yes' : 'no';
  90. break;
  91. case 'product_ids' :
  92. $value = $this->get_product_ids();
  93. break;
  94. case 'exclude_product_ids' :
  95. $value = $this->get_excluded_product_ids();
  96. break;
  97. case 'usage_limit' :
  98. $value = $this->get_usage_limit();
  99. break;
  100. case 'usage_limit_per_user' :
  101. $value = $this->get_usage_limit_per_user();
  102. break;
  103. case 'limit_usage_to_x_items' :
  104. $value = $this->get_limit_usage_to_x_items();
  105. break;
  106. case 'usage_count' :
  107. $value = $this->get_usage_count();
  108. break;
  109. case 'expiry_date' :
  110. $value = ( $this->get_date_expires() ? $this->get_date_expires()->date( 'Y-m-d' ) : '' );
  111. break;
  112. case 'product_categories' :
  113. $value = $this->get_product_categories();
  114. break;
  115. case 'exclude_product_categories' :
  116. $value = $this->get_excluded_product_categories();
  117. break;
  118. case 'minimum_amount' :
  119. $value = $this->get_minimum_amount();
  120. break;
  121. case 'maximum_amount' :
  122. $value = $this->get_maximum_amount();
  123. break;
  124. case 'customer_email' :
  125. $value = $this->get_email_restrictions();
  126. break;
  127. default :
  128. $value = '';
  129. break;
  130. }
  131. return $value;
  132. }
  133. /**
  134. * Format loaded data as array.
  135. * @param string|array $array
  136. * @return array
  137. */
  138. public function format_array( $array ) {
  139. wc_deprecated_function( 'WC_Coupon::format_array', '3.0' );
  140. if ( ! is_array( $array ) ) {
  141. if ( is_serialized( $array ) ) {
  142. $array = maybe_unserialize( $array );
  143. } else {
  144. $array = explode( ',', $array );
  145. }
  146. }
  147. return array_filter( array_map( 'trim', array_map( 'strtolower', $array ) ) );
  148. }
  149. /**
  150. * Check if coupon needs applying before tax.
  151. *
  152. * @return bool
  153. */
  154. public function apply_before_tax() {
  155. wc_deprecated_function( 'WC_Coupon::apply_before_tax', '3.0' );
  156. return true;
  157. }
  158. /**
  159. * Check if a coupon enables free shipping.
  160. *
  161. * @return bool
  162. */
  163. public function enable_free_shipping() {
  164. wc_deprecated_function( 'WC_Coupon::enable_free_shipping', '3.0', 'WC_Coupon::get_free_shipping' );
  165. return $this->get_free_shipping();
  166. }
  167. /**
  168. * Check if a coupon excludes sale items.
  169. *
  170. * @return bool
  171. */
  172. public function exclude_sale_items() {
  173. wc_deprecated_function( 'WC_Coupon::exclude_sale_items', '3.0', 'WC_Coupon::get_exclude_sale_items' );
  174. return $this->get_exclude_sale_items();
  175. }
  176. /**
  177. * Increase usage count for current coupon.
  178. *
  179. * @param string $used_by Either user ID or billing email
  180. */
  181. public function inc_usage_count( $used_by = '' ) {
  182. $this->increase_usage_count( $used_by );
  183. }
  184. /**
  185. * Decrease usage count for current coupon.
  186. *
  187. * @param string $used_by Either user ID or billing email
  188. */
  189. public function dcr_usage_count( $used_by = '' ) {
  190. $this->decrease_usage_count( $used_by );
  191. }
  192. }