Skip to content

Commit

Permalink
NEW / The plugin can work in hours
Browse files Browse the repository at this point in the history
  • Loading branch information
rvola committed Oct 2, 2018
1 parent 5d1a381 commit 4a3d0ac
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 7 deletions.
19 changes: 19 additions & 0 deletions assets/woo_cao.js
@@ -0,0 +1,19 @@
(function ($) {
$(document).ready(function () {

var woo_cao_select = '.woo_cao-field-mode';

function woo_cao_check_mode() {
$('.woo_cao-field-moded').parents('tr').hide();

var woo_cao_mode = $(woo_cao_select).find(':selected').val();
$('.woo_cao-field-' + woo_cao_mode).parents('tr').show();
}

$(woo_cao_select).on('change', function () {
woo_cao_check_mode();
});

woo_cao_check_mode();
});
})(jQuery);
48 changes: 41 additions & 7 deletions includes/class-cao.php
Expand Up @@ -66,7 +66,7 @@ private function add_event_cron() {

if ( ! wp_next_scheduled( self::CRON_EVENT ) ) {
wp_schedule_event(
strtotime( 'yesterday 0 hours' ), 'daily', self::CRON_EVENT
strtotime( 'yesterday 0 hours' ), 'hourly', self::CRON_EVENT
);
}
add_action( self::CRON_EVENT, array( $this, 'check_order' ), 10 );
Expand Down Expand Up @@ -95,13 +95,23 @@ public function check_order() {
isset( $options ) && is_array( $options )
&& isset( $options['woocao_enabled'] )
&& 'yes' === $options['woocao_enabled']
&& isset( $options['woocao_days'] )
&& ! empty( $options['woocao_days'] )
) {

$restock = isset( $options['woocao_restock'] ) && 'yes' === $options['woocao_restock'] ? 'yes' : 'no';

$old_date = strtotime( 'today -' . $options['woocao_days'] . ' days' );
$old_date_format = date( 'Y-m-d 00:00:00', $old_date );
// Calculate time, depending on the mode
$mode = isset( $options['woocao_mode'] ) ? esc_html( $options['woocao_mode'] ) : 'daily';
switch ( $mode ) {
case 'daily':
$old_date = strtotime( 'today -' . $options['woocao_days'] . ' days' );
$old_date_format = date( 'Y-m-d 00:00:00', $old_date );
break;

case 'hourly':
$old_date = current_time( 'timestamp' ) - ( $options['woocao_hours'] * HOUR_IN_SECONDS );
$old_date_format = date( 'Y-m-d H:00:00', $old_date );
break;
}

$orders = $wpdb->get_results(
$wpdb->prepare(
Expand Down Expand Up @@ -202,15 +212,38 @@ public function add_fields( $fields ) {
'type' => 'checkbox',
'label' => __( 'Activation the automatic cancellation of orders.', 'woo-cancel-abandoned-order' ),
'default' => 'no',
'value' => array(
'hourly' => __( 'Hourly', 'woo-cancel-abandoned-order' ),
'daily' => __( 'Daily', 'woo-cancel-abandoned-order' )
),
'description' => __( 'Enable this option to automatically cancel all "on Hold" orders that you have not received payment for.', 'woo-cancel-abandoned-order' ),
),
'woocao_mode' => array(
'title' => __( 'Mode', 'woo-cancel-abandoned-order' ),
'type' => 'select',
'label' => __( 'Activation the automatic cancellation of orders.', 'woo-cancel-abandoned-order' ),
'default' => 'daily',
'options' => array(
'hourly' => __( 'Hourly', 'woo-cancel-abandoned-order' ),
'daily' => __( 'Daily', 'woo-cancel-abandoned-order' )
),
'class' => 'woo_cao-field-mode',
),
'woocao_hours' => array(
'title' => __( 'Lifetime in hour', 'woo-cancel-abandoned-order' ),
'type' => 'number',
'description' => __( 'Enter the number of hours (whole number) during which the system must consider a "pending" command as canceled.', 'woo-cancel-abandoned-order' ),
'default' => apply_filters( 'woo_cao_default_hours', '1' ),
'placeholder' => __( 'days', 'woo-cancel-abandoned-order' ),
'class' => 'woo_cao-field-hourly woo_cao-field-moded',
),
'woocao_days' => array(
'title' => __( 'Lifetime ', 'woo-cancel-abandoned-order' ),
'title' => __( 'Lifetime in days', 'woo-cancel-abandoned-order' ),
'type' => 'number',
'description' => __( 'Enter the number of days that the system must consider a "on Hold" order as canceled.', 'woo-cancel-abandoned-order' ),
'default' => apply_filters( 'woo_cao_default_days', '15' ),
'placeholder' => __( 'days', 'woo-cancel-abandoned-order' ),
'class' => 'woo_cao-field-days',
'class' => 'woo_cao-field-daily woo_cao-field-moded',
),
);

Expand All @@ -234,6 +267,7 @@ public function add_fields( $fields ) {
public function assets( $hook ) {
if ( 'woocommerce_page_wc-settings' == $hook ) {
wp_enqueue_style( 'woo_cao', plugins_url( 'assets/woo_cao.css', WOOCAO_FILE ), null, WOOCAO_VERSION, 'all' );
wp_enqueue_script( 'woo_cao', plugins_url( 'assets/woo_cao.js', WOOCAO_FILE ), array( 'jquery' ), WOOCAO_VERSION, true );
}
}
}
1 change: 1 addition & 0 deletions woo-cancel-abandoned-order.php
Expand Up @@ -32,6 +32,7 @@
}

define( 'WOOCAO_FILE', __FILE__ );
define( 'WOOCAO_VERSION', '1.4.0' );

include_once ABSPATH . 'wp-admin/includes/plugin.php';
if ( is_plugin_active( 'woocommerce/woocommerce.php' ) ) {
Expand Down

0 comments on commit 4a3d0ac

Please sign in to comment.