我正在为FOR EDD创建一个插件,为您提供管理整个EDD下载的季节性折扣(偶尔进行)的选项,并在任何日期和时间安排折扣。问题是这些值没有保存到数据库。
嗨,我正在为FOR EDD创建一个插件,可为您提供管理整个EDD下载的季节性和不定期折扣的选项,并在任何日期和时间安排折扣。问题是这些值没有保存到数据库。
我的代码:
if(!defined('ABSPATH')) exit;
// Add simple sale price field
add_action( 'edd_after_price_field','wsds_product_schedule_sale_data_fields' );
// Add fields to EDD save
function wsds_product_schedule_sale_data_fields() {
global $post;
$now=time();
$post_id=$post->ID;
$status=get_post_meta($post_id,'wsds_schedule_sale_status',true);
$start_time=get_post_meta($post_id,'wsds_schedule_sale_st_time',true);
$end_time=get_post_meta($post_id,'wsds_schedule_sale_end_time',true);
$mode=get_post_meta($post_id,'wsds_schedule_sale_mode',true);
$discount_type=get_post_meta($post_id,'wsds_schedule_sale_discount_type',true);
$sale_price=get_post_meta($post_id,'wsds_schedule_sale_sale_price',true);
$start_countdown=get_post_meta($post_id,'wsds_schedule_sale_start_countdown',true);
$end_countdown=get_post_meta($post_id,'wsds_schedule_sale_end_countdown',true);
if(!empty($start_time))
{
$start_date=date('Y-m-d', $start_time);
$st_mm=date('m', $start_time);
$st_dd=date('d', $start_time);
$st_hh=date('H', $start_time);
$st_mn=date('i', $start_time);
}
if(isset($end_time) &!empty($end_time))
{
$end_date=date('Y-m-d', $end_time);
$end_mm=date('m', $end_time);
$end_dd=date('d', $end_time);
$end_hh=date('H', $end_time);
$end_mn=date('i', $end_time);
}
?>
<div id ='product_schedule_sale_data_panel' class ='panel woocommerce_options_panel wsds_options_panel' >
<div class = 'wsds_options_group' >
<p class='form-field wsds_select_status'>
<label for='wsds_select_status'><?php _e( 'Status', 'edd' ); ?></label>
<select name='wsds_select_status' class='wsds_enhanced_select' id="wsds_select_status">
<option <?php if($status==0){ echo "selected"; } ?> value='0'>Disable</option>
<option <?php if($status==1){ echo "selected"; } ?> value='1'>Enable</option>
</select>
</p>
<p class="form-field wsds_select_start_time">
<label for='wsds_select_start_time'><?php _e( 'Start Time', 'edd' ); ?></label>
<span class="screen-reader-text wsds_start_yy">Year</span>
<input type="text" id="wsds_st_date" class="wsds_st_date" name="wsds_st_date" value="<?php echo $start_date; ?>" placeholder="From… YYYY-MM-DD" maxlength="10" autocomplete="off">
<span>@</span>
<span class="screen-reader-text wsds_hh">Hour</span>
<input type="text" id="wsds_st_hh" class="wsds_st_hh" name="wsds_st_hh" placeholder="HH" value="<?php echo $st_hh; ?>" size="2" maxlength="2" autocomplete="off"><span>:</span>
<span class="screen-reader-text wsds_mn">Minute</span>
<input type="text" id="wsds_st_mn" class="wsds_st_mn" name="wsds_st_mn" placeholder="MM" value="<?php echo $st_mn; ?>" size="2" maxlength="2" autocomplete="off">
<span>GMT</span>
</p>
<p class="form-field wsds_select_end_time">
<label for='wsds_select_end_time'><?php _e( 'End Time', 'edd' ); ?></label>
<span class="screen-reader-text wsds_end_yy">Year</span>
<input type="text" id="wsds_end_date" class="wsds_end_date" name="wsds_end_date" value="<?php echo $end_date; ?>" placeholder="From… YYYY-MM-DD" maxlength="10" autocomplete="off">
<span>@</span>
<span class="screen-reader-text">Hour</span>
<input type="text" id="wsds_end_hh" class="wsds_end_hh" name="wsds_end_hh" placeholder="HH" value="<?php echo $end_hh; ?>" size="2" maxlength="2" autocomplete="off">
<span>:</span>
<span class="screen-reader-text">Minute</span>
<input type="text" id="wsds_end_mn" class="wsds_end_mn" name="wsds_end_mn" placeholder="MM" value="<?php echo $end_mn; ?>" size="2" maxlength="2" autocomplete="off">
<span>GMT</span>
</p>
<p class="form-field wsds_discounttype">
<label for='wsds_select_discounttype'><?php _e( 'Discount Type', 'edd' ); ?></label>
<select name='wsds_discounttype' class='wsds_enhanced_select' id="wsds_discounttype">
<option <?php if($discount_type=="Fixed Price Discount"){ echo "selected"; } ?> value='Fixed Price'>Fixed Price Discount</option>
<option <?php if($discount_type=="Percentage"){ echo "selected"; } ?> value='Percentage'>%(Percentage)</option>
</select>
</p>
<p class="form-field wsds_saleprice">
<label for='wsds_saleprice'><?php _e( 'Sale Price Discount', 'edd' ); ?></label>
<input type="text" id="wsds_saleprice" class="wsds_saleprice" name="wsds_saleprice" value="<?php echo $sale_price; ?>" placeholder="Sale Price Discount" autocomplete="off">
<span>Off regular price</span>
</p>
<p class="form-field wsds_sale_begin_countdown">
<label for='wsds_select_start_time'><?php _e( 'Future Sale CountDown', 'edd' ); ?></label>
<select name='wsds_sale_start_countdown' class='wsds_enhanced_select' id="wsds_sale_start_countdown">
<option <?php if($start_countdown==0){ echo "selected"; } ?> value='0'>Disable</option>
<option <?php if($start_countdown==1){ echo "selected"; } ?> value='1'>Enable</option>
</select><span>CountDown is showing until start sale</span>
</p>
<p class="form-field wsds_sale_ongoing_countdown">
<label for='wsds_select_end_time'><?php _e( 'On Sale CountDown', 'edd' ); ?></label>
<select name='wsds_sale_end_countdown' class='wsds_enhanced_select' id="wsds_sale_end_countdown">
<option <?php if($end_countdown==0){ echo "selected"; } ?> value='0'>Disable</option>
<option <?php if($end_countdown==1){ echo "selected"; } ?> value='1'>Enable</option>
</select><span>CountDown is showing during sale</span>
</p>
<p class="form-field wsds_note">Note: Start time and End time will be on GMT, Current GMT time is: <?php echo date("Y-m-d @ H:i",$now); ?><br> Note: Sale price discount will be consider on regular price. </p>
</div>
</div><?php
}
/** Hook callback function to save custom fields information */
function wsds_save_discount_sale_schedule_data($post_id) {
global $post;
$wsds_error=false;
$wsds_st_hh=00;
$wsds_st_mn=00;
$wsds_end_hh=00;
$wsds_end_mn=00;
$wsds_status =sanitize_text_field($_POST['wsds_select_status']);
$wsds_discounttype =sanitize_text_field($_POST['wsds_discounttype']);
$wsds_saleprice =sanitize_text_field($_POST['wsds_saleprice']);
$start_countdown =sanitize_text_field($_POST['wsds_sale_start_countdown']);
$end_countdown =sanitize_text_field($_POST['wsds_sale_end_countdown']);
$wsds_st_date=sanitize_text_field($_POST['wsds_st_date']);
if(!empty($_POST['wsds_st_hh'])) $wsds_st_hh=sanitize_text_field($_POST['wsds_st_hh']);
if(!empty($_POST['wsds_st_mn'])) $wsds_st_mn=sanitize_text_field($_POST['wsds_st_mn']);
$wsds_end_date=sanitize_text_field($_POST['wsds_end_date']);
if(!empty($_POST['wsds_end_hh'])) $wsds_end_hh=sanitize_text_field($_POST['wsds_end_hh']);
if(!empty($_POST['wsds_end_mn'])) $wsds_end_mn=sanitize_text_field($_POST['wsds_end_mn']);
$wsds_start_schedule_hook="wsds_start_shedule_sale_discount";
$wsds_end_schedule_hook="wsds_end_shedule_sale_discount";
//Y-m-d H:i:s
$wsds_st_time=strtotime($wsds_st_date." ".$wsds_st_hh.":".$wsds_st_mn.":00");
//echo "start time".$wsds_st_time;
$wsds_end_time=strtotime($wsds_end_date." ".$wsds_end_hh.":".$wsds_end_mn.":00");
if($wsds_status==1)
{
wp_clear_scheduled_hook( $wsds_start_schedule_hook, array($post->ID) );
wp_clear_scheduled_hook( $wsds_end_schedule_hook, array($post->ID) );
wp_schedule_single_event($wsds_st_time, $wsds_start_schedule_hook,array($post->ID));
wp_schedule_single_event($wsds_end_time, $wsds_end_schedule_hook,array($post->ID));
}
// Save Data
if (!empty($wsds_st_date) && !empty($wsds_end_date)) {
update_post_meta($post_id,'wsds_schedule_sale_status',$wsds_status);
update_post_meta($post_id,'wsds_schedule_sale_st_time',$wsds_st_time);
update_post_meta($post_id,'wsds_schedule_sale_end_time',$wsds_end_time);
update_post_meta($post_id,'wsds_schedule_sale_discount_type',$wsds_discounttype);
update_post_meta($post_id,'wsds_schedule_sale_sale_price',$wsds_saleprice);
update_post_meta($post_id,'wsds_schedule_sale_start_countdown',$start_countdown);
update_post_meta($post_id,'wsds_schedule_sale_end_countdown',$end_countdown);
if($wsds_st_time > time())
{
update_post_meta($post_id,'wsds_schedule_sale_mode',0);
}
}
}
add_filter( 'edd_metabox_fields_save','wsds_save_discount_sale_schedule_data' );