2013-08-06 10:41:20 +00:00
< ? php
/**
2015-11-03 13:53:50 +00:00
* Coupon Data
2013-08-06 10:41:20 +00:00
*
* Display the coupon data meta box .
*
2014-08-31 07:18:21 +00:00
* @ author WooThemes
* @ category Admin
* @ package WooCommerce / Admin / Meta Boxes
2013-08-06 10:41:20 +00:00
* @ version 2.1 . 0
*/
2014-09-20 20:05:06 +00:00
if ( ! defined ( 'ABSPATH' ) ) {
exit ; // Exit if accessed directly
}
2013-08-06 10:41:20 +00:00
/**
2015-11-03 12:28:01 +00:00
* WC_Meta_Box_Coupon_Data Class .
2013-08-06 10:41:20 +00:00
*/
class WC_Meta_Box_Coupon_Data {
/**
2015-11-03 12:28:01 +00:00
* Output the metabox .
2016-01-04 21:31:36 +00:00
*
* @ param WP_Post $post
2013-08-06 10:41:20 +00:00
*/
public static function output ( $post ) {
wp_nonce_field ( 'woocommerce_save_data' , 'woocommerce_meta_nonce' );
2016-08-26 13:50:17 +00:00
$coupon = new WC_Coupon ( $post -> ID );
2013-08-06 10:41:20 +00:00
?>
< style type = " text/css " >
#edit-slug-box, #minor-publishing-actions { display:none }
</ style >
2013-10-18 17:10:55 +00:00
< div id = " coupon_options " class = " panel-wrap coupon_data " >
2013-08-06 10:41:20 +00:00
2013-10-18 17:10:55 +00:00
< div class = " wc-tabs-back " ></ div >
2013-08-06 10:41:20 +00:00
2013-10-18 17:10:55 +00:00
< ul class = " coupon_data_tabs wc-tabs " style = " display:none; " >
< ? php
$coupon_data_tabs = apply_filters ( 'woocommerce_coupon_data_tabs' , array (
'general' => array (
'label' => __ ( 'General' , 'woocommerce' ),
'target' => 'general_coupon_data' ,
2013-10-28 11:19:56 +00:00
'class' => 'general_coupon_data' ,
2013-10-18 17:10:55 +00:00
),
'usage_restriction' => array (
2016-10-12 10:16:30 +00:00
'label' => __ ( 'Usage restriction' , 'woocommerce' ),
2013-10-18 17:10:55 +00:00
'target' => 'usage_restriction_coupon_data' ,
'class' => '' ,
),
'usage_limit' => array (
2016-10-12 10:16:30 +00:00
'label' => __ ( 'Usage limits' , 'woocommerce' ),
2013-10-18 17:10:55 +00:00
'target' => 'usage_limit_coupon_data' ,
'class' => '' ,
2016-08-27 01:46:45 +00:00
),
2013-10-18 17:10:55 +00:00
) );
foreach ( $coupon_data_tabs as $key => $tab ) {
2013-10-21 20:13:52 +00:00
?> <li class="<?php echo $key; ?>_options <?php echo $key; ?>_tab <?php echo implode( ' ' , (array) $tab['class'] ); ?>">
2017-02-22 02:29:06 +00:00
< a href = " #<?php echo $tab['target'] ; ?> " >< span >< ? php echo esc_html ( $tab [ 'label' ] ); ?> </span></a>
2013-10-18 17:10:55 +00:00
</ li >< ? php
}
?>
</ ul >
< div id = " general_coupon_data " class = " panel woocommerce_options_panel " >< ? php
2013-08-06 10:41:20 +00:00
// Type
2017-02-25 08:27:38 +00:00
woocommerce_wp_select ( array (
2017-03-07 17:57:28 +00:00
'id' => 'discount_type' ,
'label' => __ ( 'Discount type' , 'woocommerce' ),
'options' => wc_get_coupon_types (),
2017-02-25 08:27:38 +00:00
) );
2013-08-06 10:41:20 +00:00
// Amount
2017-02-25 08:27:38 +00:00
woocommerce_wp_text_input ( array (
2017-03-07 17:57:28 +00:00
'id' => 'coupon_amount' ,
'label' => __ ( 'Coupon amount' , 'woocommerce' ),
2017-02-25 08:27:38 +00:00
'placeholder' => wc_format_localized_price ( 0 ),
'description' => __ ( 'Value of the coupon.' , 'woocommerce' ),
2017-03-07 17:57:28 +00:00
'data_type' => 'price' ,
'desc_tip' => true ,
2017-02-25 08:27:38 +00:00
) );
2013-08-06 10:41:20 +00:00
// Free Shipping
2017-02-25 08:16:30 +00:00
if ( wc_shipping_enabled () ) {
woocommerce_wp_checkbox ( array (
2017-03-07 17:57:28 +00:00
'id' => 'free_shipping' ,
'label' => __ ( 'Allow free shipping' , 'woocommerce' ),
'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/' ),
2017-02-25 08:16:30 +00:00
) );
}
2013-08-06 10:41:20 +00:00
2013-10-18 17:10:55 +00:00
// Expiry date
2017-03-14 17:27:48 +00:00
$expiry_date = $coupon -> get_date_expires () ? $coupon -> get_date_expires () -> date ( 'Y-m-d' ) : '' ;
2017-02-25 08:27:38 +00:00
woocommerce_wp_text_input ( array (
2017-03-07 17:57:28 +00:00
'id' => 'expiry_date' ,
'value' => esc_attr ( $expiry_date ),
'label' => __ ( 'Coupon expiry date' , 'woocommerce' ),
'placeholder' => 'YYYY-MM-DD' ,
'description' => '' ,
'class' => 'date-picker' ,
'custom_attributes' => array (
'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])' ),
),
2017-02-25 08:27:38 +00:00
) );
2013-08-06 10:41:20 +00:00
2017-02-17 11:58:15 +00:00
do_action ( 'woocommerce_coupon_options' , $coupon -> get_id (), $coupon );
2013-10-18 17:10:55 +00:00
?> </div>
< div id = " usage_restriction_coupon_data " class = " panel woocommerce_options_panel " >< ? php
echo '<div class="options_group">' ;
2013-08-06 10:41:20 +00:00
// minimum spend
2017-02-25 08:27:38 +00:00
woocommerce_wp_text_input ( array (
2017-03-07 17:57:28 +00:00
'id' => 'minimum_amount' ,
'label' => __ ( 'Minimum spend' , 'woocommerce' ),
2017-02-25 08:27:38 +00:00
'placeholder' => __ ( 'No minimum' , 'woocommerce' ),
2017-11-16 10:28:43 +00:00
'description' => __ ( 'This field allows you to set the minimum spend (subtotal) allowed to use the coupon.' , 'woocommerce' ),
2017-03-07 17:57:28 +00:00
'data_type' => 'price' ,
'desc_tip' => true ,
2017-02-25 08:27:38 +00:00
) );
2013-10-18 17:10:55 +00:00
2014-08-13 10:43:28 +00:00
// maximum spend
2017-02-25 08:27:38 +00:00
woocommerce_wp_text_input ( array (
2017-03-07 17:57:28 +00:00
'id' => 'maximum_amount' ,
'label' => __ ( 'Maximum spend' , 'woocommerce' ),
2017-02-25 08:27:38 +00:00
'placeholder' => __ ( 'No maximum' , 'woocommerce' ),
2017-11-16 10:28:43 +00:00
'description' => __ ( 'This field allows you to set the maximum spend (subtotal) allowed when using the coupon.' , 'woocommerce' ),
2017-03-07 17:57:28 +00:00
'data_type' => 'price' ,
'desc_tip' => true ,
2017-02-25 08:27:38 +00:00
) );
2014-08-08 11:38:58 +00:00
2013-10-18 17:10:55 +00:00
// Individual use
2017-02-25 08:27:38 +00:00
woocommerce_wp_checkbox ( array (
2017-03-07 17:57:28 +00:00
'id' => 'individual_use' ,
'label' => __ ( 'Individual use only' , 'woocommerce' ),
'description' => __ ( 'Check this box if the coupon cannot be used in conjunction with other coupons.' , 'woocommerce' ),
2017-02-25 08:27:38 +00:00
) );
2013-10-18 17:10:55 +00:00
// Exclude Sale Products
2017-02-25 08:27:38 +00:00
woocommerce_wp_checkbox ( array (
2017-03-07 17:57:28 +00:00
'id' => 'exclude_sale_items' ,
'label' => __ ( 'Exclude sale items' , 'woocommerce' ),
2017-06-19 10:56:04 +00:00
'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' ),
2017-02-25 08:27:38 +00:00
) );
2013-08-06 10:41:20 +00:00
echo '</div><div class="options_group">' ;
// Product ids
?>
2015-01-12 11:56:44 +00:00
< p class = " form-field " >< label >< ? php _e ( 'Products' , 'woocommerce' ); ?> </label>
2016-12-21 13:23:26 +00:00
< 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 " >
< ? php
$product_ids = $coupon -> get_product_ids ();
2013-08-06 10:41:20 +00:00
2016-12-21 13:23:26 +00:00
foreach ( $product_ids as $product_id ) {
$product = wc_get_product ( $product_id );
if ( is_object ( $product ) ) {
echo '<option value="' . esc_attr ( $product_id ) . '"' . selected ( true , true , false ) . '>' . wp_kses_post ( $product -> get_formatted_name () ) . '</option>' ;
}
}
?>
2017-07-04 11:07:26 +00:00
</ select > < ? 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' ) ); ?> </p>
2013-08-06 10:41:20 +00:00
< ? php
// Exclude Product ids
?>
2015-01-12 11:56:44 +00:00
< p class = " form-field " >< label >< ? php _e ( 'Exclude products' , 'woocommerce' ); ?> </label>
2016-12-21 13:23:26 +00:00
< 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 " >
< ? php
$product_ids = $coupon -> get_excluded_product_ids ();
2013-08-06 10:41:20 +00:00
2016-12-21 13:23:26 +00:00
foreach ( $product_ids as $product_id ) {
$product = wc_get_product ( $product_id );
if ( is_object ( $product ) ) {
echo '<option value="' . esc_attr ( $product_id ) . '"' . selected ( true , true , false ) . '>' . wp_kses_post ( $product -> get_formatted_name () ) . '</option>' ;
}
}
?>
2017-07-04 11:07:26 +00:00
</ select > < ? 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' ) ); ?> </p>
2013-08-06 10:41:20 +00:00
< ? php
echo '</div><div class="options_group">' ;
// Categories
?>
2014-12-19 16:15:24 +00:00
< p class = " form-field " >< label for = " product_categories " >< ? php _e ( 'Product categories' , 'woocommerce' ); ?> </label>
2015-08-05 18:08:15 +00:00
< 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' ); ?> " >
2013-08-06 10:41:20 +00:00
< ? php
2016-08-26 13:50:17 +00:00
$category_ids = $coupon -> get_product_categories ();
2015-01-12 11:56:44 +00:00
$categories = get_terms ( 'product_cat' , 'orderby=name&hide_empty=0' );
2014-08-31 07:18:21 +00:00
2017-03-07 20:24:24 +00:00
if ( $categories ) {
foreach ( $categories as $cat ) {
echo '<option value="' . esc_attr ( $cat -> term_id ) . '"' . selected ( in_array ( $cat -> term_id , $category_ids ), true , false ) . '>' . esc_html ( $cat -> name ) . '</option>' ;
}
2014-01-15 04:02:29 +00:00
}
2013-08-06 10:41:20 +00:00
?>
2017-07-04 11:07:26 +00:00
</ 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' ) ); ?> </p>
2013-08-06 10:41:20 +00:00
< ? php
// Exclude Categories
?>
2014-01-15 04:02:29 +00:00
< p class = " form-field " >< label for = " exclude_product_categories " >< ? php _e ( 'Exclude categories' , 'woocommerce' ); ?> </label>
2015-08-05 18:08:15 +00:00
< 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' ); ?> " >
2013-08-06 10:41:20 +00:00
< ? php
2016-08-26 13:50:17 +00:00
$category_ids = $coupon -> get_excluded_product_categories ();
2015-01-12 11:56:44 +00:00
$categories = get_terms ( 'product_cat' , 'orderby=name&hide_empty=0' );
2014-08-31 07:18:21 +00:00
2017-03-07 20:24:24 +00:00
if ( $categories ) {
foreach ( $categories as $cat ) {
echo '<option value="' . esc_attr ( $cat -> term_id ) . '"' . selected ( in_array ( $cat -> term_id , $category_ids ), true , false ) . '>' . esc_html ( $cat -> name ) . '</option>' ;
}
2014-01-15 04:02:29 +00:00
}
2013-08-06 10:41:20 +00:00
?>
2017-07-04 11:07:26 +00:00
</ select > < ? 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' ) ); ?> </p>
2013-08-06 10:41:20 +00:00
< ? php
echo '</div><div class="options_group">' ;
// Customers
2016-08-27 03:56:09 +00:00
woocommerce_wp_text_input ( array (
'id' => 'customer_email' ,
'label' => __ ( 'Email restrictions' , 'woocommerce' ),
'placeholder' => __ ( 'No restrictions' , 'woocommerce' ),
2017-04-26 12:51:53 +00:00
'description' => __ ( 'List of allowed emails to check against the customer billing email when an order is placed. Separate email addresses with commas.' , 'woocommerce' ),
2017-07-14 10:37:00 +00:00
'value' => implode ( ', ' , ( array ) $coupon -> get_email_restrictions () ),
2016-08-27 03:56:09 +00:00
'desc_tip' => true ,
'type' => 'email' ,
'class' => '' ,
'custom_attributes' => array (
2016-08-27 01:46:45 +00:00
'multiple' => 'multiple' ,
2016-08-27 03:56:09 +00:00
),
) );
2013-08-06 10:41:20 +00:00
2013-10-18 17:10:55 +00:00
echo '</div>' ;
2017-02-17 11:58:15 +00:00
do_action ( 'woocommerce_coupon_options_usage_restriction' , $coupon -> get_id (), $coupon );
2014-01-13 12:51:00 +00:00
2013-10-18 17:10:55 +00:00
?> </div>
< div id = " usage_limit_coupon_data " class = " panel woocommerce_options_panel " >< ? php
2013-08-06 10:41:20 +00:00
2013-10-18 17:10:55 +00:00
echo '<div class="options_group">' ;
// Usage limit per coupons
2016-08-27 03:56:09 +00:00
woocommerce_wp_text_input ( array (
'id' => 'usage_limit' ,
'label' => __ ( 'Usage limit per coupon' , 'woocommerce' ),
2016-10-24 07:31:07 +00:00
'placeholder' => esc_attr__ ( 'Unlimited usage' , 'woocommerce' ),
2016-08-27 03:56:09 +00:00
'description' => __ ( 'How many times this coupon can be used before it is void.' , 'woocommerce' ),
'type' => 'number' ,
'desc_tip' => true ,
'class' => 'short' ,
'custom_attributes' => array (
2016-09-01 10:43:31 +00:00
'step' => 1 ,
'min' => 0 ,
2016-08-27 03:56:09 +00:00
),
2016-09-01 10:43:31 +00:00
'value' => $coupon -> get_usage_limit () ? $coupon -> get_usage_limit () : '' ,
2016-08-27 03:56:09 +00:00
) );
2013-08-06 10:41:20 +00:00
2013-10-18 17:10:55 +00:00
// Usage limit per product
2016-08-27 03:56:09 +00:00
woocommerce_wp_text_input ( array (
'id' => 'limit_usage_to_x_items' ,
'label' => __ ( 'Limit usage to X items' , 'woocommerce' ),
2016-10-24 07:31:07 +00:00
'placeholder' => esc_attr__ ( 'Apply to all qualifying items in cart' , 'woocommerce' ),
2016-08-27 03:56:09 +00:00
'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' ),
'desc_tip' => true ,
'class' => 'short' ,
'type' => 'number' ,
'custom_attributes' => array (
2016-09-01 10:43:31 +00:00
'step' => 1 ,
'min' => 0 ,
2016-08-27 03:56:09 +00:00
),
2016-09-01 10:43:31 +00:00
'value' => $coupon -> get_limit_usage_to_x_items () ? $coupon -> get_limit_usage_to_x_items () : '' ,
2016-08-27 03:56:09 +00:00
) );
2013-10-01 10:48:27 +00:00
2013-10-18 17:10:55 +00:00
// Usage limit per users
2016-08-27 03:56:09 +00:00
woocommerce_wp_text_input ( array (
'id' => 'usage_limit_per_user' ,
'label' => __ ( 'Usage limit per user' , 'woocommerce' ),
2016-10-24 07:31:07 +00:00
'placeholder' => esc_attr__ ( 'Unlimited usage' , 'woocommerce' ),
2017-01-26 17:51:34 +00:00
'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' ),
2016-08-27 03:56:09 +00:00
'desc_tip' => true ,
'class' => 'short' ,
'type' => 'number' ,
'custom_attributes' => array (
2016-09-01 10:43:31 +00:00
'step' => 1 ,
'min' => 0 ,
2016-08-27 03:56:09 +00:00
),
2016-09-01 10:43:31 +00:00
'value' => $coupon -> get_usage_limit_per_user () ? $coupon -> get_usage_limit_per_user () : '' ,
2016-08-27 03:56:09 +00:00
) );
2013-08-06 10:41:20 +00:00
echo '</div>' ;
2017-02-17 11:58:15 +00:00
do_action ( 'woocommerce_coupon_options_usage_limit' , $coupon -> get_id (), $coupon );
2014-01-13 12:51:00 +00:00
2013-10-18 17:10:55 +00:00
?> </div>
2017-02-17 11:58:15 +00:00
< ? php do_action ( 'woocommerce_coupon_data_panels' , $coupon -> get_id (), $coupon ); ?>
2013-10-18 17:10:55 +00:00
< div class = " clear " ></ div >
2013-08-06 10:41:20 +00:00
</ div >
< ? php
}
/**
2015-11-03 12:28:01 +00:00
* Save meta box data .
2016-01-04 21:31:36 +00:00
*
* @ param int $post_id
* @ param WP_Post $post
2013-08-06 10:41:20 +00:00
*/
public static function save ( $post_id , $post ) {
// Check for dupe coupons
2017-02-22 17:40:24 +00:00
$coupon_code = wc_format_coupon_code ( $post -> post_title );
2016-08-30 10:43:53 +00:00
$id_from_code = wc_get_coupon_id_by_code ( $coupon_code , $post_id );
if ( $id_from_code ) {
2013-08-06 10:41:20 +00:00
WC_Admin_Meta_Boxes :: add_error ( __ ( 'Coupon code already exists - customers will use the latest coupon with this code.' , 'woocommerce' ) );
2014-01-15 04:02:29 +00:00
}
2013-08-06 10:41:20 +00:00
2016-11-24 17:43:01 +00:00
$product_categories = isset ( $_POST [ 'product_categories' ] ) ? ( array ) $_POST [ 'product_categories' ] : array ();
$exclude_product_categories = isset ( $_POST [ 'exclude_product_categories' ] ) ? ( array ) $_POST [ 'exclude_product_categories' ] : array ();
2016-08-26 13:50:17 +00:00
$coupon = new WC_Coupon ( $post_id );
$coupon -> set_props ( array (
'code' => $post -> post_title ,
'discount_type' => wc_clean ( $_POST [ 'discount_type' ] ),
'amount' => wc_format_decimal ( $_POST [ 'coupon_amount' ] ),
2016-08-26 14:20:44 +00:00
'date_expires' => wc_clean ( $_POST [ 'expiry_date' ] ),
2016-08-26 13:50:17 +00:00
'individual_use' => isset ( $_POST [ 'individual_use' ] ),
2017-01-03 14:16:58 +00:00
'product_ids' => isset ( $_POST [ 'product_ids' ] ) ? array_filter ( array_map ( 'intval' , ( array ) $_POST [ 'product_ids' ] ) ) : array (),
'excluded_product_ids' => isset ( $_POST [ 'exclude_product_ids' ] ) ? array_filter ( array_map ( 'intval' , ( array ) $_POST [ 'exclude_product_ids' ] ) ) : array (),
2016-08-26 13:50:17 +00:00
'usage_limit' => absint ( $_POST [ 'usage_limit' ] ),
'usage_limit_per_user' => absint ( $_POST [ 'usage_limit_per_user' ] ),
'limit_usage_to_x_items' => absint ( $_POST [ 'limit_usage_to_x_items' ] ),
'free_shipping' => isset ( $_POST [ 'free_shipping' ] ),
2016-11-24 17:43:01 +00:00
'product_categories' => array_filter ( array_map ( 'intval' , $product_categories ) ),
'excluded_product_categories' => array_filter ( array_map ( 'intval' , $exclude_product_categories ) ),
2016-08-26 13:50:17 +00:00
'exclude_sale_items' => isset ( $_POST [ 'exclude_sale_items' ] ),
'minimum_amount' => wc_format_decimal ( $_POST [ 'minimum_amount' ] ),
'maximum_amount' => wc_format_decimal ( $_POST [ 'maximum_amount' ] ),
'email_restrictions' => array_filter ( array_map ( 'trim' , explode ( ',' , wc_clean ( $_POST [ 'customer_email' ] ) ) ) ),
) );
$coupon -> save ();
2017-02-17 11:58:15 +00:00
do_action ( 'woocommerce_coupon_options_save' , $post_id , $coupon );
2013-08-06 10:41:20 +00:00
}
2014-01-15 03:57:41 +00:00
}