Different Time Limit According To User Roles
-
Hello, RVOLA Studio!
First of all I would say thank you for developing such an awesome plugin! It helped a lot.
But currently I am looking for a way around for changing the time limit according to user roles, so VIP members can have longer time limit before their on-hold orders get cancelled. Any ideas?
Thanks in advance!
-
Hello @ysteventan
I will study this possibilityHello @ysteventan
do you have knowledge of SQL?Yes, I do.
Btw, I found a solution, and would like to know what you think about this.
First, I add the value customer role to metadata
//ADD CUSTOMER ROLE TO METADATA add_action('woocommerce_checkout_create_order', 'before_checkout_create_order', 20, 2); function before_checkout_create_order( $order, $data ) { $user = wp_get_current_user(); $role = ( array ) $user->roles; $order->update_meta_data( '_user_role', $role[0] ); }
Then, I create a duplicate of your SQL Query in check_order() function, with a longer $old_date (I multiply the time by 2 so users with role “author” will have twice longer time than other user roles)
/** * Main method that tracks options and orders pending payment. * If the elements match (activation for the gateway, lifetime, command on hold), the system will cancel the command if it exceeds its time. */ public function check_order() { global $wpdb; if ( $this->gateways ) { foreach ( $this->gateways as $gateway ) { $options = get_option( 'woocommerce_' . $gateway . '_settings' ); if ( isset( $options ) && is_array( $options ) && isset( $options['woocao_enabled'] ) && 'yes' === $options['woocao_enabled'] ) { $restock = isset( $options['woocao_restock'] ) && 'yes' === $options['woocao_restock'] ? 'yes' : 'no'; // 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' ); break; case 'hourly': $old_date = current_time( 'timestamp' ) - ( $options['woocao_hours'] * HOUR_IN_SECONDS ); $old_date_author = current_time( 'timestamp' ) - ( 2 * $options['woocao_hours'] * HOUR_IN_SECONDS ); break; } $old_date = apply_filters( 'woo_cao_date_order', $old_date, $gateway, $mode ); $old_date_author = apply_filters( 'woo_cao_date_order', $old_date_author, $gateway, $mode ); $old_date_format = date( 'Y-m-d 00:00:00', $old_date ); $old_date_author_format = date( 'Y-m-d 00:00:00', $old_date_author ); $orders = $wpdb->get_results( $wpdb->prepare( " SELECT posts.ID FROM $wpdb->posts as posts INNER JOIN $wpdb->postmeta as meta ON posts.ID = meta.post_id WHERE posts.post_type = 'shop_order' AND posts.post_status = 'wc-on-hold' AND posts.post_date < %s AND meta.meta_key = '_user_role' AND meta.meta_value != 'author' ", $old_date_format ) ); $orders_author = $wpdb->get_results( $wpdb->prepare( " SELECT posts.ID FROM $wpdb->posts as posts INNER JOIN $wpdb->postmeta as meta ON posts.ID = meta.post_id WHERE posts.post_type = 'shop_order' AND posts.post_status = 'wc-on-hold' AND posts.post_date < %s AND meta.meta_key = '_user_role' AND meta.meta_value = 'author' ", $old_date_author_format ) ); if ( $orders ) { foreach ( $orders as $order ) { // Cancel order. $this->cancel_order( $order->ID ); // Restock product. if ( 'yes' === $restock ) { $this->restock( $order->ID ); } } wp_cache_flush(); } if ( $orders_author ) { foreach ( $orders_author as $order ) { // Cancel order. $this->cancel_order( $order->ID ); // Restock product. if ( 'yes' === $restock ) { $this->restock( $order->ID ); } } wp_cache_flush(); } } } } }
Sorry if the code is a little bit unpretty, would like to hear what you think about it.
- This reply was modified 6 years ago by ysteventan.
Sorry, the above code does not work for me, this one is tested and working :
/** * Main method that tracks options and orders pending payment. * If the elements match (activation for the gateway, lifetime, command on hold), the system will cancel the command if it exceeds its time. */ public function check_order() { global $wpdb; if ( $this->gateways ) { foreach ( $this->gateways as $gateway ) { $options = get_option( 'woocommerce_' . $gateway . '_settings' ); if ( isset( $options ) && is_array( $options ) && isset( $options['woocao_enabled'] ) && 'yes' === $options['woocao_enabled'] ) { $restock = isset( $options['woocao_restock'] ) && 'yes' === $options['woocao_restock'] ? 'yes' : 'no'; // 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' ); break; case 'hourly': $old_date = current_time( 'timestamp' ) - ( 1 * HOUR_IN_SECONDS ); $old_date_author = current_time( 'timestamp' ) - ( 2 * HOUR_IN_SECONDS ); break; } //$old_date = apply_filters( 'woo_cao_date_order', $old_date, $gateway, $mode ); //$old_date_author = apply_filters( 'woo_cao_date_order', $old_date_author, $gateway, $mode ); $old_date_format = date( 'Y-m-d H:i:s', $old_date ); $old_date_author_format = date( 'Y-m-d H:i:s', $old_date_author ); $orders = $wpdb->get_results( $wpdb->prepare( " SELECT posts.ID FROM $wpdb->posts as posts INNER JOIN $wpdb->postmeta as meta ON posts.ID = meta.post_id WHERE posts.post_type = 'shop_order' AND posts.post_status = 'wc-on-hold' AND posts.post_date < %s AND meta.meta_key = '_user_role' AND meta.meta_value != 'author' ", $old_date_format ) ); $orders_author = $wpdb->get_results( $wpdb->prepare( " SELECT posts.ID FROM $wpdb->posts as posts INNER JOIN $wpdb->postmeta as meta ON posts.ID = meta.post_id WHERE posts.post_type = 'shop_order' AND posts.post_status = 'wc-on-hold' AND posts.post_date < %s AND meta.meta_key = '_user_role' AND meta.meta_value = 'author' ", $old_date_author_format ) ); if ( $orders ) { foreach ( $orders as $order ) { // Cancel order. $this->cancel_order( $order->ID ); // Restock product. if ( 'yes' === $restock ) { $this->restock( $order->ID ); } } wp_cache_flush(); } if ( $orders_author ) { foreach ( $orders_author as $order ) { // Cancel order. $this->cancel_order( $order->ID ); // Restock product. if ( 'yes' === $restock ) { $this->restock( $order->ID ); } } wp_cache_flush(); } } } } }
Hi @ysteventan
Can you test this version of the extension with the filter available to handle the SQL query.
https://github.com/rvola/woo-cancel-abandoned-order/tree/test/sql_filterHere is the added code : https://github.com/rvola/woo-cancel-abandoned-order/commit/4aed0ba106735d9f4f46f2530ac5e392ef5ee1e4
remember to rename the folder if you download it from Github by removing “-test-sql_filter” from the folder name
Hello @ysteventan
can you tell me if this code helps you?
- The topic ‘Different Time Limit According To User Roles’ is closed to new replies.