Forums › Maximum Products per User for WooCommerce › Is it possible to limit the amount a user can buy in a given timeframe?
- Support forum for the Maximum Products per User for WooCommerce.
- This topic has 1 reply, 2 voices, and was last updated 4 years, 5 months ago by
Tom Anbinder.
-
AuthorPosts
-
September 4, 2020 at 2:04 pm #100430
Jacob
GuestIs it possible to limit the amount of total products a user can purchase in a certain time frame? I was able to limit a product to max out at 1 purchase (so no one bought duplicate products), but I would like to limit each user to a maximum of 3 products per month. Is it possible with the free version? Is it possible with the paid version?
Thanks!
September 7, 2020 at 6:04 pm #100431Hi Jacob,
Sorry for the late reply.
This can be done with our plugin, however, you will need to: a) add a small PHP snippet to your (child) theme’s functions.php file, and b) assign all your products to some category or tag.
If that’s good enough – here is what you need to do:
1. To limit the total number of products to 3 – as mentioned, you need to assign all your products to some category or tag. After that you need to enable “Per product category” (or “Per product tag”) option in “WooCommerce > Settings > Maximum Products per User > Limits”, and then set “Limit per user” option for that category (or tag) (in “Products > Categories > Your category > Edit”) to “3”. Unfortunately, “Per product category” (and “Per product tag”) options are available in Pro version only.
And, as you want this monthly, you need to set the “Date range” option (in “WooCommerce > Settings > Maximum Products per User > General”) to “This month” or “Last 30 days” (depending if you want it to be counted from the 1st of the current month, or in last 30 days).
2. Now to add an additional “one product only” requirement, you will need to add this snippet:
add_filter( 'alg_wc_mppu_check_quantities_for_product', 'my_alg_wc_mppu_limit_all_products_to_one', 10, 3 ); if ( ! function_exists( 'my_alg_wc_mppu_limit_all_products_to_one' ) ) { /** * Sets max quantity for all products to one. */ function my_alg_wc_mppu_limit_all_products_to_one( $is_valid, $core, $args ) { if ( $is_valid ) { $max_qty_all_products = 1; $bought_data = $core->get_user_already_bought_qty( $args['product_id'], $args['current_user_id'], true ); $user_already_bought = $bought_data['bought']; if ( ( $user_already_bought + $args['cart_item_quantity'] ) > $max_qty_all_products ) { if ( $args['do_add_notices'] ) { $core->output_notice( $args['product_id'], $max_qty_all_products, $bought_data, $args['is_cart'], $args['cart_item_quantity'], 0 ); } return false; } } return $is_valid; } }
Please give it a try and let me know if you have any questions.
-
AuthorPosts
- You must be logged in to reply to this topic.