Woocommerce product PostType & Pro Site Quota

Hello guys,

This is just a code snippet for pro site and woocommerce. Sometime back, i saw someone asked a question about whether prosites was able to limit post quota for Woo product post_type based on the blog level. The answer to the question was yes, however even in the current version, in other to use “product” as the “post type”, in Post/Page Quotas woocommerce would HAVE TO BE activated in the main site. with this snippet there’s no need to have woocommerce install in the main.

NB.

No need to enable “Post/Page Quotas” unless you’re limiting other post type

No need to have woocommerce install in the main site.

This code can be used basically for any post type

so, here’s the code. just edit the number 100 and assigned which level you want it to apply to.

<?php
/*
* Plugin Name: (Prosite) Limit Postype
* Description: This plugin require prosite in other to work. Edit it to suite your need.
* Author: Thomas Whittaker
* Author URI: https://www.linkedin.com/in/thomaswhittaker
* Network: true
*/

add_action( 'post_submitbox_misc_actions', 'upgrade_button' );

function upgrade_button (){
//prosite & WP global variables
global $psts, $current_screen, $level, $blog_id;
//assigned the current blog id to $level variable
$level = $psts->get_level($blog_id);
//that's where you limit the number of post/items
$limit ="100";
//thats where you defined the posttype, in this case. postype is product.
$post_type ="product";
/* here, we get the current screen that the user is viewing, then the postype which we have assigned "product" to,
then we tell it, to make sure that the user is able to manage_option. by default administrator manage_option is assigned to administrator
Please view WP codex :https://codex.wordpress.org/Roles_and_Capabilities
*/
if ( in_array( $current_screen->id, array( $post_type, 'edit-' . $post_type ) ) && current_user_can('manage_options') ) {
/*
This is very important: this where you check the blog level and the total # of product
that this blog level can add before you tell them they need to upgrade.
in this example, we check for blog with LEVEL 1 and We tell that blog you can only have 100 products.
*/
if ($level=="1" && is_numeric($limit ) && wp_count_posts( $post_type )->publish >= $limit ) {
?>
<style>
#publishing-action { display: none; }
#publish-action { display: none;}
#psts-upgrade { margin-top: 4px;display: block;text-align: center; }
</style>
<div class="misc-pub-section">
<a id="psts-upgrade" class="button button-primary button-large" target="_blank"
href="<?php echo $psts->checkout_url( get_current_blog_id() ); ?>"><?php _e( 'Upgrade Your Account', 'psts' ); ?></a>
</div>
<?php
}
}
}
add_action( 'admin_notices', 'upgrade_message');
function upgrade_message(){
global $psts, $current_screen, $post_type, $blog_id;
$post_type ="product";
//Get quota settings
$quota_settings = $psts->get_setting( "pq_quotas" );
//get pro site blog level
$level = $psts->get_level($blog_id);
//if limit not set for post type
if ( ! empty( $post_type ) && ! isset( $quota_settings[ $post_type ] ) ) {
return;
}
//again we limit the number of products
$limit = "100";
/*
we check if blog level is 1, and count the number of active/published product
and we compare it with the number that is assigned to variable $limit
*/
if ($level=="1" && is_numeric($limit ) && wp_count_posts( $post_type )->publish >= $limit ) {
//if all conditions are met then we show a notice in the admin area that they have reached their limitation. bye bye
echo "<div class="error"><p><a target='_blank' href=".$psts->checkout_url( get_current_blog_id() ).">You've reached your PRODUCT publishing limit, To publish more product, please upgrade to a higher plan.</a></p></div>";

}
//we check if blog level is 1 and the current login user can manage options
elseif($level=="1" && current_user_can('manage_options')){
//we set to variables to compare the total of published/activate product
$published_post = wp_count_posts( $post_type )->publish;
//we asigned to total limit of product a user can have to variable $allowed_post
$allowed_post = $limit;
//then we tell the user how many products they have left.
echo '<div class="error"><p>You have <b>'. ( $allowed_post - $published_post ) .'</b> ' . $post_type ."'s". ' left.</p></div>';
}

}

  • Tom
    • Design Lord, Child of Thor

    This awesome, i was going to open a ticket about it but i said let me just scroll to the tickets!!!!!! am new here but that is very help full. i don’t know if its possible for regular user to give point but i will sure give u some point. this is dope. :slight_smile:

    Thank you

  • Christoffer
    • Site Builder, Child of Zeus

    Hello Thomas, hope you are having a good day, and thank you for sharing.

    Could this be written that quotas could be set by level?

    For example:

    Level 1: 10 products

    Level 2: 100 products

    Level 3: 500 products

    and so on

    Have a nice day, and thanks :relaxed:

  • Ivan Shulev
    • The Bug Hunter

    Hey Christoffer ,

    I hope you are having a nice day so far!

    Here is the modified code with included multiple levels:

    <?php
    /*
    * Plugin Name: (Prosite) Limit Postype
    * Description: This plugin require prosite in other to work. Edit it to suite your need.
    * Author: Thomas Whittaker
    * Author URI: https://www.linkedin.com/in/thomaswhittaker
    * Network: true
    */

    add_action( 'post_submitbox_misc_actions', 'upgrade_button' );

    function upgrade_button (){
    //prosite & WP global variables
    global $psts, $current_screen, $level, $blog_id;
    //assigned the current blog id to $level variable
    $level = $psts->get_level($blog_id);
    //that's where you limit the number of post/items
    $limit_1 ="10";
    $limit_2 ="100";
    $limit_3 ="500";
    //thats where you defined the posttype, in this case. postype is product.
    $post_type ="product";
    /* here, we get the current screen that the user is viewing, then the postype which we have assigned "product" to,
    then we tell it, to make sure that the user is able to manage_option. by default administrator manage_option is assigned to administrator
    Please view WP codex :https://codex.wordpress.org/Roles_and_Capabilities
    */
    if ( in_array( $current_screen->id, array( $post_type, 'edit-' . $post_type ) ) && current_user_can('manage_options') ) {
    /*
    This is very important: this where you check the blog level and the total # of product
    that this blog level can add before you tell them they need to upgrade.
    in this example, we check for blog with LEVEL 1 and We tell that blog you can only have 100 products.
    */
    if ($level=="1" && is_numeric($limit_1 ) && wp_count_posts( $post_type )->publish >= $limit_1 || $level=="2" && is_numeric($limit_2 ) && wp_count_posts( $post_type )->publish >= $limit_2 || $level=="3" && is_numeric($limit_3 ) && wp_count_posts( $post_type )->publish >= $limit_3 ) {
    ?>
    <style>
    #publishing-action { display: none; }
    #publish-action { display: none;}
    #psts-upgrade { margin-top: 4px;display: block;text-align: center; }
    </style>
    <div class="misc-pub-section">
    <a id="psts-upgrade" class="button button-primary button-large" target="_blank"
    href="<?php echo $psts->checkout_url( get_current_blog_id() ); ?>"><?php _e( 'Upgrade Your Account', 'psts' ); ?></a>
    </div>
    <?php
    }
    }
    }
    add_action( 'admin_notices', 'upgrade_message');
    function upgrade_message(){
    global $psts, $current_screen, $post_type, $blog_id;
    $post_type ="product";
    //Get quota settings
    $quota_settings = $psts->get_setting( "pq_quotas" );
    //get pro site blog level
    $level = $psts->get_level($blog_id);
    //if limit not set for post type
    if ( ! empty( $post_type ) && ! isset( $quota_settings[ $post_type ] ) ) {
    return;
    }
    //again we limit the number of products
    $limit_1 ="10";
    $limit_2 ="100";
    $limit_3 ="500";
    /*
    we check if blog level is 1, and count the number of active/published product
    and we compare it with the number that is assigned to variable $limit
    */
    if ($level=="1" && is_numeric($limit_1 ) && wp_count_posts( $post_type )->publish >= $limit_1 || $level=="2" && is_numeric($limit_2 ) && wp_count_posts( $post_type )->publish >= $limit_2 || $level=="3" && is_numeric($limit_3 ) && wp_count_posts( $post_type )->publish >= $limit_3) {
    //if all conditions are met then we show a notice in the admin area that they have reached their limitation. bye bye
    echo "<div class="error"><p><a target='_blank' href=".$psts->checkout_url( get_current_blog_id() ).">You've reached your PRODUCT publishing limit, To publish more product, please upgrade to a higher plan.</a></p></div>";

    }
    //we check if blog level is 1 and the current login user can manage options
    elseif($level=="1" && current_user_can('manage_options')){
    //we set to variables to compare the total of published/activate product
    $published_post = wp_count_posts( $post_type )->publish;
    //we asigned to total limit of product a user can have to variable $allowed_post
    $allowed_post = $limit_1;
    //then we tell the user how many products they have left.
    echo '<div class="error"><p>You have <b>'. ( $allowed_post - $published_post ) .'</b> ' . $post_type ."'s". ' left.</p></div>';
    }
    //we check if blog level is 1 and the current login user can manage options
    elseif($level=="2" && current_user_can('manage_options')){
    //we set to variables to compare the total of published/activate product
    $published_post = wp_count_posts( $post_type )->publish;
    //we asigned to total limit of product a user can have to variable $allowed_post
    $allowed_post = $limit_2;
    //then we tell the user how many products they have left.
    echo '<div class="error"><p>You have <b>'. ( $allowed_post - $published_post ) .'</b> ' . $post_type ."'s". ' left.</p></div>';
    }
    //we check if blog level is 1 and the current login user can manage options
    elseif($level=="3" && current_user_can('manage_options')){
    //we set to variables to compare the total of published/activate product
    $published_post = wp_count_posts( $post_type )->publish;
    //we asigned to total limit of product a user can have to variable $allowed_post
    $allowed_post = $limit_3;
    //then we tell the user how many products they have left.
    echo '<div class="error"><p>You have <b>'. ( $allowed_post - $published_post ) .'</b> ' . $post_type ."'s". ' left.</p></div>';
    }

    }

    I hope this helps and I wish you an awesome day ahead!

    Ivan

  • Christoffer
    • Site Builder, Child of Zeus

    Thank you Ivan! :thumbsup:

    Working like a charm, this is not though a final fix, as the users can still bypass the quota by editing the css :slight_smile:

    For those who also want to limit other posts, just change the posttype in the code.

    I think this should be included to the pro-sites plugin as well, with the same design as upload quotas.

  • Ivan Shulev
    • The Bug Hunter

    Hey Christoffer ,

    It is true that it can be bypassed by changing the CSS.

    However, this topic was started by another member providing a simple solution to a problem and my previous post is building on that.

    If you want it working properly, there needs to be some back-end checks and if you really want that, I would suggest posting the job here -> https://premium.wpmudev.org/jobs/new-job/

    In the meantime, I will move this topic to Features & Feedback, so if other members are interested in this it might be included in Pro sites.

    Thank you and I wish you an awesome day ahead!

    Ivan

  • Ash
    • WordPress Hacker

    You can use it as a plugin. Copy the code, paste it in a file. Upload the file in /wp-content/plugins/CREATE_A_FOLDER/ and then activate from Dashboard > Plugins > All Plugins.

    You can also use this code in your theme’s functions.php if the theme is not changed. Otherwise mu-plugin is another option. To create a mu-plugin, go to wp-content/mu-plugins folder. If there is no mu-plugins folder then, create one. Now, inside the mu-plugins folder create file with name anything.php (make sure file extension is .php). Now start with a <?php tag and then put the above code.

    Hope it helps! Please feel free to ask more questions if you have any.

    Have a nice day!

    Cheers,

    Ash

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.