In this tutorial we’ll learn how to limit number of products site admin can add to WP woocommerce. To achieve this add the following lines of code in your functions.php.
You can access functions.php here:
Add the following lines of code at the end of this file and update file.
add_action( 'admin_head-post-new.php', 'woo_limit_products_creation' ); function woo_limit_products_creation() { global $post_type; global $wpdb; $products_limit = 5; // Change this value if( $post_type === 'product' ) { $products_count = $wpdb->get_var( "SELECT count(*) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'product'" ); if( $products_count >= $products_limit ) { wp_die( sprintf( __("Error. You are not allowed to create more than %s products."), $products_limit) ); } } return; }
You can change the value of product_limit variable with the value you want to set the product limit of your site.
Add code and update file.
I’ve setted product limit to 5 on my site.
I’ve added 5 products on my site.
Now when i try to add sixth product, by clicking on add product i’ll get error. This error will be promted and product couldn’t be added.