Last active
May 30, 2018 12:47
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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; | |
} |
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
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?