• Resolved alexzaragoza21

    (@alexzaragoza21)


    Hello, I have read your documentation regarding post count reset,

    I found this:

    add_action( 'woocommerce_order_status_completed', 'upl_wc_update_user_cycle' );
    function upl_wc_update_user_cycle( $order_id ) {
    	$products_id = [ '683' ];
    	$order = wc_get_order( $order_id );
    	$user_id = $order->get_user_id();
    	if ( ! user_can( $user_id, get_option( 'upl_manage_cap', 'manage_options' ) ) ) {
    		foreach ( $order->get_items() as $item ) {
    			if ( in_array( $item->get_product_id(), $products_id ) ) {
    				if ( empty( get_user_meta( $user_id, 'cycle', true ) ) || get_user_meta( $user_id, 'cycle', true ) <= current_time( 'Y-m-d' ) ) {
    					update_user_meta( $user_id, 'cycle', date( 'Y-m-d', strtotime( current_time( 'Y-m-d' ) . ' + 1 day' ) ) );
    				}
    				break;
    			}
    		}
    	}
    }

    Is there a way to reset the post count right after the successful purchase? Thank you so much in advance.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author Condless

    (@condless)

    Hi,
    The posts count can be reset by the end of the day of the purchase, you can copy the code which appears under the ‘How to allow posts count reset per user?’, in addition to the code that you mentioned (just update the product id that grant the reset).

    Thread Starter alexzaragoza21

    (@alexzaragoza21)

    Thank you so much! I hope there is a way that after the purchase the post limit/record will be updated.

    But I am already thankful to this plugin. You guys are the best!

    Hello @condless
    I was wondering the same thing, but if you can elaborate more in your explanation or show me what code to add that would be great.
    P.S
    I am still new to WordPress.

    Plugin Author Condless

    (@condless)

    Hi,
    2 code snippets should be copied into your functions.php file, the first appears under the ‘How to allow posts count reset per user?’, and the second under the ‘How to reset user’s posts count when purchase some product in WooCommerce?’, in the second snippet you should set the $product_id with the id of the product which will reset the count.
    Then when some user purchase this product his count will be reset at the end of the day (12:00 AM).

    Hello @condless

    Thank you very much for the brief explanation, but if you can add the whole code I should add to the reply that would be great. thank you very much for the help, but as I told you I’m really a beginner at WordPress.

    Thank you :).

    Plugin Author Condless

    (@condless)

    add_filter( 'upl_query', 'upl_users_cycle', 10, 2 );
    add_action( 'show_user_profile', 'upl_add_cycle_user_data' );
    add_action( 'edit_user_profile', 'upl_add_cycle_user_data' );
    add_action( 'personal_options_update', 'upl_save_cycle_user_data' );
    add_action( 'edit_user_profile_update', 'upl_save_cycle_user_data' );
    add_filter( 'manage_users_columns', 'upl_cycle_user_table' );
    add_filter( 'manage_users_custom_column', 'upl_cycle_user_table_row', 10, 3 );
    
    function upl_users_cycle( $args, $i ) {
    	if ( ! user_can( $args['author'], get_option( 'upl_manage_cap', 'manage_options' ) ) ) {
    		$rule_role = '';
    		$cycle = get_user_meta( $args['author'], 'cycle', true );
    		if ( ( empty( $rule_role ) || $rule_role === get_option( 'upl_user_role' )[ $i ] ) && ! empty( $cycle ) && $cycle <= current_time( 'Y-m-d' ) ) {
    			$args['date_query']['after'] = date( 'Y-m-d', strtotime( $cycle . ' - 1 day' ) );
    		}
    	}
    	return $args;
    }
    
    function upl_add_cycle_user_data( $user ) {
    	if ( current_user_can( get_option( 'upl_manage_cap', 'manage_options' ) ) ) { ?>
    		<h3><?php esc_html_e( 'User Posts Limit', 'user-posts-limit' ); ?></h3>
    		<table class="form-table">
    			<tr>
    				<th><label for="cycle"><?php esc_html_e( 'Cycle', 'user-posts-limit' ); ?></label></th>
    				<td><input type="date" min="1970-01-01" name="cycle" value="<?php echo esc_attr( get_user_meta( $user->ID, 'cycle', true ) ); ?>" class="regular-text" /></td>
    			</tr>
    		</table>
    		<br>
    	<?php }
    }
    
    function upl_save_cycle_user_data( $user_id ) {
    	if ( current_user_can( get_option( 'upl_manage_cap', 'manage_options' ) ) ) {
    		update_user_meta( $user_id, 'cycle', sanitize_text_field( $_POST['cycle'] ) );
    	}
    }
    
    function upl_cycle_user_table( $columns ) {
    	$columns['cycle'] = __( 'Cycle', 'user-posts-limit' );
    	return $columns;
    }
    
    function upl_cycle_user_table_row( $row_output, $column_id_attr, $user_id ) {
    	return 'cycle' === $column_id_attr ? get_user_meta( $user_id, 'cycle', true ) : $row_output;
    }
    
    add_action( 'woocommerce_order_status_completed', 'upl_wc_update_user_cycle' );
    function upl_wc_update_user_cycle( $order_id ) {
    	$products_id = [ '683' ]; // Here update your product ID
    	$order = wc_get_order( $order_id );
    	$user_id = $order->get_user_id();
    	if ( ! user_can( $user_id, get_option( 'upl_manage_cap', 'manage_options' ) ) ) {
    		foreach ( $order->get_items() as $item ) {
    			if ( in_array( $item->get_product_id(), $products_id ) ) {
    				if ( empty( get_user_meta( $user_id, 'cycle', true ) ) || get_user_meta( $user_id, 'cycle', true ) <= current_time( 'Y-m-d' ) ) {
    					update_user_meta( $user_id, 'cycle', date( 'Y-m-d', strtotime( current_time( 'Y-m-d' ) . ' + 1 day' ) ) );
    				}
    				break;
    			}
    		}
    	}
    }

    Thank you @condless for the great support :).

    Hi @condless,

    Just a quick question related to this topic, can the count be reset instantaneously after the purchase. And not after 12am? If so how would you go about implementing that?

    Thanks so much!

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Reset post count after product purchase’ is closed to new replies.