class-wc-meta-box-coupon-data.php 16 KB

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