Restricting User Purchase Limit for a Specific Product ID in WooCommerce
If you're looking to sell a set number of products per user, this snippet will allow you to restrict the quantity sales allowed. In this example, the user cannot purchase over 500 products with the id of "123".
If you're looking to a limited number of gift cards or credit, you can use a product that costs $1 and this code snippet to do that. All you need to do is edit the error message.
add_action( 'woocommerce_check_cart_items', 'restrict_user_purchase_limit' );
function restrict_user_purchase_limit() {
$product_id = '123'; // The product ID
$limit = 500; // The limit amount
$user_id = get_current_user_id(); // The user ID
$product_name = get_the_title( $product_id ); // The product name
// Get the product quantity in cart
$product_quantity = 0;
foreach( WC()->cart->get_cart() as $cart_item ){
if( $cart_item['product_id'] == $product_id ){
$product_quantity += $cart_item['quantity'];
}
}
// Get the product quantity in past orders
$past_orders = wc_get_orders( array(
'limit' => -1,
'status' => array_map( 'wc_get_order_status_name', wc_get_is_paid_statuses() ),
'customer' => $user_id,
) );
foreach( $past_orders as $order ){
foreach( $order->get_items() as $item ){
if( $item->get_product_id() == $product_id ){
$product_quantity += $item->get_quantity();
}
}
}
// Display an error notice if the limit is reached
if( $product_quantity > $limit ){
wc_add_notice( sprintf( 'You have already purchased %s %s. You can not purchase more than %s %s.', $product_quantity, $product_name, $limit, $product_name ), 'error' );
}
}
Snippet Explanation
This code restricts the purchase limit for a specific product ID (123) in WooCommerce for each user based on a limit amount (500). The code uses a hook called "woocommerce_check_cart_items" to check the user's past orders and current cart items to determine whether the user has already purchased the product ID in question and has exceeded the limit amount.
The "add_action" function is used to register the "woocommerce_check_cart_items" hook and specify the function that will be called when the hook is triggered ("restrict_user_purchase_limit").
The "restrict_user_purchase_limit" function first retrieves the product ID, limit amount, and user ID using the "get_current_user_id" and "get_the_title" functions.
The function then retrieves the product quantity currently in the user's cart using a foreach loop and the "WC()->cart->get_cart()" method. It also retrieves the product quantity in the user's past orders using a foreach loop and the "wc_get_orders" function.
If the user has exceeded the limit amount for the product, the function displays an error notice using the "wc_add_notice" function.
Overall, this code provides a way for WooCommerce store owners to limit the purchase of a specific product ID for each user based on a set limit amount. This can be useful for managing inventory, controlling costs, and preventing hoarding or reselling of products.