• Resolved nolabelstudios

    (@nolabelstudios)


    Hi,

    I need the ability to limit the total number products, or any post type really, not by role but just a total count.

    My use case right now is someone on my “Basic” plan can take 250 WooCommerce “Orders” per month. I don’t want to assign this to a user but instead limit it regardless of the role of a user. So this needs to work for Guest orders as well as Customers who are logged in when ordering.

    Can this be added or done with the current plugin?

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

    (@condless)

    Hi,
    To limit the orders regardless of the user role use the code from the plugin’s docs ‘How to make rules that applied on certain post type to limit by the total posts in the website?’ and update: $post_type = ‘shop_order’;

    Thread Starter nolabelstudios

    (@nolabelstudios)

    @condless do I apply this to my functions.php file? Please confirm.

    Plugin Author Condless

    (@condless)

    Yes, functions.php of your theme (preferably child theme).

    Thread Starter nolabelstudios

    (@nolabelstudios)

    Hi,

    I’ve added the code and it works well. What if I need to limit the total number of sitewide posts for 3 post types?

    The code I have running at the moment is this:

    add_filter( 'upl_query', 'upl_limit_total_posts' );
    function upl_limit_total_posts( $args ) {
    	$post_type = 'shop_order';
    	if ( $post_type === $args['post_type'] ) {
    		unset( $args['author'] );
    	}
    	return $args;
    }

    Thank you for your continued help.

    Thread Starter nolabelstudios

    (@nolabelstudios)

    Also, this setting doesn’t actually stop someone placing an order on the front end of the site. It only stops backend order creation.

    Plugin Author Condless

    (@condless)

    Hi,
    To limit by the total number for orders and for products, and apply the order limit on all users/guests redownload the plugin and use the following code instead of the previous one:

    add_filter( 'upl_query', 'upl_limit_total_posts' );
    add_filter( 'upl_rule_limit_current_user_role_check', 'upl_disable_user_role', 10, 2 );
    
    function upl_limit_total_posts( $args ) {
    	$post_types = [ 'shop_order', 'product' ];
    	if ( in_array( $args['post_type'], $post_types ) ) {
    		unset( $args['author'] );
    	}
    	return $args;
    }
    
    function upl_disable_user_role( $check, $i ) {
    	$post_type = 'shop_order';
    	return $post_type === get_option( 'upl_posts_type' )[ $i ] ? true : $check;
    }
    Thread Starter nolabelstudios

    (@nolabelstudios)

    @condless thank you so much for the adjusted code. It worked lovely. I do see some flaws in my setup that require additional help. Apologies for only discovering these now.

    1. Can the code be adjusted so that if an order is set to “Completed” this is no longer counted towards their order limit but only when the next cycle begins?

    2. Likewise, if an order is received, marked as “Completed” and then purged from the “Trash” before the next cycle begins can we still count those deleted items in their shop_order limit for the current cycle? I would only want this to apply for orders who’s status is set to “Completed” before being moved to the trash and purged. Orders that are not set to “Completed” before being deleted should not count towards the total shop_order in the current cycle.

    Bonus question – Is there any way of preventing customers from adding items to their basket and/or getting to the checkout page once the shop_order is reached for the current cycle? I don’t want customers to be able to create an order, which likely takes time and effort, only to be disappointed when the checkout fails and no error is shown to confirm why.

    Perhaps we could hide a certain class or id from the front end to prevent customers clicking the “add to cart” button. Is that possible once the total shop_order limit is reached in the current cycle? The frontend element is:

    button.orderable-button.orderable-product__add-to-order

    Plugin Author Condless

    (@condless)

    1. Can you further explain what you are trying to achieve?

    2. You can prevent shop owners from deleting completed orders from the last month:

    add_filter( 'pre_delete_post', 'upl_restrict_post_deletion', 10, 2 );
    function upl_restrict_post_deletion( $check, $post ) {
    	return ! current_user_can( get_option( 'upl_manage_cap' ) ) && 'shop_order' === get_post_type( $post ) && 'wc-completed' === get_post_meta( $post->ID, '_wp_trash_meta_status', true ) && strtotime( get_the_date( 'Y-m-d', $post ) . ' + 1 month' ) > strtotime( date( 'Y-m-d' ) ) ? false : $check;
    }

    3. Hide buttons if limit exceeded (update the button’s class):

    add_action( 'wp_head', 'upl_hide_button' );
    function upl_hide_button() {
    	echo do_shortcode( '[upl_hide type="shop_order" message="<style>.button{ display: none !important; }<style>"][/upl_hide]' );
    }
    Thread Starter nolabelstudios

    (@nolabelstudios)

    Happy to clarify…

    1. If a customer receives orders in the cycle/month and does not delete them will those same orders be counted in the next cycle/month? If they are can those orders NOT be counted in the next cycle/month?

    2. What I need here is a way of preventing shop owners from deleting “completed” orders that were received in the current cycle/month. When the next cycle/month begins I want shop owners to be able to delete previous cycle/months orders.

    3. I can’t wrap the add to cart button in a shortcode (I assume that’s how this should work). Is there a different way of hiding the button if the order limit is reached? For example if I use the adjusted code below it hides the button 100% of the time. Can it be adjusted to only run when the order limit is reached?

    add_action( 'wp_head', 'upl_hide_button' );
    function upl_hide_button() {
    	echo ( '<style>.button{ display: none !important; }<style>' );
    }
    Plugin Author Condless

    (@condless)

    You can enable the ‘Documents Statistics’ option and see the counts in the users table list (Dashboard => Users).
    1. ‘Monthly’ cycle means that orders older than 30 days won’t be counted.
    2. The code from the previous comment should fit.
    3. Use the code as is, you can also replace ‘.button’ with more specific class/id of your add to cart button.

    Thread Starter nolabelstudios

    (@nolabelstudios)

    1. Great, it sounds like it just works out of the box that way.

    2. I’ll use that code and report back if I notice anything weird.

    3. I changed the code to use the class name of the button on my frontend page but it didn’t hide the button. It could be because I can’t seem to get the limits to apply anymore. See below…

    For some reason I can’t get the limits to work at all now. It just says “unlimited” in the WordPress Dashboard widget. This is what I see.

    https://e1.pcloud.link/publink/show?code=XZeBfpZEEUDjjgpH5mH7c8Je7bIULdFXYck

    To confirm my functions.php code has this code in it. Perhaps I copied it wrong – but then again WordPress never flagged an error in the code.

    // User post limits - limit total products and total orders - orders are disabled if limit reached
    
    add_filter( 'upl_query', 'upl_limit_total_posts' );
    add_filter( 'upl_rule_limit_current_user_role_check', 'upl_disable_user_role', 10, 2 );
    
    function upl_limit_total_posts( $args ) {
    	$post_types = [ 'shop_order', 'product' ];
    	if ( in_array( $args['post_type'], $post_types ) ) {
    		unset( $args['author'] );
    	}
    	return $args;
    }
    
    function upl_disable_user_role( $check, $i ) {
    	$post_type = 'shop_order';
    	return $post_type === get_option( 'upl_posts_type' )[ $i ] ? true : $check;
    }
    
    add_filter( 'pre_delete_post', 'upl_restrict_post_deletion', 10, 2 );
    function upl_restrict_post_deletion( $check, $post ) {
    	return ! current_user_can( get_option( 'upl_manage_cap' ) ) && 'shop_order' === get_post_type( $post ) && 'wc-completed' === get_post_meta( $post->ID, '_wp_trash_meta_status', true ) && strtotime( get_the_date( 'Y-m-d', $post ) . ' + 1 month' ) > strtotime( date( 'Y-m-d' ) ) ? false : $check;
    }
    
    // Hide Order System order button from frontend if order limit reached in current cycle
    add_action( 'wp_head', 'upl_hide_button' );
    function upl_hide_button() {
    	echo do_shortcode( '[upl_hide type="shop_order" message="<style>button.orderable-button.orderable-product__add-to-order{ display: none !important; }<style>"][/upl_hide]' );
    }

    I’m very thankful for your support so far especially since this plugin is free. Hopefully you can help me get these final issues ironed out and I’ll leave you be for a while 😀

    Plugin Author Condless

    (@condless)

    3. try ‘manage_options’ in the ‘Plugin Management Capability’ option (it must contain a capability that the Kitchen Manager role doesn’t have).

    Thread Starter nolabelstudios

    (@nolabelstudios)

    I tried every option and nothing works. If everything else is correct I guess it could be a plugin conflict.

    I’ll try to resolve this later.

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Limit total WooCommerce orders without setting a role’ is closed to new replies.