Woocommerce link product visibility to day of the week

Does anyone know if it’s possible to link a products’ visibility in Woocommerce to specific days of the week? i.e. Monday, Products 1-4 visible, Tuesday, Products 5-10 etc, which would enable a restaurant menu to show only daily options that repeat each week?

Any help would be much appreciated, thanks

Related posts

Leave a Reply

1 comment

  1. Code

    1. Copy the WooCommerce template file content-product.php to your theme’s woocommerce directory
    2. Change the area where it checks for visibility

      FROM

      // Ensure visibility
      if ( ! $product || ! $product->is_visible() )
      return;

      TO

      // Ensure visibility
      // starting custom content
      $product_visible = check_for_product_allowed_days( $product );
      if ( ! $product || ! $product->is_visible() || ! $product_visible )
      return;
    3. Add the following to your functions.php file

    function check_for_product_allowed_days ( $product ) {
    
      $product_id  =  $product->id;
      $product_terms  =  get_the_terms ( $product_id, 'product_tag' );
      // remove the strtolower if you capitalized your tag names
      $current_day  =  strtolower ( date ( 'l' ) );
    
      // $all_days value should be the name of the tag
      // that you want to be able to be ordered on all days
      $all_days  =  'all days';
    
      foreach ( $product_terms as $tag ) {
        if ( strtolower ( $tag->name )  ==  $current_day || $tag->name  ==  $all_days ) {
          $product_is_visible  =  true;
          break;
        }
        else {
          $product_is_visible  =  false;
        }
      }
      return $product_is_visible;
    }
    

    WooCommerce Admin Setup

    1. Add tags to all of your products

      • “All Days” or “all days” (just be sure to change the value of $all_days above to whatever you set it to be – monday, tuesday, wednesday

    Thinking

    I couldn’t find a method to break out of WooCommerce’s loop and set the product visibility before hand so a template update was necessary

    Result

    The results aren’t updated to match with this code, but it shows in this example that there is more products than are displayed.

    example of code

    Solution Crossposted