Woocommerce function -- 2

Job ID: 35295231

Budget: £10 – £20 GBP

hello this function stops user from adding to the cart and shows error on product page and stops the user from buying from a catergory if they have brought within 30 days:

I would like user to add product to cart and show error notice in cart page and stop user from checkout
also I would like to allow users to buy eg 2 items from the catergory in two orders within 30 days.

Thanks
// Based partially on wc_customer_bought_product(), will return a boolean value based on orders count (false for O orders and true when there is at least one paid order)
function has_bought( $value = 0 ) {
if ( ! is_user_logged_in() && $value === 0 ) {
return false;
}

global $wpdb;

// Based on user ID (registered users)
if ( is_numeric( $value ) ) {
$meta_key = '_customer_user';
$meta_value = $value == 0 ? (int) get_current_user_id() : (int) $value;
}
// Based on billing email (Guest users)
else {
$meta_key = '_billing_email';
$meta_value = sanitize_email( $value );
}

$paid_order_statuses = array_map( 'esc_sql', wc_get_is_paid_statuses() );

$count = $wpdb->get_var( $wpdb->prepare("
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_status IN ( 'wc-" . implode( "','wc-", $paid_order_statuses ) . "' )
AND p.post_type LIKE 'shop_order'
AND pm.meta_key = '%s'
AND pm.meta_value = %s
AND UNIX_TIMESTAMP(p.post_date) >= (UNIX_TIMESTAMP(NOW()) - (86400 * 1))
LIMIT 1
", $meta_key, $meta_value ) );

// Return a boolean value based on orders count
return $count > 0 ? true : false;
}




function filter_woocommerce_add_to_cart_validation( $passed, $product_id, $quantity, $variation_id = null, $variations = null ) {
// Set categories
$categories = array ( 'tv-series' );

// If passed & has category
if ( $passed && has_term( $categories, 'product_cat', $product_id ) ) {
// Initialize
$value = '';

// User logged in
if ( is_user_logged_in() ) {
// Get the current user's ID
$value = get_current_user_id();
} else {
// Get billing_email
$value = WC()->customer->get_billing_email();

// When empty
if ( empty ( $value ) ) {
// Get account email
$value = WC()->customer->get_email();
}
}

// NOT empty
if ( ! empty ( $value ) ) {
if ( has_bought( $value ) ) {
// Display an error message
wc_add_notice( __( 'My custom error message', 'woocommerce' ), 'error' );

// False
$passed = false;
}
}
}

return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation', 'filter_woocommerce_add_to_cart_validation', 10, 5 );
Related categories: PHP JavaScript WordPress HTML WooCommerce