I am setting up a wordpress/woocommerce website and would like users to only purchase a certain number of items just once per 30 days due to the price of the items but in a certain catergory which is 'microwaves', I have found this code on here and it works really really well but for ALL products I really just want ot to work for 'microwaves' catergory but keep all the other functions.
`
// Utility conditional function (Check if user has purchased in the passed week)
function has_week_purshases( $user_id = 0 ){
global $wpdb;
$customer_id = $user_id > 0 ? $user_id : get_current_user_id();
$count = $wpdb->get_var( "
SELECT COUNT(p.ID)
FROM {$wpdb->prefix}posts as p
INNER JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id
WHERE p.post_type LIKE 'shop_order'
AND pm.meta_key LIKE '_customer_user'
AND pm.meta_value = $customer_id
AND UNIX_TIMESTAMP(p.post_date) >= (UNIX_TIMESTAMP(NOW()) - (86400 * 30))
" );
return $count > 0 ? true : false;
}
// Cart and checkout validation
add_action( 'woocommerce_check_cart_items', 'conditionally_allowing_checkout' );
add_action( 'woocommerce_checkout_process', 'conditionally_allowing_checkout' );
function conditionally_allowing_checkout() {
if ( is_user_logged_in() && has_week_purshases() ) {
// Display an error notice when customer is not allowed yet
wc_add_notice( __("You are not allowed yet to make purchases"), 'error' );
}
}
`