I want to set a limit on the number of orders a customer can send to a specific product per month on WooCommerce. The scenario is: I have a variable product, one feature is free and three features are paid. I want to limit its free feature. So that each user (guest or registered) can not place free orders more than 5 times per month. And when it exceeds the monthly limit, it will show him a message on the checkout page that he can not order the free product and must wait until next month or order the paid product. I found the following code but I think it is incomplete.
add_filter('woocommerce_is_purchasable', 'preventPurchaseIfOrderLimitReached', 10, 2);
function preventPurchaseIfOrderLimitReached($is_purchasable, $product)
{
$OrderLimit = 5;
$productId = $product->get_id();
$quantityOrderedToday = getOrderAmount($productId);
if ($quantityOrdered >= $OrderLimit) {
$is_purchasable = false;
}
return $is_purchasable;
}