No Description

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

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