Forums › Maximum Products per User for WooCommerce › Can I set a different date range for different products?
- Support forum for the Maximum Products per User for WooCommerce.
- This topic has 5 replies, 3 voices, and was last updated 3 years, 3 months ago by
onlinestore_admin.
-
AuthorPosts
-
October 30, 2021 at 1:46 am #100766
onlinestore_admin
ParticipantHi! is there an available individual product setting to set the date range per product? If there is no individual setting, could you please help me with a shortcode? This is the setup:
product_A = maximum qty limit of 1, lifetime range
product_B = maximum qty limit of 1, within 7 days
product_C = maximum qty limit of 1 withing 1 day
Looking forward to your response. Thank you.
November 1, 2021 at 10:18 am #100767Zohaib
ParticipantHi,
Unfortunately, this is not possible. The date range limits in the General plugin settings will apply to all the products.
November 2, 2021 at 12:00 am #100768Hi,
Although it’s not possible to set a date range per product using the admin settings or by using shortcodes, I believe it would be possible via code using the
alg_wc_mppu_date_to_check
filter.If you only need to set limits for these specific products, you can simply enable “Limits > Per Product” option and then access the admin product page from A, B and C, setting the “Limit per user” option as 1.
In your (child) theme’s
functions.php
file you can add this code, considering products A, B and C with ids 1, 2 and 3:add_filter( 'alg_wc_mppu_date_to_check', function ( $date_to_check, $date_range, $current_time, $product_or_term_id, $current_user_id, $is_product ) { if ( ! $is_product ) { return $date_to_check; } switch ( (int) $product_or_term_id ) { case 1: // Product A - Lifetime. $date_to_check = 0; break; case 2: // Product B - Last 7 days. $date_to_check = ( $current_time - WEEK_IN_SECONDS ); break; case 3: // Product C - Last 24 hours. $date_to_check = ( $current_time - DAY_IN_SECONDS ); break; } return $date_to_check; }, 10, 6 );
Let me know if it helps 😉
November 7, 2021 at 11:33 pm #100769onlinestore_admin
ParticipantHi! I haven’t tested this but some more questions..
In your example:
switch ( (int) $product_or_term_id ) { case 1: // Product A - Lifetime. $date_to_check = 0; break; case 2: // Product B - Last 7 days. $date_to_check = ( $current_time - WEEK_IN_SECONDS ); break; case 3: // Product C - Last 24 hours. $date_to_check = ( $current_time - DAY_IN_SECONDS ); break; }
– How do I place the product ID instead of the product name? Example Product ID#123
– What about multiple product ID’s? example: id#123, id#234, id#345
– What is the code to check for 15days and 30days?
Thank you!
November 9, 2021 at 1:43 am #100770Hi,
The product name was just a comment. The number I used after the word “case” was actually the product ID. Product A, B and C with IDs 1, 2 and 3.
Please check this other code for your other questions:
add_filter( 'alg_wc_mppu_date_to_check', function ( $date_to_check, $date_range, $current_time, $product_or_term_id, $current_user_id, $is_product ) { if ( ! $is_product ) { return $date_to_check; } if ( in_array( (int) $product_or_term_id, array( 1, 2, 3 ) ) ) { // Product IDs 1, 2 and 3 - 15 days $date_to_check = ( $current_time - ( DAY_IN_SECONDS * 15 ) ); } elseif ( in_array( (int) $product_or_term_id, array( 4, 5, 6 ) ) ) { // Product IDs 4, 5 and 6 - 30 days $date_to_check = ( $current_time - MONTH_IN_SECONDS ); } return $date_to_check; }, 10, 6 );
November 9, 2021 at 7:18 am #100771onlinestore_admin
ParticipantApologies for the duplicate post, the browser was adding a new post instead of replying here…
Another question, since I’m running a multisite and each site has a different requirement or product restrictions, I am using a code snippet plugin instead of adding it in the functions.php file.
I am getting an error with the <pre></pre>tag. Is this required to be in the code or can I just remove it? I tried the code without this and it seems to be working fine. Thanks again -
AuthorPosts
- You must be logged in to reply to this topic.