Created
July 27, 2018 11:36
-
-
Save mycred/206bc64e46a381a96a53a45329f0c23c 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 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment