From 4a3d0acc7d1c2c404e7d07c2a166513cb210e76b Mon Sep 17 00:00:00 2001 From: Nicolas Gehin Date: Tue, 2 Oct 2018 17:12:07 +0200 Subject: [PATCH] NEW / The plugin can work in hours --- assets/woo_cao.js | 19 ++++++++++++++ includes/class-cao.php | 48 +++++++++++++++++++++++++++++----- woo-cancel-abandoned-order.php | 1 + 3 files changed, 61 insertions(+), 7 deletions(-) create mode 100644 assets/woo_cao.js diff --git a/assets/woo_cao.js b/assets/woo_cao.js new file mode 100644 index 0000000..81e9fe0 --- /dev/null +++ b/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); diff --git a/includes/class-cao.php b/includes/class-cao.php index 94dc048..2708917 100644 --- a/includes/class-cao.php +++ b/includes/class-cao.php @@ -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 ); @@ -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( @@ -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', ), ); @@ -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 ); } } } diff --git a/woo-cancel-abandoned-order.php b/woo-cancel-abandoned-order.php index 48ebebf..3a57445 100644 --- a/woo-cancel-abandoned-order.php +++ b/woo-cancel-abandoned-order.php @@ -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' ) ) {