We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36571
    • 145 Posts
    Hey all,

    I am looking for a way to get Ditto to rotate to a different product (document) every day. I have already set up the structure, created the TVs and built the chunks, now I just need to be able to auctomatically display a different product on the home page every day.

    Is there any way to do this? Please let me know.

    Thanks.
      • 1122
      • 209 Posts
      Such a rotation is not a big deal -- Ditto snippet provides us with almost all means that are necessary for solving this task.
      [!Ditto? &parents=`x,y,z,...` &display=`1` &start=`offset` &tpl=`product-template` ...and-possibly-some-other-params... !]
      

      I listed crucial parameters for products rotation. "x,y,z" is a folders list where products are stored; &display specifies that we are grabbing exactly one product; &tpl specifies how it should be rendered; and &start actually does the rotation. Of course, we need to arrange start’s changing on a daily basis. Luckily, PHP function date is a perfect opportunity for such arrangement. We can create one-line snippet:
      return date('z', time());
      

      This snippet returns current yearday (0..365). Now you only need to connect this snippet instead of the offset. Done. On Jan 1st application will display first product, on Jan 2nd -- second, etc. If you are not satisfied with this simple rotation, you can develop this suggestion and create something more sophisticated. Enjoy!
        • 36571
        • 145 Posts
        That’s a great solution and it seems to work fine. Thanks!

        Can you think of a way to tweak the custom snippet so that it only rotates through, say, 100 products (as that is all there is) and after that goes back to the start. I guess the best way would be an if statement checking for a maximum number, and then returning a revised value, but I am not sure how to construct this properly.
          • 1122
          • 209 Posts
          The requested limitation to 100 products should look like
          return date('z', time()) % 100;
          

          "%" is a modulus operator that returns the remainder of yearday divided by 100 (0..99) thus properly moving the offset within the scope of your dataset. If you want to adjust starting your rotation from a specific product, then use something like this
          return (date('z', time()) + CONSTANT) % 100;
          

            • 36571
            • 145 Posts
            Awesome! Thank you so much for your help.