WJECF_Wrap_Coupon

Wrap a data object ( Coupons and products were converted to WC_Data since WC 2.7.0 ).

Defined (1)

The class is defined in the following location(s).

/includes/wjecf-wc.php  
  1. class WJECF_Wrap_Coupon extends WJECF_Wrap { 
  2.  
  3. public function exists() { 
  4. return $this->get_id() > 0; 
  5.  
  6. public function get_code() { 
  7. if ( is_callable( array( $this->object, 'get_code' ) ) ) { 
  8. return $this->object->get_code(); 
  9.  
  10. return $this->object->code; 
  11.  
  12. public function get_amount() { 
  13. if ( is_callable( array( $this->object, 'get_amount' ) ) ) { 
  14. return $this->object->get_amount(); 
  15.  
  16. return $this->object->coupon_amount; 
  17. }  
  18.  
  19. public function get_individual_use() { 
  20. if ( is_callable( array( $this->object, 'get_individual_use' ) ) ) { 
  21. return $this->object->get_individual_use(); 
  22.  
  23. return $this->object->individual_use == 'yes'; 
  24.  
  25. public function get_limit_usage_to_x_items() { 
  26. if ( is_callable( array( $this->object, 'get_limit_usage_to_x_items' ) ) ) { 
  27. return $this->object->get_limit_usage_to_x_items(); 
  28.  
  29. return $this->object->limit_usage_to_x_items; 
  30.  
  31. public function set_limit_usage_to_x_items( $limit_usage_to_x_items ) { 
  32. if ( is_callable( array( $this->object, 'set_limit_usage_to_x_items' ) ) ) { 
  33. $this->object->set_limit_usage_to_x_items( $limit_usage_to_x_items ); 
  34. } else { 
  35. $this->object->limit_usage_to_x_items = $limit_usage_to_x_items; 
  36.  
  37. public function get_discount_type() { 
  38. if ( is_callable( array( $this->object, 'get_discount_type' ) ) ) { 
  39. return $this->object->get_discount_type(); 
  40.  
  41. return $this->object->discount_type; 
  42. }  
  43.  
  44. public function set_discount_type( $discount_type ) { 
  45. if ( is_callable( array( $this->object, 'set_discount_type' ) ) ) { 
  46. $this->object->set_discount_type( $discount_type ); 
  47. } else { 
  48. $this->object->discount_type = $discount_type; 
  49. $this->object->type = $discount_type; 
  50.  
  51.  
  52. public function get_email_restrictions() { 
  53. if ( is_callable( array( $this->object, 'get_email_restrictions' ) ) ) { 
  54. return $this->object->get_email_restrictions(); 
  55.  
  56. return $this->object->customer_email; 
  57.  
  58. public function get_product_ids() { 
  59. if ( is_callable( array( $this->object, 'get_product_ids' ) ) ) { 
  60. return $this->object->get_product_ids(); 
  61.  
  62. return $this->object->product_ids; 
  63.  
  64. public function get_free_shipping() { 
  65. if ( is_callable( array( $this->object, 'get_free_shipping' ) ) ) { 
  66. return $this->object->get_free_shipping(); 
  67.  
  68. return $this->object->enable_free_shipping(); 
  69. }  
  70.  
  71. public function get_product_categories() { 
  72. if ( is_callable( array( $this->object, 'get_product_categories' ) ) ) { 
  73. return $this->object->get_product_categories(); 
  74.  
  75. return $this->object->product_categories; 
  76.  
  77. public function get_minimum_amount() { 
  78. if ( is_callable( array( $this->object, 'get_minimum_amount' ) ) ) { 
  79. return $this->object->get_minimum_amount(); 
  80.  
  81. return $this->object->minimum_amount; 
  82.  
  83. /** 
  84. * Set the product IDs this coupon cannot be used with. 
  85. * @since 2.4.2 (For WC3.0) 
  86. * @param array $excluded_product_ids 
  87. * @throws WC_Data_Exception 
  88. */ 
  89. public function set_excluded_product_ids( $excluded_product_ids ) { 
  90. if ( is_callable( array( $this->object, 'set_excluded_product_ids' ) ) ) { 
  91. $this->object->set_excluded_product_ids( $excluded_product_ids ); 
  92. } else { 
  93. //NOTE: Prior to WC2.7 it was called exclude_ instead of excluded_ 
  94. $this->object->exclude_product_ids = $excluded_product_ids; 
  95.  
  96. /** 
  97. * Set the product category IDs this coupon cannot be used with. 
  98. * @since 2.4.2 (For WC3.0) 
  99. * @param array $excluded_product_categories 
  100. * @throws WC_Data_Exception 
  101. */ 
  102. public function set_excluded_product_categories( $excluded_product_categories ) { 
  103. if ( is_callable( array( $this->object, 'set_excluded_product_categories' ) ) ) { 
  104. $this->object->set_excluded_product_categories( $excluded_product_categories ); 
  105. } else { 
  106. //NOTE: Prior to WC2.7 it was called exclude_ instead of excluded_ 
  107. $this->object->exclude_product_categories = $excluded_product_categories; 
  108.  
  109. /** 
  110. * Set if this coupon should excluded sale items or not. 
  111. * @since 2.4.2 (For WC3.0) 
  112. * @param bool $exclude_sale_items 
  113. * @throws WC_Data_Exception 
  114. */ 
  115. public function set_exclude_sale_items( $exclude_sale_items ) { 
  116. if ( is_callable( array( $this->object, 'set_exclude_sale_items' ) ) ) { 
  117. $this->object->set_exclude_sale_items( $exclude_sale_items ); 
  118. } else { 
  119. //NOTE: Prior to WC2.7 it was yes/no instead of boolean 
  120. $this->object->exclude_sale_items = $exclude_sale_items ? 'yes' : 'no'; 
  121. }  
  122.  
  123. /** 
  124. * Check the type of the coupon 
  125. * @param string|array $type The type(s) we want to check for 
  126. * @return bool True if the coupon is of the type 
  127. */ 
  128. public function is_type( $type ) { 
  129. //Backwards compatibility 2.2.11 
  130. if ( method_exists( $this->object, 'is_type' ) ) { 
  131. return $this->object->is_type( $type ); 
  132.  
  133. return ( $this->object->discount_type == $type || ( is_array( $type ) && in_array( $this->object->discount_type, $type ) ) ) ? true : false; 
  134. }  
  135.  
  136. /** 
  137. * Update single meta data item by meta key. 
  138. * Call save() afterwards! 
  139. * @since 2.4.0 
  140. * @param string $key 
  141. * @param mixed $value The value; use null to clear 
  142. */ 
  143. public function set_meta( $meta_key, $value ) { 
  144. if ( is_callable( array( $this->object, 'update_meta_data' ) ) ) {  
  145. if ( $value === null ) { 
  146. $this->object->delete_meta_data( $meta_key ); 
  147. } else { 
  148. $this->object->update_meta_data( $meta_key, $value ); 
  149. return; 
  150.  
  151. $this->maybe_get_custom_fields(); 
  152. //WJECF()->log('...setting legacy meta ' . $meta_key ); 
  153. $this->legacy_custom_fields[ $meta_key ] = $value; 
  154. $this->legacy_unsaved_keys[] = $meta_key; 
  155.  
  156. /** 
  157. * Save the metadata 
  158. * @return id of this object 
  159. */ 
  160. public function save() { 
  161. //WJECF()->log('Saving ' . $this->get_id() ); 
  162. if ( is_callable( array( $this->object, 'save' ) ) ) {  
  163. return $this->object->save(); 
  164.  
  165. //Save the unsaved... 
  166. foreach( $this->legacy_unsaved_keys as $meta_key ) { 
  167. //WJECF()->log('...saving legacy meta ' . $meta_key ); 
  168. $value = $this->legacy_custom_fields[ $meta_key ]; 
  169. if ( $value === null ) { 
  170. delete_post_meta( $this->get_id(), $meta_key ); 
  171. } else { 
  172. update_post_meta( $this->get_id(), $meta_key, $value ); 
  173. $this->legacy_unsaved_keys = array(); 
  174.  
  175. return $this->get_id(); 
  176.  
  177. protected $legacy_custom_fields = null; 
  178. protected $legacy_unsaved_keys = array(); 
  179.  
  180. protected function maybe_get_custom_fields() { 
  181. //Read custom fields if not yet done 
  182. if ( is_null( $this->legacy_custom_fields ) ) { 
  183. $this->legacy_custom_fields = $this->object->coupon_custom_fields; 
  184.  
  185. protected function get_meta_legacy( $meta_key, $single = true ) { 
  186. //Read custom fields if not yet done 
  187. $this->maybe_get_custom_fields(); 
  188.  
  189. if ( isset( $this->legacy_custom_fields[ $meta_key ] ) ) { 
  190. $values = $this->legacy_custom_fields[ $meta_key ]; 
  191. //WP_CLI::log( "LEGACY:" . print_r( $values, true )); 
  192. if ($single) { 
  193. return maybe_unserialize( reset( $values ) ); //reset yields the first 
  194. $values = array_map( 'maybe_unserialize', $values ); 
  195. return $values; 
  196.  
  197. return $single ? '' : array(); 
  198.