Announcement

Collapse
No announcement yet.

Randomize 'Featured Products' on SFNT

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Randomize 'Featured Products' on SFNT

    We have a modified version of Levels so I'm not 100% sure if this is 'stock' or not. In any case the SFNT has a 'Featured Products' slider on the bottom of the page, that pulls products from the 'featured' category. I manually switch these around (when I remember) to keep it fresh. Right now it reads the products into an array and outputs using a default sort method. I was wondering if there is an easy-to-implement way to 'jumble' the array index, or otherwise randomize (or pseudo-randomize) the output products so the page slider would be 'fresh' without my manual intervention.

    #2
    To randomize the products, add this code above the FOREACH for the product list:
    Code:
    <mvt:comment>
        This section will randomize the products generated by the ReadyTheme Product Listing being used.
    </mvt:comment>
    <mvt:foreach iterator="product" array="readytheme:products">
        <mvt:assign name="g.random_item" value="random(miva_array_elements(l.settings:readytheme:products) - 1) + 1"/>
        <mvt:assign name="g.random_product" value="l.settings:product"/>
        <mvt:assign name="l.settings:product" value="l.settings:readytheme:products[g.random_item]"/>
        <mvt:assign name="g.item_counter" value="1"/>
        <mvt:foreach iterator="new_product" array="readytheme:products">
            <mvt:if expr="g.item_counter EQ g.random_item">
                <mvt:assign name="l.settings:new_product" value="g.random_product"/>
                <mvt:foreachstop/>
            </mvt:if>
            <mvt:assign name="g.item_counter" value="g.item_counter + 1"/>
        </mvt:foreach>
    </mvt:foreach>
    Matt Zimmermann

    Miva Web Developer
    Alchemy Web Development
    https://www.alchemywebdev.com
    Site Development - Maintenance - Consultation

    Miva Certified Developer
    Miva Professional Developer

    https://www.dev4web.net | Twitter

    Comment


      #3
      Thanks Matt. Works like a charm. You should add it to 'useful snippets' for the theme.

      Comment


        #4
        Love this script - thanks Matt! Quick question - Is there a way to have it randomly grab random products and entirely switch out products as opposed to jumbling a set amount of images?

        Comment


          #5
          danceman If you are looking to change the amount of products, you can chance the display quantity in the ReadyTheme component. If you are looking to pull in random product, like the old Random function within Tool Kit, I'm not sure of a way to do that within this product list.
          Matt Zimmermann

          Miva Web Developer
          Alchemy Web Development
          https://www.alchemywebdev.com
          Site Development - Maintenance - Consultation

          Miva Certified Developer
          Miva Professional Developer

          https://www.dev4web.net | Twitter

          Comment


            #6
            Ok, thanks - yes, was thinking Random function.

            Comment


              #7
              Thank you! I've been needing this for a while. It is much appreciated!
              Leslie Kirk
              Miva Certified Developer
              Miva Merchant Specialist since 1997
              Previously of Webs Your Way
              (aka Leslie Nord leslienord)

              Email me: [email protected]
              www.lesliekirk.com

              Follow me: Twitter | Facebook | FourSquare | Pinterest | Flickr

              Comment


                #8
                Originally posted by Matt Zimmermann View Post
                To randomize the products, add this code above the FOREACH for the product list:
                Code:
                <mvt:comment>
                This section will randomize the products generated by the ReadyTheme Product Listing being used.
                </mvt:comment>
                <mvt:foreach iterator="product" array="readytheme:products">
                <mvt:assign name="g.random_item" value="random(miva_array_elements(l.settings:readytheme:products) - 1) + 1"/>
                <mvt:assign name="g.random_product" value="l.settings:product"/>
                <mvt:assign name="l.settings:product" value="l.settings:readytheme:products[g.random_item]"/>
                <mvt:assign name="g.item_counter" value="1"/>
                <mvt:foreach iterator="new_product" array="readytheme:products">
                <mvt:if expr="g.item_counter EQ g.random_item">
                <mvt:assign name="l.settings:new_product" value="g.random_product"/>
                <mvt:foreachstop/>
                </mvt:if>
                <mvt:assign name="g.item_counter" value="g.item_counter + 1"/>
                </mvt:foreach>
                </mvt:foreach>
                I've placed this code in a Product Listing Theme Component. I have the Number of Products set to 4 and the Products To Display to All Products. It is only randomizing the same 4 products - How can I get it to randomize all the products?


                Leslie Kirk
                Miva Certified Developer
                Miva Merchant Specialist since 1997
                Previously of Webs Your Way
                (aka Leslie Nord leslienord)

                Email me: [email protected]
                www.lesliekirk.com

                Follow me: Twitter | Facebook | FourSquare | Pinterest | Flickr

                Comment


                  #9
                  Looking here https://docs.miva.com/reference-guid...oduct-listings it seems as if Number of Products grabs the number you set then this script randomizes those. Try a higher number like 20 and you should see the randomizing at a guess. My not a real programmer concern is if you have 3000 products in 'all products' and it builds an array which it is going to sort and pick out 4 random every page load that might make the page chug.

                  In my case I created a category with 'featured products' which I update now and then. It pulls from those 20 to 30 products and randomizes those - in our case I'm trying to highlight some products which people tend to overlook.

                  Comment


                    #10
                    Originally posted by habreu View Post
                    Looking here https://docs.miva.com/reference-guid...oduct-listings it seems as if Number of Products grabs the number you set then this script randomizes those. Try a higher number like 20 and you should see the randomizing at a guess. My not a real programmer concern is if you have 3000 products in 'all products' and it builds an array which it is going to sort and pick out 4 random every page load that might make the page chug.

                    In my case I created a category with 'featured products' which I update now and then. It pulls from those 20 to 30 products and randomizes those - in our case I'm trying to highlight some products which people tend to overlook.
                    Ahhh, I understand upping the number of products, since it still seems to be displaying the same 4 products since it's set to display 4. But I need to display only 4 products, if I up the number of products, it displays that amount and then randomizes just those.
                    Leslie Kirk
                    Miva Certified Developer
                    Miva Merchant Specialist since 1997
                    Previously of Webs Your Way
                    (aka Leslie Nord leslienord)

                    Email me: [email protected]
                    www.lesliekirk.com

                    Follow me: Twitter | Facebook | FourSquare | Pinterest | Flickr

                    Comment

                    Working...
                    X