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