sniurs
sniurs

Reputation: 155

Limit number of products site admin can add to WP woocommerce

I want to limit the number of products site admin can add to their shop. I am using wordpress multisites + woocommerce.

Any help would be appreciated, thanks!

Upvotes: 3

Views: 1620

Answers (1)

Vaibs_Cool
Vaibs_Cool

Reputation: 6160

Add this in functions.php

add_action( 'admin_head-post-new.php', 'woo_limit_products_creation' );

function woo_limit_products_creation() {

    global $post_type;
    global $wpdb;

    $products_limit = 50; // 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;
}

Upvotes: 4

Related Questions