Commit 7e1024ba by 杨子

修改商城

parent 3d14e87c
.wc-coupon-generator-wrap {
/** Steps */
/* Step specific */ }
.wc-coupon-generator-wrap.wc-coupon-generator-wrap .steps .step {
background: #8b8e94;
padding: 10px 20px;
color: white;
width: calc( ( 100% - ( 68px * 4 ) ) / 4 );
display: inline-block;
margin-right: 20px;
margin-left: 0px;
font-weight: bold;
font-size: 14px;
white-space: nowrap; }
.wc-coupon-generator-wrap.wc-coupon-generator-wrap .steps .step a {
color: white;
text-decoration: none; }
.wc-coupon-generator-wrap.wc-coupon-generator-wrap .steps .step a:focus {
box-shadow: none; }
.wc-coupon-generator-wrap.wc-coupon-generator-wrap .steps .step.active {
background: #e33a3d;
border-left-color: #e33a3d; }
.wc-coupon-generator-wrap.wc-coupon-generator-wrap .steps .step:after {
content: "";
width: 0px;
height: 0px;
border-top: 20px solid transparent;
border-bottom: 20px solid transparent;
border-left: 20px solid #8b8e94;
float: right;
position: relative;
margin: -10px -40px; }
.wc-coupon-generator-wrap.wc-coupon-generator-wrap .steps .step:before {
content: "";
width: 0px;
height: 0px;
border-top: 20px solid #8b8e94;
border-bottom: 20px solid #8b8e94;
border-left: 20px solid transparent;
float: left;
position: relative;
margin: -10px -40px; }
.wc-coupon-generator-wrap.wc-coupon-generator-wrap .steps .step.active:after {
border-left-color: #e33a3d; }
.wc-coupon-generator-wrap.wc-coupon-generator-wrap .steps .step.active:before {
border-top-color: #e33a3d;
border-bottom-color: #e33a3d; }
.wc-coupon-generator-wrap.wc-coupon-generator-wrap .steps .step.step-0 {
margin-left: 20px; }
.wc-coupon-generator-wrap.wc-coupon-generator-wrap .continue-button-wrap {
float: right; }
.wc-coupon-generator-wrap.wc-coupon-generator-wrap .continue-button-wrap:focus {
box-shadow: none; }
.wc-coupon-generator-wrap.wc-coupon-generator-wrap .continue-button-wrap:hover .continue-button {
background: #C12123; }
.wc-coupon-generator-wrap.wc-coupon-generator-wrap .continue-button-wrap:hover .continue-button:before {
border-top-color: #C12123;
border-bottom-color: #C12123; }
.wc-coupon-generator-wrap.wc-coupon-generator-wrap .continue-button-wrap:hover .continue-button:after {
border-right-color: #C12123;
border-left-color: #C12123; }
.wc-coupon-generator-wrap.wc-coupon-generator-wrap .continue-button-wrap .continue-button {
background: #E33A3D;
border: none;
color: #FFF;
padding: 10px 30px;
height: 18px;
line-height: 18px;
position: relative;
display: inline-block;
margin-left: 20px;
margin-right: 20px;
font-weight: bold;
font-size: 14px; }
.wc-coupon-generator-wrap.wc-coupon-generator-wrap .continue-button-wrap .continue-button:after {
content: "";
width: 0px;
height: 0px;
border-top: 19px solid transparent;
border-bottom: 19px solid transparent;
border-left: 19px solid #E33A3D;
position: relative;
float: right;
margin: -10px -49px;
padding: 0px;
line-height: 0; }
.wc-coupon-generator-wrap.wc-coupon-generator-wrap .continue-button-wrap .continue-button:before {
content: "";
width: 0px;
height: 0px;
border-top: 19px solid #E33A3D;
border-bottom: 19px solid #E33A3D;
border-left: 19px solid transparent;
float: left;
position: relative;
margin: 0;
margin: -10px -49px; }
.wc-coupon-generator-wrap.wc-coupon-generator-wrap-step-2 #wc-coupon-generator-options {
float: left;
width: 80%; }
.wc-coupon-generator-wrap.wc-coupon-generator-wrap-step-2 #wc-coupon-generator-options .inside {
padding: 0;
margin: 0; }
.wc-coupon-generator-wrap.wc-coupon-generator-wrap-step-3 {
/** Progress bar */ }
.wc-coupon-generator-wrap.wc-coupon-generator-wrap-step-3 .wc-coupon-generator-progress-bar {
width: calc( 100% - 50px );
height: 30px;
border: 1px solid #DDD;
position: relative;
display: inline-block; }
.wc-coupon-generator-wrap.wc-coupon-generator-wrap-step-3 .wc-coupon-generator-progress-bar .wc-coupon-generator-progress-percentage {
text-align: center;
line-height: 30px;
z-index: 10;
position: relative;
font-weight: bold;
position: absolute;
width: 100%; }
.wc-coupon-generator-wrap.wc-coupon-generator-wrap-step-3 .wc-coupon-generator-progress-bar .progress {
width: 0%;
height: 30px;
display: inline-block;
background-color: #E33A3D;
position: absolute;
z-index: 15;
overflow: hidden;
-webkit-transition: width 1s ease-in-out;
-moz-transition: width 1s ease-in-out;
-o-transition: width 1s ease-in-out;
transition: width 1s ease-in-out; }
.wc-coupon-generator-wrap.wc-coupon-generator-wrap-step-3 .wc-coupon-generator-progress-bar .progress .inner-progress {
position: absolute;
color: #FFF;
z-index: 15;
text-align: center;
display: block;
height: 30px;
line-height: 31px;
-webkit-transition: none;
-moz-transition: none;
-o-transition: none;
transition: none; }
.wc-coupon-generator-wrap.wc-coupon-generator-wrap-step-3 .spinner {
float: right;
margin: 5px 10px; }
.wc-coupon-generator-wrap.wc-coupon-generator-wrap-step-3 pre.wc-coupon-generator-progress-messages {
background: #F5F5F5;
padding: 12px;
max-height: 300px;
overflow: scroll; }
.wc-coupon-generator-wrap {
/** Steps */
&.wc-coupon-generator-wrap {
.steps {
.step {
background: #8b8e94;
padding: 10px 20px;
color: white;
width: calc( ( 100% - ( 68px * 4 ) ) / 4 );
display:inline-block;
margin-right: 20px;
margin-left: 0px;
font-weight: bold;
font-size: 14px;
white-space: nowrap;
a {
color: white;
text-decoration: none;
&:focus { box-shadow: none; }
}
&.active {
background: #e33a3d;
border-left-color: #e33a3d;
}
// Step arrows (left and right)
&:after {
content: "";
width: 0px;
height: 0px;
border-top: 20px solid rgba(0, 0, 0, 0);
border-bottom: 20px solid rgba(0, 0, 0, 0);
border-left: 20px solid #8b8e94;
float: right;
position: relative;
margin: -10px -40px;
}
&:before {
content: "";
width: 0px;
height: 0px;
border-top: 20px solid #8b8e94;
border-bottom: 20px solid #8b8e94;
border-left: 20px solid rgba(0, 0, 0, 0);
float: left;
position: relative;
margin: -10px -40px;
}
&.active:after {
border-left-color: #e33a3d;
}
&.active:before {
border-top-color: #e33a3d;
border-bottom-color: #e33a3d;
}
}
.step.step-0 {
margin-left: 20px;
}
}
.continue-button-wrap {
float: right;
&:focus { box-shadow: none; }
&:hover .continue-button { background: #C12123; }
&:hover .continue-button:before { border-top-color: #C12123; border-bottom-color: #C12123; }
&:hover .continue-button:after { border-right-color: #C12123; border-left-color: #C12123; }
.continue-button {
background: #E33A3D; border: none; color: #FFF; padding: 10px 30px;
height: 18px;
line-height: 18px;
position: relative;
display: inline-block;
margin-left: 20px;
margin-right: 20px;
font-weight: bold;
font-size: 14px;
&:after {
content: "";
width: 0px;
height: 0px;
border-top: 19px solid rgba(0, 0, 0, 0);
border-bottom: 19px solid rgba(0, 0, 0, 0);
border-left: 19px solid #E33A3D;
position: relative;
float: right;
margin: -10px -49px;
padding: 0px;
line-height: 0;
}
&:before {
content: "";
width: 0px;
height: 0px;
border-top: 19px solid #E33A3D;
border-bottom: 19px solid #E33A3D;
border-left: 19px solid rgba(0, 0, 0, 0);
float: left;
position: relative;
margin: 0;
margin: -10px -49px;
}
}
}
}
/* Step specific */
&.wc-coupon-generator-wrap-step-2 {
#wc-coupon-generator-options {
float: left;
width: 80%;
.inside {
padding: 0;
margin: 0;
}
}
}
&.wc-coupon-generator-wrap-step-3 {
/** Progress bar */
.wc-coupon-generator-progress-bar {
width: calc( 100% - 50px );
height: 30px;
border: 1px solid #DDD;
position: relative;
display: inline-block;
.wc-coupon-generator-progress-percentage {
text-align: center;
line-height: 30px;
z-index: 10;
position: relative;
font-weight: bold;
position: absolute;
width: 100%;
}
.progress {
width: 0%;
height: 30px;
display: inline-block;
background-color: #E33A3D;
position: absolute;
z-index: 15;
overflow: hidden;
-webkit-transition: width 1s ease-in-out;
-moz-transition: width 1s ease-in-out;
-o-transition: width 1s ease-in-out;
transition: width 1s ease-in-out;
.inner-progress {
position: absolute;
color: #FFF;
z-index: 15;
text-align: center;
display: block;
height: 30px;
line-height: 31px;
-webkit-transition: none;
-moz-transition: none;
-o-transition: none;
transition: none;
}
}
}
.spinner { float: right; margin: 5px 10px; }
pre.wc-coupon-generator-progress-messages {
background: #F5F5F5;
padding: 12px;
max-height: 300px;
overflow: scroll;
}
}
}
var WCCG_Generator={init:function(){this.generate_coupons(0,this)},generate_coupons:function(e,r){var o={action:"wccg_generate_coupons",form_data:jQuery("#wc-coupon-generator-form").serialize(),batch_step:e};jQuery.post(ajaxurl,o,function(e){e=JSON.parse(e),e.message&&r.add_message(e.message),"done"==e.step?r.completed(r,e.total_coupons_generated,e.total_coupons_generated):r.generate_coupons(e.step,r),r.progress_bar(e.progress)})},progress_bar:function(e){jQuery(".wc-coupon-generator-progress-bar .progress").css("width",e+"%"),jQuery(".wc-coupon-generator-progress-percentage").html(e+"%"),jQuery(".inner-progress").css("width",jQuery(".wc-coupon-generator-progress-bar").width())},completed:function(e,r,o){jQuery(".wc-coupon-generator-progress-bar + .spinner").remove()},add_message:function(e){jQuery(".wc-coupon-generator-progress-messages").prepend('<span class="wc-coupon-generator-progress-message">'+e+"</span><br/>")}};
\ No newline at end of file
var WCCG_Generator = {
init: function() {
this.generate_coupons( 0, this );
},
generate_coupons: function( batch_step, self ) {
var data = {
'action': 'wccg_generate_coupons',
'form_data': jQuery( '#wc-coupon-generator-form' ).serialize(),
'batch_step': batch_step
};
jQuery.post ( ajaxurl, data, function( response ) {
response = JSON.parse( response );
if ( response.message ) {
self.add_message( response.message );
}
if ( 'done' == response.step ) {
self.completed( self, response.total_coupons_generated, response.total_coupons_generated );
} else {
self.generate_coupons( response.step, self );
}
self.progress_bar( response.progress );
});
},
progress_bar: function( progress ) {
jQuery( '.wc-coupon-generator-progress-bar .progress' ).css( 'width', progress + '%' );
jQuery( '.wc-coupon-generator-progress-percentage' ).html( progress + '%' );
jQuery( '.inner-progress' ).css( 'width', jQuery( '.wc-coupon-generator-progress-bar' ).width() );
},
completed: function( self, coupons_genrated, execution_time ) {
jQuery( '.wc-coupon-generator-progress-bar + .spinner' ).remove();
},
add_message: function( message ) {
jQuery( '.wc-coupon-generator-progress-messages' ).prepend( '<span class="wc-coupon-generator-progress-message">' + message + '</span><br/>' );
}
};
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
/**
* Admin class.
*
* Handle all general admin business.
*
* @class WCCG_Admin
* @author Jeroen Sormani
* @package WooCommerce Coupon Generator
* @version 1.0.0
*/
class WCCG_Admin {
/**
* Constructor.
*
* @since 1.0.0
*/
public function __construct() {
if ( is_admin() ) :
// Initialize
add_action( 'init', array( $this, 'init' ) ); // Used init because admin_init is too late for admin_menu
endif;
}
/**
* Admin init.
*
* Initialize admin components.
*
* @since 1.0.0
*/
public function init() {
global $pagenow;
// Add generator page
add_action( 'admin_menu', array( $this, 'add_generator_page' ) );
// Enqueue scripts
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ), 20 );
// WooCommerce screen IDs
add_action( 'woocommerce_screen_ids', array( $this, 'add_wc_screen_id' ) );
if ( 'plugins.php' == $pagenow ) :
// Plugins page
add_filter( 'plugin_action_links_' . plugin_basename( WooCommerce_Coupon_Generator()->file ), array( $this, 'add_plugin_action_links' ), 10, 2 );
add_filter( 'plugin_row_meta', array( $this, 'add_plugin_row_meta' ), 10, 2 );
endif;
}
/**
* Enqueue scripts.
*
* Enqueue script as javascript and style sheets.
*
* @since 1.0.0
*/
public function enqueue_scripts( $hook ) {
$current_screen = get_current_screen();
if ( 'woocommerce_page_woocommerce_coupon_generator' == $current_screen->id ) :
$min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
wp_enqueue_style( 'coupon-generator-for-woocommerce', plugins_url( 'assets/css/woocommerce-coupon-generator-admin.css', WooCommerce_Coupon_Generator()->file ), array(), WooCommerce_Coupon_Generator()->version );
wp_enqueue_script( 'coupon-generator-for-woocommerce', plugins_url( 'assets/js/woocommerce-coupon-generator-admin.js', WooCommerce_Coupon_Generator()->file ), array( 'jquery' ), WooCommerce_Coupon_Generator()->version, true );
wp_enqueue_script( 'wc-admin-meta-boxes' );
wp_enqueue_script( 'wc-admin-coupon-meta-boxes' );
endif;
}
/**
* Add generator page.
*
* Add the generator page to the WordPress admin.
*
* @since 1.0.0
*/
public function add_generator_page() {
add_submenu_page( 'woocommerce', __( 'WooCommerce Coupon Generator', 'coupon-generator-for-woocommerce' ), __( 'Coupon generator', 'coupon-generator-for-woocommerce' ), 'manage_woocommerce', 'woocommerce_coupon_generator', array( $this, 'coupon_generator_callback' ) );
}
/**
* Generator callback.
*
* Initialize and output the contents of the generator
* page in the admin backend.
*
* @since 1.0.0
*/
public function coupon_generator_callback() {
require_once plugin_dir_path( WooCommerce_Coupon_Generator()->file ) . 'includes/admin/class-wccg-generator.php';
$coupon_generator = new WCCG_Generator();
$coupon_generator->output_step();
}
/**
* WooCommerce screen IDs.
*
* Add the coupon generator coupon page to the WC Screen IDs.
*
* @since 1.0.0
*
* @param array $screen_ids List of existing screen IDs.
* @return array List of modfied screen IDs.
*/
public function add_wc_screen_id( $screen_ids ) {
$screen_ids[] = 'woocommerce_page_woocommerce_coupon_generator';
return $screen_ids;
}
/**
* Plugin action links.
*
* Add links to the plugins.php page below the plugin name
* and besides the 'activate', 'edit', 'delete' action links.
*
* @since 1.0.2
*
* @param array $links List of existing links.
* @param string $file Name of the current plugin being looped.
* @return array List of modified links.
*/
public function add_plugin_action_links( $links, $file ) {
if ( $file == plugin_basename( WooCommerce_Coupon_Generator()->file ) ) :
$links = array_merge( array(
'<a href="' . esc_url( admin_url( '/admin.php?page=woocommerce_coupon_generator' ) ) . '">' . __( 'Start generating coupons', 'coupon-generator-for-woocommerce' ) . '</a>'
), $links );
endif;
return $links;
}
/**
* Plugin row meta.
*
* Add extra plugin row meta, these are links / meta below the plugin description.
*
* @since 1.0.2
*
* @param array $links List of existing links.
* @param string $file Name of the current plugin being looped.
* @return array List of modified links.
*/
public function add_plugin_row_meta( $links, $file ) {
if ( $file == plugin_basename( WooCommerce_Coupon_Generator()->file ) ) :
$links[] = '<a href="https://shopplugins.com/plugins/category/woocommerce/" target="_blank">' . __( 'More WooCommerce plugins by Shop Plugins', 'coupon-generator-for-woocommerce' ) . '</a>';
endif;
return $links;
}
}
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
/**
* Class WCCG_Generator.
*
* Generator class containing the generation steps.
*
* @class WCCG_Generator
* @author Jeroen Sormani
* @package WooCommerce Coupon Generator
* @version 1.0.0
*/
class WCCG_Generator {
/**
* Steps.
*
* @since 1.0.0
* @var array $steps List of steps in the generator.
*/
public $steps = array();
/**
* Constructor.
*
* @since 1.0.0
*/
public function __construct() {
$this->steps = array(
'0' => array(
'name' => __( 'Introduction', 'coupon-generator-for-woocommerce' ),
'handler' => 'introduction_handler',
),
'1' => array(
'name' => __( 'Coupon options', 'coupon-generator-for-woocommerce' ),
'handler' => 'coupon_options_handler',
),
'2' => array(
'name' => __( 'Generator options', 'coupon-generator-for-woocommerce' ),
'handler' => 'generator_options_handler',
),
'3' => array(
'name' => __( 'Generate coupons', 'coupon-generator-for-woocommerce' ),
'handler' => 'generate_coupons_handler',
),
);
}
/**
* Current step.
*
* Get the current step number.
*
* @since 1.0.0
*
* @return int Step number.
*/
public function current_step() {
$step = 0;
if ( isset( $_REQUEST['step'] ) ) :
$step = absint( $_REQUEST['step'] );
endif;
return $step;
}
/**
* Output step.
*
* Output a step of the generator. Uses the step
* handler as a callback.
*
* @since 1.0.0
*
* @param int $step Step to output. Leave empty to use the current step.
*/
public function output_step( $step = null ) {
if ( null == $step ) :
$step = $this->current_step();
endif;
// Fallback to first step
if ( ! isset( $this->steps[ $step ] ) ) :
$step = 0;
endif;
if ( ! is_array( $this->steps ) || ! isset( $this->steps[ $step ]['handler'] ) ) :
wp_die( __( 'I\'m trying to load a step but couldn\'t find it! Please go back and try again.', 'coupon-generator-for-woocommerce' ), __( 'Could not find step', 'coupon-generator-for-woocommerce' ) );
endif;
if ( ! is_callable( array( $this, $this->steps[ $step ]['handler'] ) ) && ! is_callable( $this->steps[ $step ]['handler'] ) ) :
wp_die( __( 'I\'m trying to load a generator step but couldn\'t find the right callback! Please go back and try again.', 'coupon-generator-for-woocommerce' ), __( 'Could not find step', 'coupon-generator-for-woocommerce' ) );
endif;
$handler = $this->steps[ $step ]['handler'];
if ( is_callable( array( $this, $handler ) ) ) :
call_user_func( array( $this, $handler ) );
endif;
}
/**
* Introduction handler.
*
* Handler to output the introduction page (step 0).
*
* @since 1.0.0
*/
public function introduction_handler() {
require_once 'views/html-coupon-generator-step-0.php';
}
/**
* Coupon options handler.
*
* Handler to output the coupon options page (step 1).
*
* @since 1.0.0
*/
public function coupon_options_handler() {
require_once 'views/html-coupon-generator-step-1.php';
}
/**
* Generator options handler.
*
* Handler to output the coupon generator options (step 2).
*
* @since 1.0.0
*/
public function generator_options_handler() {
// Make sure values from the previous step are present
if ( ! isset( $_POST['coupon_amount'] ) ) :
return $this->output_step( 1 );
endif;
require_once 'views/html-coupon-generator-step-2.php';
}
/**
* Generate coupon handler.
*
* Handler to output the coupon generator page (step 3).
*
* @since 1.0.0
*/
public function generate_coupons_handler() {
// Make sure values from the previous step are present
if ( ! isset( $_POST['coupon_amount'] ) || ! isset( $_POST['number_of_coupons'] ) ) :
return $this->output_step( 2 );
endif;
require_once 'views/html-coupon-generator-step-3.php';
}
}
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
/**
* Coupon generator step 0.
*
* This is the initial step of the coupon generation process. It basically is a
* explanatory page.
*
* @author Jeroen Sormani
* @package WooCommerce Coupon Generator
* @version 1.0.0
*/
?><div class='wrap'>
<div class='wc-coupon-generator-wrap wc-coupon-generator-wrap-step-0'>
<h2><?php _e( 'WooCommerce Coupon Generator', 'coupon-generator-for-woocommerce' ); ?></h2>
<div class='steps'>
<span class='step step-0 active'><a href='<?php echo esc_url( remove_query_arg( 'step' ) ); ?>'><?php _e( '0. Introduction', 'coupon-generator-for-woocommerce' ); ?></a></span>
<span class='step step-1'><a href='<?php echo esc_url( add_query_arg( 'step', 1 ) ); ?>'><?php _e( '1. Coupon options', 'coupon-generator-for-woocommerce' ); ?></a></span>
<span class='step step-2'><?php _e( '2. Generator options', 'coupon-generator-for-woocommerce' ); ?></span>
<span class='step step-3'><?php _e( '3. Generating coupons', 'coupon-generator-for-woocommerce' ); ?></span>
</div>
<p><?php _e( 'Hi!', 'coupon-generator-for-woocommerce' ); ?></p>
<p><?php _e( 'Thank you for using WooCommerce Coupon Generator. To use the coupon generator you have to go through the following steps.', 'coupon-generator-for-woocommerce' ); ?>
<ul>
<li>
<strong><?php _e( '0. Introduction', 'coupon-generator-for-woocommerce' ); ?></strong>&nbsp;
<span class='description'><?php _e( 'You are here now', 'coupon-generator-for-woocommerce' ); ?></span>
</li>
<li><?php _e( '1. Coupon settings', 'coupon-generator-for-woocommerce' ); ?>&nbsp;
<span class='description'><?php _e( 'Here you can set the coupon settings you\'re used to set in the default WooCommerce coupon settings.', 'coupon-generator-for-woocommerce' ); ?></span>
</li>
<li><?php _e( '2. Generator settings', 'coupon-generator-for-woocommerce' ); ?>&nbsp;
<span class='description'><?php _e( 'Set options like the amount of coupons you want to generate.', 'coupon-generator-for-woocommerce' ); ?></span>
</li>
<li><?php _e( '3. Generating coupons', 'coupon-generator-for-woocommerce' ); ?>&nbsp;
<span class='description'><?php _e( 'This is the step where the coupons are actually generated.', 'coupon-generator-for-woocommerce' ); ?></span>
</li>
</ul>
</p>
<div class='clear'></div>
<a href="<?php echo esc_url( add_query_arg( 'step', 1 ) ); ?>" class="continue-button-wrap">
<span class="continue-button"><?php _e( 'Continue to the next step', 'coupon-generator-for-woocommerce' ); ?></span>
</a>
</div>
</div>
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
/**
* Coupon generator step 1.
*
* In this step the options of the coupon are shown as known from the regular
* add coupon screen.
*
* @author Jeroen Sormani
* @package WooCommerce Coupon Generator
* @version 1.0.0
*/
?><div class='wrap'>
<div class='wc-coupon-generator-wrap wc-coupon-generator-wrap-step-1'>
<h2><?php _e( 'WooCommerce Coupon Generator', 'coupon-generator-for-woocommerce' ); ?></h2>
<div class='steps'>
<span class='step step-0'><a href='<?php echo esc_url( remove_query_arg( 'step' ) ); ?>'><?php _e( '0. Introduction', 'coupon-generator-for-woocommerce' ); ?></a></span>
<span class='step step-1 active'><?php _e( '1. Coupon options', 'coupon-generator-for-woocommerce' ); ?></span>
<span class='step step-2'><?php _e( '2. Generator options', 'coupon-generator-for-woocommerce' ); ?></span>
<span class='step step-3'><?php _e( '3. Generating coupons', 'coupon-generator-for-woocommerce' ); ?></span>
</div>
<form method='post' id='wc-coupon-generator' action='<?php echo esc_url( add_query_arg( 'step', 2 ) ); ?>'>
<div class='wc-coupon-generator-coupon-data' id='poststuff'>
<div id="postbox-container-2" class="postbox-container">
<div id="normal-sortables" class="meta-box-sortables ui-sortable">
<div id="woocommerce-coupon-data" class="postbox ">
<h3 class="hndle ui-sortable-handle"><span><?php _e( 'Coupon Data', 'woocommerce' ); ?></span></h3>
<div class="inside"><?php
$temp_coupon = wp_insert_post( array(
'post_type' => 'shop_coupon',
'post_status' => 'draft',
'post_title' => 'temp_generator_coupon',
) );
global $thepostid;
$thepostid = $temp_coupon;
WC_Meta_Box_Coupon_Data::output( (object) array( 'ID' => null ) );
wp_delete_post( $temp_coupon, true );
?></div>
</div>
</div>
</div>
<div class='clear'></div>
<a href="javascript:void(0);" class="continue-button-wrap" onclick="document.getElementById('wc-coupon-generator').submit();">
<span class="continue-button"><?php _e( 'Continue to the next step', 'coupon-generator-for-woocommerce' ); ?></span>
</a>
</div>
</form>
</div>
</div>
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
/**
* Coupon generator step 2
*
* In this step the generation options of the coupon are shown.
*
* @author Jeroen Sormani
* @package WooCommerce Coupon Generator
* @version 1.0.0
*/
?><div class='wrap'>
<div class='wc-coupon-generator-wrap wc-coupon-generator-wrap-step-2'>
<h2><?php _e( 'WooCommerce Coupon Generator', 'coupon-generator-for-woocommerce' ); ?></h2>
<div class='steps'>
<span class='step step-0'><a href='<?php echo esc_url( remove_query_arg( 'step' ) ); ?>'><?php _e( '0. Introduction', 'coupon-generator-for-woocommerce' ); ?></a></span>
<span class='step step-1'><a href='<?php echo esc_url( add_query_arg( 'step', 1 ) ); ?>'><?php _e( '1. Coupon options', 'coupon-generator-for-woocommerce' ); ?></a></span>
<span class='step step-2 active'><?php _e( '2. Generator options', 'coupon-generator-for-woocommerce' ); ?></span>
<span class='step step-3'><?php _e( '3. Generating coupons', 'coupon-generator-for-woocommerce' ); ?></span>
</div>
<form method='post' id='woocommerce-coupon-generator' action='<?php echo esc_url( add_query_arg( 'step', 3 ) ); ?>'><?php
wp_nonce_field( 'wccg_generate_coupons', 'generate_coupons_nonce' );
?><div class='wc-coupon-generator-coupon-data' id='poststuff'>
<div id="postbox-container-0" class="postbox-container">
<div id="normal-sortables" class="meta-box-sortables ui-sortable">
<div id="woocommerce-coupon-generator-options" class="postbox ">
<h3 class="hndle ui-sortable-handle"><span><?php _e( 'Generator options', 'coupon-generator-for-woocommerce' ); ?></span></h3>
<div class="inside">
<div id="coupon_options" class="panel-wrap">
<div class="panel woocommerce_options_panel" style="display: block;">
<p class="form-field number_of_coupons ">
<label for="number_of_coupons"><?php _e( 'Number of coupons', 'coupon-generator-for-woocommerce' ); ?></label>
<input type="number" min="1" autofocus class="short" style="width: 200px;" name="number_of_coupons" id="coupon_amount" value="1" placeholder="10">
<img class="help_tip" data-tip='<?php _e( 'Number of coupons to generate in this batch', 'coupon-generator-for-woocommerce' ); ?>' src="<?php echo WC()->plugin_url(); ?>/assets/images/help.png" height="16" width="16" />
</p>
</div><!-- .woocommerce_options_panel -->
<div class="clear"></div>
</div><!-- #coupon_options -->
</div><!-- .inside -->
</div>
</div>
</div>
</div>
<div class='clear'></div>
<a href="javascript:void(0);" class="continue-button-wrap" onclick="document.getElementById('woocommerce-coupon-generator').submit();">
<span class="continue-button"><?php _e( 'Continue and generate coupons', 'coupon-generator-for-woocommerce' ); ?></span>
</a><?php
// Keep existing post values
foreach ( $_POST as $key => $val ) :
if ( is_array( $val ) ) :
foreach ( $val as $inner_val ) :
?><input type="hidden" name="<?php echo esc_attr( $key ); ?>[]" value="<?php echo esc_attr( $inner_val ); ?>" /><?php
endforeach;
else :
?><input type="hidden" name="<?php echo esc_attr( $key ); ?>" value="<?php echo esc_attr( $val ); ?>" /><?php
endif;
endforeach;
?></div>
</form>
</div>
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
/**
* Coupon generator step 2
*
* In this step the generation options of the coupon are shown.
*
* @author Jeroen Sormani
* @package WooCommerce Coupon Generator
* @version 1.0.0
*/
?><div class='wrap'>
<div class='wc-coupon-generator-wrap wc-coupon-generator-wrap-step-3'>
<h2><?php _e( 'WooCommerce Coupon Generator', 'coupon-generator-for-woocommerce' ); ?></h2>
<div class='steps'>
<span class='step step-0'><a href='<?php echo esc_url( remove_query_arg( 'step' ) ); ?>'><?php _e( '0. Introduction', 'coupon-generator-for-woocommerce' ); ?></a></span>
<span class='step step-1'><a href='<?php echo esc_url( add_query_arg( 'step', 1 ) ); ?>'><?php _e( '1. Coupon options', 'coupon-generator-for-woocommerce' ); ?></a></span>
<span class='step step-2'><?php _e( '2. Generator options', 'coupon-generator-for-woocommerce' ); ?></span>
<span class='step step-3 active'><?php _e( '3. Generating coupons', 'coupon-generator-for-woocommerce' ); ?></span>
</div>
<div class='wc-coupon-generator-coupon-data' id='poststuff'>
<div id="postbox-container-2" class="postbox-container">
<div id="normal-sortables" class="meta-box-sortables ui-sortable">
<div id="wc-coupon-generator-options" class="postbox ">
<h3 class="hndle ui-sortable-handle"><span><?php _e( 'Generating coupons', 'coupon-generator-for-woocommerce' ); ?></span></h3>
<div class="inside">
<div id="coupon_options" class="panel-wrap">
<div class="panel woocommerce_options_panel" style="display: block;">
<div class='wc-coupon-generator-progress-bar-wrap'>
<div class="wc-coupon-generator-progress-bar">
<div class="wc-coupon-generator-progress-percentage">0%</div>
<span class="progress">
<span class='inner-progress wc-coupon-generator-progress-percentage'>0%</span>
</span>
</div>
<span class="spinner is-active"></span>
</div>
<!-- Messages -->
<div class='wc-coupon-generator-progress-messages-wrap'>
<pre class='wc-coupon-generator-progress-messages'></pre>
</div>
<script>
jQuery( document ).ready( function( $ ) {
WCCG_Generator.init();
} );
</script>
<form id='wc-coupon-generator-form'><?php
// Keep existing post values
foreach ( $_POST as $key => $val ) :
if ( is_array( $val ) ) :
foreach ( $val as $inner_val ) :
?><input type="hidden" name="<?php echo esc_attr( $key ); ?>[]" value="<?php echo esc_attr( $inner_val ); ?>" /><?php
endforeach;
else :
?><input type="hidden" name="<?php echo esc_attr( $key ); ?>" value="<?php echo esc_attr( $val ); ?>" /><?php
endif;
endforeach;
?></form>
</div><!-- .woocommerce_options_panel -->
<div class="clear"></div>
</div><!-- #coupon_options -->
</div>
</div>
</div>
</div>
</div>
<div class='clear'></div>
</div>
</div>
<?php
/**
* WC Coupong Generator core functions.
*
* @author Jeroen Sormani
* @since 1.0.0
*/
if ( ! defined( 'ABSPATH' ) ) exit;
/**
* Generate coupons.
*
* Generate the coupons based on the $args arguments.
*
* @since 1.0.0
* TODO
*/
function wccg_generate_coupons( $number, $args = array() ) {
// Verify required values
if ( ! isset( $args['number_of_coupons'] ) ) :
return;
endif;
// TODO default args
global $wpdb;
$insert_coupon_ids = array();
$wpdb->query( 'START TRANSACTION' );
// Query coupons
$number_of_coupons = absint( $number );
for ( $i = 0; $i < $number_of_coupons; $i++ ) :
$coupon_code = wccg_get_random_coupon();
// Insert coupon post
$wpdb->query( $wpdb->prepare( "INSERT INTO $wpdb->posts SET
post_author=%d,
post_date=%s,
post_date_gmt=%s,
post_title=%s,
post_status='publish',
comment_status='closed',
ping_status='closed',
post_name=%s,
post_modified=%s,
post_modified_gmt=%s,
post_type='shop_coupon'
",
get_current_user_id(),
current_time( 'mysql' ),
current_time( 'mysql', 1 ),
sanitize_title( $coupon_code ),
$coupon_code,
current_time( 'mysql' ),
current_time( 'mysql', 1 )
) );
$insert_coupon_ids[] = $wpdb->insert_id;
$coupon_id = $wpdb->insert_id;
// Set GUID
// $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET guid=%s WHERE ID=%d", get_permalink( $coupon_id ), $coupon_id ) ); // Slow
$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET guid=%s WHERE ID=%d", esc_url_raw( add_query_arg( array( 'post_type' => 'shop_coupon', 'p' => $coupon_id ), home_url() ) ), $coupon_id ) ); // 10% faster -1 query per coupon
endfor;
// Add/Replace data to array
$product_ids = isset( $args['product_ids'] ) ? (array) $args['product_ids'] : array();
$exclude_ids = isset( $args['exclude_product_ids'] ) ? (array) $args['exclude_product_ids'] : array();
$meta_array = apply_filters( 'woocommerce_coupon_generator_coupon_meta_data', array(
'discount_type' => empty( $args['discount_type'] ) ? 'fixed_cart' : wc_clean( $args['discount_type'] ),
'coupon_amount' => wc_format_decimal( $args['coupon_amount'] ),
'individual_use' => isset( $args['individual_use'] ) ? 'yes' : 'no',
'product_ids' => implode( ',', array_filter( array_map( 'intval', $product_ids ) ) ),
'exclude_product_ids' => implode( ',', array_filter( array_map( 'intval', $exclude_ids ) ) ),
'usage_limit' => empty( $args['usage_limit'] ) ? '' : absint( $args['usage_limit'] ),
'usage_limit_per_user' => empty( $args['usage_limit_per_user'] ) ? '' : absint( $args['usage_limit_per_user'] ),
'limit_usage_to_x_items' => empty( $args['limit_usage_to_x_items'] ) ? '' : absint( $args['limit_usage_to_x_items'] ),
'expiry_date' => wc_clean( $args['expiry_date'] ),
'free_shipping' => isset( $args['free_shipping'] ) ? 'yes' : 'no',
'exclude_sale_items' => isset( $args['exclude_sale_items'] ) ? 'yes' : 'no',
'product_categories' => isset( $args['product_categories'] ) ? array_map( 'intval', $args['product_categories'] ) : array(),
'exclude_product_categories' => isset( $args['exclude_product_categories'] ) ? array_map( 'intval', $args['exclude_product_categories'] ) : array(),
'minimum_amount' => wc_format_decimal( $args['minimum_amount'] ),
'maximum_amount' => wc_format_decimal( $args['maximum_amount'] ),
'customer_email' => array_filter( array_map( 'trim', explode( ',', wc_clean( $args['customer_email'] ) ) ) ),
), $coupon_id );
$insert_meta_values = '';
// Insert all coupons meta
foreach ( $meta_array as $key => $value ) :
foreach ( $insert_coupon_ids as $coupon_id ) :
$insert_meta_values .= $wpdb->prepare( '(%d, %s, %s)', $coupon_id, sanitize_title( wp_unslash( $key ) ), maybe_serialize( wp_unslash( $value ) ) );
$meta_array_keys = array_keys( $meta_array );
if ( $key == end( $meta_array_keys ) && $coupon_id == end( $insert_coupon_ids ) ) :
$insert_meta_values .= ';';
else :
$insert_meta_values .= ', ';
endif;
endforeach;
endforeach;
$wpdb->query( "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) VALUES $insert_meta_values" );
$wpdb->query( 'COMMIT' );
}
/**
* Random coupon.
*
* Get a random coupon code.
*
* @since 1.0.0
*
* @return string Random coupon code.
*/
function wccg_get_random_coupon() {
// Generate unique coupon code
$random_coupon = '';
$length = 12;
$charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
$count = strlen( $charset );
while ( $length-- ) :
$random_coupon .= $charset[ mt_rand( 0, $count-1 ) ];
endwhile;
$random_coupon = implode( '-', str_split( strtoupper( $random_coupon ), 4 ) );
// Ensure coupon code is correctly formatted
$coupon_code = apply_filters( 'woocommerce_coupon_code', $random_coupon );
return $coupon_code;
}
/**
* AJAX Process coupons.
*
* Process a batch of coupons. This function is fired via a ajax call.
* Arguments are based on the $_POST['form_data'] arguments.
*
* @since 1.0.0
*/
function wccg_ajax_process_batch_coupons() {
parse_str( $_POST['form_data'], $post_data );
if ( ! isset( $post_data['generate_coupons_nonce'] ) || ! wp_verify_nonce( $post_data['generate_coupons_nonce'], 'wccg_generate_coupons' ) ) :
die( -1 );
endif;
$progress = 0;
$message = '';
$batch_size = 500;
$batch_step = absint( $_POST['batch_step'] );
$total_number_coupons = $post_data['number_of_coupons'];
$coupons_generated = $batch_step * $batch_size;
$coupons_to_generate = min( $total_number_coupons - $coupons_generated, $batch_size );
// Coupon generation
$start_time = microtime( true );
wccg_generate_coupons( $coupons_to_generate, $post_data );
$execution_time = microtime( true ) - $start_time;
// Step
$coupons_generated += $coupons_to_generate;
if ( $coupons_generated == $total_number_coupons ) :
$batch_step = 'done';
$message .= '<strong>' . sprintf( __( 'Coupon generation completed! Created %1$d coupons.', 'coupon-generator-for-woocommerce' ), $coupons_generated ) . '</strong><br/>';
else :
++$batch_step;
endif;
// Add message
$message .= sprintf( __( '%1$s coupons created in %2$s seconds', 'coupon-generator-for-woocommerce' ), $coupons_to_generate, round( $execution_time, 3 ) );
// Progress
$progress = round( $coupons_generated / $total_number_coupons * 100 );
die( json_encode( array( 'step' => $batch_step, 'progress' => $progress, 'message' => $message ) ) );
}
add_action( 'wp_ajax_wccg_generate_coupons', 'wccg_ajax_process_batch_coupons' );
=== Coupon Generator for WooCommerce ===
Contributors: sormano
Tags: woocommerce coupon generator, woocommerce, woocommerce coupon, coupons, coupon importer, woocommerce automatic coupons, woocommerce random coupons
Requires at least: 4.0.0
Tested up to: 4.7.3
Stable tag: 1.0.1
Requires PHP: 5.3
License: GPLv3 or later
License URI: http://www.gnu.org/licenses/gpl-3.0.html
Generate WooCommerce coupons easily and fast.
== Description ==
WooCommerce Coupon Generator is a coupon generator that allows you to setup and generator tens of thousands of coupons at the same time.
By going through 3 easy steps you can setup your desired coupons and start generating the coupons.
**Generation process**<br />
Step 0) Introduction, ‘how to’<br />
Step 1) Select coupon settings<br />
Step 2) Select generation settings<br />
Step 3) Coupons are being generated<br />
The coupons are generated in a very effective way so they will even work on low-cost hosting. During the generation process you will get updates about the progress.
Tested up to **1,000,000** (!!) coupons.
**Please leave a rating if you found this plugin useful**
If you have a feature request, let me know, they're always welcome!
== Installation ==
1. Upload the folder `woocommerce-coupon-generator to the `/wp-content/plugins/` directory
2. Activate the plugin through the 'Plugins' menu in WordPress
3. Go to the generation page and start generating :-)
== Screenshots ==
1. Start of generating coupons
2. Coupon options
3. Generation options
4. Generating coupons
5. Generated coupons
== Changelog ==
= 1.0.2 - 2017/04/13 =
* [Fix] - WC 3.0 incompatibility with the 'Products' / 'Excluded products' coupon setting
= 1.0.0 =
* Initial release
\ No newline at end of file
<?php
/**
* Plugin Name: WooCommerce Coupon Generator
* Plugin URI: http://jeroensormani.com/
* Description: Easily generate <strong>MILLIONS</strong> of unique coupons for your online store. Use all the coupon settings you are familiar with!
* Version: 1.0.1
* Author: Jeroen Sormani
* Author URI: http://jeroensormani.com/
* Text Domain: coupon-generator-for-woocommerce
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
/**
* Class WooCommerce_Coupon_Generator.
*
* Main WooCommerce_Coupon_Generator class initializes the plugin.
*
* @class WooCommerce_Coupon_Generator
* @version 1.0.0
* @author Jeroen Sormani
*/
class WooCommerce_Coupon_Generator {
/**
* Plugin version.
*
* @since 1.0.0
* @var string $version Plugin version number.
*/
public $version = '1.0.1';
/**
* Plugin file.
*
* @since 1.0.0
* @var string $file Plugin file path.
*/
public $file = __FILE__;
/**
* Instance of WooCommerce_Coupon_Generator.
*
* @since 1.0.0
* @access private
* @var object $instance The instance of WooCommerce_Coupon_Generator.
*/
private static $instance;
/**
* Construct.
*
* Initialize the class and plugin.
*
* @since 1.0.0
*/
public function __construct() {
// Initialize plugin parts
$this->init();
}
/**
* Instance.
*
* An global instance of the class. Used to retrieve the instance
* to use on other files/plugins/themes.
*
* @since 1.0.0
* @return object Instance of the class.
*/
public static function instance() {
if ( is_null( self::$instance ) ) :
self::$instance = new self();
endif;
return self::$instance;
}
/**
* Init.
*
* Initialize plugin parts.
*
* @since 1.0.0
*/
public function init() {
// Check if WooCommerce is active
if ( ! function_exists( 'is_plugin_active_for_network' ) ) :
require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
endif;
if ( ! in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) :
if ( ! is_plugin_active_for_network( 'woocommerce/woocommerce.php' ) ) :
return;
endif;
endif;
if ( is_admin() ) :
// Functions
require_once plugin_dir_path( __FILE__ ) . 'includes/admin/wccg-core-functions.php';
// Classes
require_once plugin_dir_path( __FILE__ ) . 'includes/admin/class-wccg-admin.php';
$this->admin = new WCCG_Admin();
endif;
// Load textdomain
$this->load_textdomain();
}
/**
* Textdomain.
*
* Load the textdomain based on WP language.
*
* @since 1.0.0
*/
public function load_textdomain() {
$locale = apply_filters( 'plugin_locale', get_locale(), 'coupon-generator-for-woocommerce' );
// Load textdomain
load_textdomain( 'coupon-generator-for-woocommerce', WP_LANG_DIR . '/woocommerce-coupon-generator/woocommerce-coupon-generator-' . $locale . '.mo' );
load_plugin_textdomain( 'coupon-generator-for-woocommerce', false, basename( dirname( __FILE__ ) ) . '/languages' );
}
}
/**
* The main function responsible for returning the WooCommerce_Coupon_Generator object.
*
* Use this function like you would a global variable, except without needing to declare the global.
*
* Example: <?php WooCommerce_Coupon_Generator()->method_name(); ?>
*
* @since 1.0.0
*
* @return object WooCommerce_Coupon_Generator class object.
*/
if ( ! function_exists( 'WooCommerce_Coupon_Generator' ) ) :
function WooCommerce_Coupon_Generator() {
return WooCommerce_Coupon_Generator::instance();
}
endif;
WooCommerce_Coupon_Generator();
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment