Forums › Maximum Products per User for WooCommerce › How to set different timeframe for two different category?
- Support forum for the Maximum Products per User for WooCommerce.
- This topic has 3 replies, 2 voices, and was last updated 4 years, 2 months ago by Doug Cox.
-
AuthorPosts
-
November 13, 2020 at 6:37 pm #100508DougGuest
Hi Tom,
Thanks so much for answering my last question!
I have the pro version and I’d like to know if it’s possible to have two different “timeframes”, possibly using the formulas.
Here is my example….
I have one category that you can buy up to 5 units of product A & B in one year.
But then Product C, you are only allowed 1 unit every 3 years.
Can the plugin do two different “timeframes” ?Thanks so much for your help?
November 13, 2020 at 11:42 pm #100509Hi Doug,
That’s possible, however, you will need to add a small PHP snippet to your site. If that’s good enough, first you need to set “Date range” option (in “WooCommerce > Settings > Maximum Products per User > General”) to “Last 365 days” as you would normally do, and then add this snippet to your (child) theme’s functions.php file:
if ( ! function_exists( 'my_alg_wc_mppu_date_to_check' ) ) { function my_alg_wc_mppu_date_to_check( $date_to_check, $date_range, $current_time, $product_or_term_id, $current_user_id, $is_product ) { // TODO: You need to add your term "timeframe exceptions" here; in `term ID => days` format. $terms = array( 123 => 365 * 3, 124 => 365 * 4, ); return ( ! $is_product && isset( $terms[ $product_or_term_id ] ) ? ( $current_time - $terms[ $product_or_term_id ] * DAY_IN_SECONDS ) : $date_to_check ); } add_filter( 'alg_wc_mppu_date_to_check', 'my_alg_wc_mppu_date_to_check', 10, 6 ); }
* You only need to change the code in the snippet where it says “TODO: You need to add your term …”.
So in this example term with ID
123
will have its own timeframe of365 * 3
days (i.e. 3 years), and term124
will have365 * 4
days (i.e. 4 years).Hope that helps. Please give it a try and let me know what you think.
November 16, 2020 at 8:02 pm #100510Doug CoxGuestTom,
This is great. Thank you so much for this
I will test it out..Doug
November 16, 2020 at 8:46 pm #100511Doug CoxGuestHi Tom,
So far, so good. Do my settings seem correct to you here?
My settings are set to “Last 365 days” located here… WooCommerce > Settings > Maximum Products per User > General > Date Range)
Then my code for the term_id is
321 => 90, (for this termID = clock is reset every 90 days, or every quarter )
325 => 365 * 5, ( for this termID = clock is reset every 5 years)Also, I see in the code you pasted it says DAY_IN_SECONDS and I just want to confirm that is correct.
return ( ! $is_product && isset( $terms[ $product_or_term_id ] ) ? ( $current_time – $terms[ $product_or_term_id ] * DAY_IN_SECONDS ) : $date_to_check );
}Thanks for your help with this…
-
AuthorPosts
- You must be logged in to reply to this topic.