How to set different timeframe for two different category?

Top WooCommerce & WordPress Plugins Forums Maximum Products per User for WooCommerce How to set different timeframe for two different category?

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #100508
    Doug
    Guest

    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?

    #100509
    Tom Anbinder
    Moderator
    Plugin Support

    Hi 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 of 365 * 3 days (i.e. 3 years), and term 124 will have 365 * 4 days (i.e. 4 years).

    Hope that helps. Please give it a try and let me know what you think.

    #100510
    Doug Cox
    Guest

    Tom,

    This is great. Thank you so much for this
    I will test it out..

    Doug

    #100511
    Doug Cox
    Guest

    Hi 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…

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.