Can I set a different date range for different products?

Top WooCommerce & WordPress Plugins Forums Maximum Products per User for WooCommerce Can I set a different date range for different products?

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #100766
    onlinestore_admin
    Participant

    Hi! 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.

    #100767
    Zohaib
    Participant

    Hi,

    Unfortunately, this is not possible. The date range limits in the General plugin settings will apply to all the products.

    #100768
    Pablo
    Moderator
    Plugin Support

    Hi,

    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 😉

    #100769
    onlinestore_admin
    Participant

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

    #100770
    Pablo
    Moderator
    Plugin Support

    Hi,

    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 );
    #100771
    onlinestore_admin
    Participant

    Apologies 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

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