Skip to content

Instantly share code, notes, and snippets.

@gabrielmerovingi
Last active May 30, 2018 12:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabrielmerovingi/8974426 to your computer and use it in GitHub Desktop.
Save gabrielmerovingi/8974426 to your computer and use it in GitHub Desktop.
This code snippet will limit the number of times a post can be purchased. Once a post reaches the limit, the content will no longer be set for sale but be visible.
/**
* Disable Content Sales
* Disabled content sales after 3 purchases.
* @version 1.0
*/
add_filter( 'mycred_add', 'mycred_limit_content_sales', 10, 3 );
function mycred_limit_content_sales( $reply, $request, $mycred ) {
// Only apply this to content purchases
if ( $reply === false || $request['ref'] != 'buy_content' ) return $reply;
// The post id
$post_id = absint( $request['ref_id'] );
// The maximum number of times a post can be bought
$max = 3;
// Check the log for number of sales of this post
global $wpdb;
$count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$mycred->log_table} WHERE ref = %s AND ref_id = %d;", 'buy_content', $post_id ) );
// If this is the last purchase, disable sales now
if ( $count+1 >= $max ) {
$prefs = get_post_meta( $post_id, 'myCRED_sell_content', true );
$prefs['status'] = 'disabled';
update_post_meta( $post_id, 'myCRED_sell_content', $prefs );
}
// Always return something
return $reply;
}
@nickmcleanmusiclessons
Copy link

nickmcleanmusiclessons commented Apr 15, 2017

Hello! I am having troubles getting this to work on my website. I have inserted it in my themes functions file and changed the $max=3 to 1, but it does not limit the purchases using points.

Would you be able to tell me if I am doing something wrong?

@tergra
Copy link

tergra commented May 30, 2018

Same here, this code doesn't seem to work anymore. Please update ! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment