WC_Meta_Box_Coupon_Data

WC_Meta_Box_Coupon_Data Class.

Defined (1)

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

/includes/admin/meta-boxes/class-wc-meta-box-coupon-data.php  
  1. class WC_Meta_Box_Coupon_Data { 
  2.  
  3. /** 
  4. * Output the metabox. 
  5. * @param WP_Post $post 
  6. */ 
  7. public static function output( $post ) { 
  8. wp_nonce_field( 'woocommerce_save_data', 'woocommerce_meta_nonce' ); 
  9.  
  10. $coupon = new WC_Coupon( $post->ID ); 
  11. ?> 
  12. <style type="text/css"> 
  13. #edit-slug-box, #minor-publishing-actions { display:none } 
  14. </style> 
  15. <div id="coupon_options" class="panel-wrap coupon_data"> 
  16.  
  17. <div class="wc-tabs-back"></div> 
  18.  
  19. <ul class="coupon_data_tabs wc-tabs" style="display:none;"> 
  20. <?php 
  21. $coupon_data_tabs = apply_filters( 'woocommerce_coupon_data_tabs', array( 
  22. 'general' => array( 
  23. 'label' => __( 'General', 'woocommerce' ),  
  24. 'target' => 'general_coupon_data',  
  25. 'class' => 'general_coupon_data',  
  26. ),  
  27. 'usage_restriction' => array( 
  28. 'label' => __( 'Usage restriction', 'woocommerce' ),  
  29. 'target' => 'usage_restriction_coupon_data',  
  30. 'class' => '',  
  31. ),  
  32. 'usage_limit' => array( 
  33. 'label' => __( 'Usage limits', 'woocommerce' ),  
  34. 'target' => 'usage_limit_coupon_data',  
  35. 'class' => '',  
  36. ),  
  37. ) ); 
  38.  
  39. foreach ( $coupon_data_tabs as $key => $tab ) { 
  40. ?><li class="<?php echo $key; ?>_options <?php echo $key; ?>_tab <?php echo implode( ' ' , (array) $tab['class'] ); ?>"> 
  41. <a href="#<?php echo $tab['target']; ?>"><span><?php echo esc_html( $tab['label'] ); ?></span></a> 
  42. </li><?php 
  43. ?> 
  44. </ul> 
  45. <div id="general_coupon_data" class="panel woocommerce_options_panel"><?php 
  46.  
  47. // Type 
  48. woocommerce_wp_select( array( 
  49. 'id' => 'discount_type',  
  50. 'label' => __( 'Discount type', 'woocommerce' ),  
  51. 'options' => wc_get_coupon_types(),  
  52. ) ); 
  53.  
  54. // Amount 
  55. woocommerce_wp_text_input( array( 
  56. 'id' => 'coupon_amount',  
  57. 'label' => __( 'Coupon amount', 'woocommerce' ),  
  58. 'placeholder' => wc_format_localized_price( 0 ),  
  59. 'description' => __( 'Value of the coupon.', 'woocommerce' ),  
  60. 'data_type' => 'price',  
  61. 'desc_tip' => true,  
  62. ) ); 
  63.  
  64. // Free Shipping 
  65. if ( wc_shipping_enabled() ) { 
  66. woocommerce_wp_checkbox( array( 
  67. 'id' => 'free_shipping',  
  68. 'label' => __( 'Allow free shipping', 'woocommerce' ),  
  69. 'description' => sprintf( __( 'Check this box if the coupon grants free shipping. A <a href="%s" target="_blank">free shipping method</a> must be enabled in your shipping zone and be set to require "a valid free shipping coupon" (see the "Free Shipping Requires" setting).', 'woocommerce' ), 'https://docs.woocommerce.com/document/free-shipping/' ),  
  70. ) ); 
  71.  
  72. // Expiry date 
  73. $expiry_date = $coupon->get_date_expires() ? $coupon->get_date_expires()->date( 'Y-m-d' ) : ''; 
  74. woocommerce_wp_text_input( array( 
  75. 'id' => 'expiry_date',  
  76. 'value' => esc_attr( $expiry_date ),  
  77. 'label' => __( 'Coupon expiry date', 'woocommerce' ),  
  78. 'placeholder' => 'YYYY-MM-DD',  
  79. 'description' => '',  
  80. 'class' => 'date-picker',  
  81. 'custom_attributes' => array( 
  82. 'pattern' => apply_filters( 'woocommerce_date_input_html_pattern', '[0-9]{4}-(0[1-9]|1[012])-(0[1-9]|1[0-9]|2[0-9]|3[01])' ),  
  83. ),  
  84. ) ); 
  85.  
  86. do_action( 'woocommerce_coupon_options', $coupon->get_id(), $coupon ); 
  87.  
  88. ?></div> 
  89. <div id="usage_restriction_coupon_data" class="panel woocommerce_options_panel"><?php 
  90.  
  91. echo '<div class="options_group">'; 
  92.  
  93. // minimum spend 
  94. woocommerce_wp_text_input( array( 
  95. 'id' => 'minimum_amount',  
  96. 'label' => __( 'Minimum spend', 'woocommerce' ),  
  97. 'placeholder' => __( 'No minimum', 'woocommerce' ),  
  98. 'description' => __( 'This field allows you to set the minimum spend (subtotal, including taxes) allowed to use the coupon.', 'woocommerce' ),  
  99. 'data_type' => 'price',  
  100. 'desc_tip' => true,  
  101. ) ); 
  102.  
  103. // maximum spend 
  104. woocommerce_wp_text_input( array( 
  105. 'id' => 'maximum_amount',  
  106. 'label' => __( 'Maximum spend', 'woocommerce' ),  
  107. 'placeholder' => __( 'No maximum', 'woocommerce' ),  
  108. 'description' => __( 'This field allows you to set the maximum spend (subtotal, including taxes) allowed when using the coupon.', 'woocommerce' ),  
  109. 'data_type' => 'price',  
  110. 'desc_tip' => true,  
  111. ) ); 
  112.  
  113. // Individual use 
  114. woocommerce_wp_checkbox( array( 
  115. 'id' => 'individual_use',  
  116. 'label' => __( 'Individual use only', 'woocommerce' ),  
  117. 'description' => __( 'Check this box if the coupon cannot be used in conjunction with other coupons.', 'woocommerce' ),  
  118. ) ); 
  119.  
  120. // Exclude Sale Products 
  121. woocommerce_wp_checkbox( array( 
  122. 'id' => 'exclude_sale_items',  
  123. 'label' => __( 'Exclude sale items', 'woocommerce' ),  
  124. 'description' => __( 'Check this box if the coupon should not apply to items on sale. Per-item coupons will only work if the item is not on sale. Per-cart coupons will only work if there are no sale items in the cart.', 'woocommerce' ),  
  125. ) ); 
  126.  
  127. echo '</div><div class="options_group">'; 
  128.  
  129. // Product ids 
  130. ?> 
  131. <p class="form-field"><label><?php _e( 'Products', 'woocommerce' ); ?></label> 
  132. <select class="wc-product-search" multiple="multiple" style="width: 50%;" name="product_ids[]" data-placeholder="<?php esc_attr_e( 'Search for a product…', 'woocommerce' ); ?>" data-action="woocommerce_json_search_products_and_variations"> 
  133. <?php 
  134. $product_ids = $coupon->get_product_ids(); 
  135.  
  136. foreach ( $product_ids as $product_id ) { 
  137. $product = wc_get_product( $product_id ); 
  138. if ( is_object( $product ) ) { 
  139. echo '<option value="' . esc_attr( $product_id ) . '"' . selected( true, true, false ) . '>' . wp_kses_post( $product->get_formatted_name() ) . '</option>'; 
  140. ?> 
  141. </select> <?php echo wc_help_tip( __( 'Products which need to be in the cart to use this coupon or, for "Product Discounts", which products are discounted.', 'woocommerce' ) ); ?></p> 
  142. <?php 
  143.  
  144. // Exclude Product ids 
  145. ?> 
  146. <p class="form-field"><label><?php _e( 'Exclude products', 'woocommerce' ); ?></label> 
  147. <select class="wc-product-search" multiple="multiple" style="width: 50%;" name="exclude_product_ids[]" data-placeholder="<?php esc_attr_e( 'Search for a product…', 'woocommerce' ); ?>" data-action="woocommerce_json_search_products_and_variations"> 
  148. <?php 
  149. $product_ids = $coupon->get_excluded_product_ids(); 
  150.  
  151. foreach ( $product_ids as $product_id ) { 
  152. $product = wc_get_product( $product_id ); 
  153. if ( is_object( $product ) ) { 
  154. echo '<option value="' . esc_attr( $product_id ) . '"' . selected( true, true, false ) . '>' . wp_kses_post( $product->get_formatted_name() ) . '</option>'; 
  155. ?> 
  156. </select> <?php echo wc_help_tip( __( 'Products which must not be in the cart to use this coupon or, for "Product Discounts", which products are not discounted.', 'woocommerce' ) ); ?></p> 
  157. <?php 
  158.  
  159. echo '</div><div class="options_group">'; 
  160.  
  161. // Categories 
  162. ?> 
  163. <p class="form-field"><label for="product_categories"><?php _e( 'Product categories', 'woocommerce' ); ?></label> 
  164. <select id="product_categories" name="product_categories[]" style="width: 50%;" class="wc-enhanced-select" multiple="multiple" data-placeholder="<?php esc_attr_e( 'Any category', 'woocommerce' ); ?>"> 
  165. <?php 
  166. $category_ids = $coupon->get_product_categories(); 
  167. $categories = get_terms( 'product_cat', 'orderby=name&hide_empty=0' ); 
  168.  
  169. if ( $categories ) foreach ( $categories as $cat ) { 
  170. echo '<option value="' . esc_attr( $cat->term_id ) . '"' . selected( in_array( $cat->term_id, $category_ids ), true, false ) . '>' . esc_html( $cat->name ) . '</option>'; 
  171. ?> 
  172. </select> <?php echo wc_help_tip( __( 'A product must be in this category for the coupon to remain valid or, for "Product Discounts", products in these categories will be discounted.', 'woocommerce' ) ); ?></p> 
  173. <?php 
  174.  
  175. // Exclude Categories 
  176. ?> 
  177. <p class="form-field"><label for="exclude_product_categories"><?php _e( 'Exclude categories', 'woocommerce' ); ?></label> 
  178. <select id="exclude_product_categories" name="exclude_product_categories[]" style="width: 50%;" class="wc-enhanced-select" multiple="multiple" data-placeholder="<?php esc_attr_e( 'No categories', 'woocommerce' ); ?>"> 
  179. <?php 
  180. $category_ids = $coupon->get_excluded_product_categories(); 
  181. $categories = get_terms( 'product_cat', 'orderby=name&hide_empty=0' ); 
  182.  
  183. if ( $categories ) foreach ( $categories as $cat ) { 
  184. echo '<option value="' . esc_attr( $cat->term_id ) . '"' . selected( in_array( $cat->term_id, $category_ids ), true, false ) . '>' . esc_html( $cat->name ) . '</option>'; 
  185. ?> 
  186. </select> <?php echo wc_help_tip( __( 'Product must not be in this category for the coupon to remain valid or, for "Product Discounts", products in these categories will not be discounted.', 'woocommerce' ) ); ?></p> 
  187. <?php 
  188.  
  189. echo '</div><div class="options_group">'; 
  190.  
  191. // Customers 
  192. woocommerce_wp_text_input( array( 
  193. 'id' => 'customer_email',  
  194. 'label' => __( 'Email restrictions', 'woocommerce' ),  
  195. 'placeholder' => __( 'No restrictions', 'woocommerce' ),  
  196. 'description' => __( 'List of allowed emails to check against the customer\'s billing email when an order is placed. Separate email addresses with commas.', 'woocommerce' ),  
  197. 'value' => implode( ', ', (array) get_post_meta( $post->ID, 'customer_email', true ) ),  
  198. 'desc_tip' => true,  
  199. 'type' => 'email',  
  200. 'class' => '',  
  201. 'custom_attributes' => array( 
  202. 'multiple' => 'multiple',  
  203. ),  
  204. ) ); 
  205.  
  206. echo '</div>'; 
  207.  
  208. do_action( 'woocommerce_coupon_options_usage_restriction', $coupon->get_id(), $coupon ); 
  209.  
  210. ?></div> 
  211. <div id="usage_limit_coupon_data" class="panel woocommerce_options_panel"><?php 
  212.  
  213. echo '<div class="options_group">'; 
  214.  
  215. // Usage limit per coupons 
  216. woocommerce_wp_text_input( array( 
  217. 'id' => 'usage_limit',  
  218. 'label' => __( 'Usage limit per coupon', 'woocommerce' ),  
  219. 'placeholder' => esc_attr__( 'Unlimited usage', 'woocommerce' ),  
  220. 'description' => __( 'How many times this coupon can be used before it is void.', 'woocommerce' ),  
  221. 'type' => 'number',  
  222. 'desc_tip' => true,  
  223. 'class' => 'short',  
  224. 'custom_attributes' => array( 
  225. 'step' => 1,  
  226. 'min' => 0,  
  227. ),  
  228. 'value' => $coupon->get_usage_limit() ? $coupon->get_usage_limit() : '',  
  229. ) ); 
  230.  
  231. // Usage limit per product 
  232. woocommerce_wp_text_input( array( 
  233. 'id' => 'limit_usage_to_x_items',  
  234. 'label' => __( 'Limit usage to X items', 'woocommerce' ),  
  235. 'placeholder' => esc_attr__( 'Apply to all qualifying items in cart', 'woocommerce' ),  
  236. 'description' => __( 'The maximum number of individual items this coupon can apply to when using product discounts. Leave blank to apply to all qualifying items in cart.', 'woocommerce' ),  
  237. 'desc_tip' => true,  
  238. 'class' => 'short',  
  239. 'type' => 'number',  
  240. 'custom_attributes' => array( 
  241. 'step' => 1,  
  242. 'min' => 0,  
  243. ),  
  244. 'value' => $coupon->get_limit_usage_to_x_items() ? $coupon->get_limit_usage_to_x_items() : '',  
  245. ) ); 
  246.  
  247. // Usage limit per users 
  248. woocommerce_wp_text_input( array( 
  249. 'id' => 'usage_limit_per_user',  
  250. 'label' => __( 'Usage limit per user', 'woocommerce' ),  
  251. 'placeholder' => esc_attr__( 'Unlimited usage', 'woocommerce' ),  
  252. 'description' => __( 'How many times this coupon can be used by an individual user. Uses billing email for guests, and user ID for logged in users.', 'woocommerce' ),  
  253. 'desc_tip' => true,  
  254. 'class' => 'short',  
  255. 'type' => 'number',  
  256. 'custom_attributes' => array( 
  257. 'step' => 1,  
  258. 'min' => 0,  
  259. ),  
  260. 'value' => $coupon->get_usage_limit_per_user() ? $coupon->get_usage_limit_per_user() : '',  
  261. ) ); 
  262.  
  263. echo '</div>'; 
  264.  
  265. do_action( 'woocommerce_coupon_options_usage_limit', $coupon->get_id(), $coupon ); 
  266.  
  267. ?></div> 
  268. <?php do_action( 'woocommerce_coupon_data_panels', $coupon->get_id(), $coupon ); ?> 
  269. <div class="clear"></div> 
  270. </div> 
  271. <?php 
  272.  
  273. /** 
  274. * Save meta box data. 
  275. * @param int $post_id 
  276. * @param WP_Post $post 
  277. */ 
  278. public static function save( $post_id, $post ) { 
  279. global $wpdb; 
  280.  
  281. // Check for dupe coupons 
  282. $coupon_code = wc_format_coupon_code( $post->post_title ); 
  283. $id_from_code = wc_get_coupon_id_by_code( $coupon_code, $post_id ); 
  284.  
  285. if ( $id_from_code ) { 
  286. WC_Admin_Meta_Boxes::add_error( __( 'Coupon code already exists - customers will use the latest coupon with this code.', 'woocommerce' ) ); 
  287.  
  288. $product_categories = isset( $_POST['product_categories'] ) ? (array) $_POST['product_categories'] : array(); 
  289. $exclude_product_categories = isset( $_POST['exclude_product_categories'] ) ? (array) $_POST['exclude_product_categories'] : array(); 
  290.  
  291. $coupon = new WC_Coupon( $post_id ); 
  292. $coupon->set_props( array( 
  293. 'code' => $post->post_title,  
  294. 'discount_type' => wc_clean( $_POST['discount_type'] ),  
  295. 'amount' => wc_format_decimal( $_POST['coupon_amount'] ),  
  296. 'date_expires' => wc_clean( $_POST['expiry_date'] ),  
  297. 'individual_use' => isset( $_POST['individual_use'] ),  
  298. 'product_ids' => isset( $_POST['product_ids'] ) ? array_filter( array_map( 'intval', (array) $_POST['product_ids'] ) ) : array(),  
  299. 'excluded_product_ids' => isset( $_POST['exclude_product_ids'] ) ? array_filter( array_map( 'intval', (array) $_POST['exclude_product_ids'] ) ) : array(),  
  300. 'usage_limit' => absint( $_POST['usage_limit'] ),  
  301. 'usage_limit_per_user' => absint( $_POST['usage_limit_per_user'] ),  
  302. 'limit_usage_to_x_items' => absint( $_POST['limit_usage_to_x_items'] ),  
  303. 'free_shipping' => isset( $_POST['free_shipping'] ),  
  304. 'product_categories' => array_filter( array_map( 'intval', $product_categories ) ),  
  305. 'excluded_product_categories' => array_filter( array_map( 'intval', $exclude_product_categories ) ),  
  306. 'exclude_sale_items' => isset( $_POST['exclude_sale_items'] ),  
  307. 'minimum_amount' => wc_format_decimal( $_POST['minimum_amount'] ),  
  308. 'maximum_amount' => wc_format_decimal( $_POST['maximum_amount'] ),  
  309. 'email_restrictions' => array_filter( array_map( 'trim', explode( ', ', wc_clean( $_POST['customer_email'] ) ) ) ),  
  310. ) ); 
  311. $coupon->save(); 
  312. do_action( 'woocommerce_coupon_options_save', $post_id, $coupon );