Announcement

Collapse
No announcement yet.

Recently Viewed Products a cookie based solution.

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

    Recently Viewed Products a cookie based solution.

    Thought I'd share a solution I found for this. I couldn't find a better way of doing it. Requires the toolkit module

    It's now implemented on our site facewest.co.uk. Currently only shows recently viewed products on the homepage, although they can easily go on any page.

    It uses a cookie to store the value of the last X number of products viewed. So works when a customer comes back a few days later.

    This code needs to go on each PROD page in the Head Tag:


    <mvt:comment> Read the Cookie "RecentlyViewed" and Set the variable "RecentlyViewed" to be the value of that cookie
    </mvt:comment>

    <mvt:item name="toolkit" param="vacreate|cookies|s.http_cookie|;" />
    <mvt:foreach iterator="cookie" array="cookies">
    <mvt:item name="toolkit" param="gettoken|l.all_settings:cookie,=,1|name" />
    <mvt:item name="toolkit" param="gettoken|l.all_settings:cookie,=,2|value" />
    <mvt:if expr="g.name EQ 'RecentlyViewed'">
    <mvt:item name="toolkit" param="vassign|RecentlyViewed|value" />
    </mvt:if>
    </mvt:foreach>


    <mvt:comment> If the current product code is not part of that list than add it to the front and a comma. And outputs variable RecentlyViewedNew</mvt:comment>

    <mvt:item name="toolkit" param="sassign|comma|," />

    <mvt:if expr="l.settings:product:code CIN RecentlyViewed">
    <mvt:item name="toolkit" param="vassign|RecentlyViewedNew|RecentlyViewed" />
    <mvt:else>
    <mvt:item name="toolkit" param="vassign|RecentlyViewedNew|l.all_settings:pr oduct:code" />
    <mvt:item name="toolkit" param="concat|RecentlyViewedNew|RecentlyViewedNew| comma" />
    <mvt:item name="toolkit" param="concat|RecentlyViewedNew|RecentlyViewedNew| RecentlyViewed" />
    </mvt:if>



    <mvt:comment> Now Trim String to make it contain just the last 4 items viewed</mvt:comment>
    <mvt:item name="toolkit" param="sassign|RecentlyViewedTrimmed|" />
    <mvt:item name="toolkit" param="vquick|pcount|RecentlyViewedNew" />
    <mvt:item name="toolkit" param="sassign|counter|1" />
    <mvt:item name="toolkit" param="sassign|one|1" />
    <mvt:foreach iterator="quicklist" array="quicklists">
    <mvt:if expr="Counter LE '4'">
    <mvt:item name="toolkit" param="math_add|counter|counter|one" />
    <mvt:item name="toolkit" param="concat|RecentlyViewedTrimmed|RecentlyViewed Trimmed|l.all_settings:quicklist:code" />
    <mvt:item name="toolkit" param="concat|RecentlyViewedTrimmed|RecentlyViewed Trimmed|comma" />
    <mvt:else>
    </mvt:if>

    </mvt:foreach>

    <mvt:comment> Now set cookie to make it contain just the last 4 items viewed</mvt:comment>
    <meta http-equiv="set-cookie" content="RecentlyViewed=&mvte:global:RecentlyViewe dTrimmed;; expires=Sat, 25-Nov-2023 12:00:00 GMT;">



    Then on any page you want to show the recently viewed product you use:



    <mvt:comment> Shows Recently Viewed Products </mvt:comment>


    <mvt:item name="toolkit" param="vacreate|cookies|s.http_cookie|;" />
    <mvt:foreach iterator="cookie" array="cookies">
    <mvt:item name="toolkit" param="gettoken|l.all_settings:cookie,=,1|name" />
    <mvt:item name="toolkit" param="gettoken|l.all_settings:cookie,=,2|value" />
    <mvt:if expr="g.name EQ 'RecentlyViewed'">
    <mvt:item name="toolkit" param="vassign|RecentlyViewed|value" />
    </mvt:if>
    </mvt:foreach>

    <mvt:if expr="g.RecentlyViewed">
    <mvt:item name="toolkit" param="vquick|pcount|RecentlyViewed" />
    <mvt:foreach iterator="quicklist" array="quicklists">
    Use whatever code here to display your products as per your layouts, this is documented in the toolkit Quick / VQuick function.
    </mvt:foreach>
    </mvt:if>


    Hope that helps someone else.

    If any one can see why that's a rubbish solution and there is a better way then please let me know.

    Jake




    Facewest- UK Adventure sports Equipment
    www.facewest.co.uk

    #2
    You don't need toolkit. You can write cookies directly from SMT code with the following:
    Code:
    <mvt:assign name="g.expires" value="s.time_t + (60 * 60 * 24 * 5)" /> // 5 days from today
    <mvt:assign name="g.cookie_name" value="'mycookie'" />
    <mvt:assign name="g.cookie_value" value="'hello world'" />
    <mvt:do file="g.module_library_utilities" name="g.null" value="SetCookie(g.Output_Cookies, g.cookie_name, g.cookie_value , g.cookiedomain , g.expires, g.cookiepath, 0 )" />
    <mvt:do file="g.module_library_utilities" name="g.null" value="OutputCookies( g.Output_Cookies )" />
    You can read the cookie as a global variable so long has it hasn't expired

    g.request_cookies:cookie_name
    Bruce Golub
    Phosphor Media - "Your Success is our Business"

    Improve Your Customer Service | Get MORE Customers | Edit CSS/Javascript/HTML Easily | Make Your Site Faster | Get Indexed by Google | Free Modules | Follow Us on Facebook
    phosphormedia.com

    Comment


      #3
      Originally posted by Bruce - PhosphorMedia View Post
      You can read the cookie as a global variable so long has it hasn't expired

      g.request_cookies:cookie_name
      Would a simple
      Code:
      &mvt:g.request_cookies:cookie_name
      output to the page? Or is there another step?

      Comment


        #4
        &mvt:global:request_cookies:cookie_name;

        but you'll want to work with the values within 'g.request_cookies:cookie_name'
        Bruce Golub
        Phosphor Media - "Your Success is our Business"

        Improve Your Customer Service | Get MORE Customers | Edit CSS/Javascript/HTML Easily | Make Your Site Faster | Get Indexed by Google | Free Modules | Follow Us on Facebook
        phosphormedia.com

        Comment


          #5
          I must be doing something else wrong.

          I have the following in the PROD "Product Display Layout"
          Code:
          <mvt:assign name="g.cookie_name" value="'mycookie'" />
          <mvt:assign name="g.cookie_value" value="'cookie time'" />
          <mvt:do file="g.module_library_utilities" name="g.null" value="SetCookie(g.Output_Cookies, g.cookie_name, g.cookie_value , g.cookiedomain , g.expires, g.cookiepath, 0 )" />
          <mvt:do file="g.module_library_utilities" name="g.null" value="OutputCookies( g.Output_Cookies )" />
          I want to set the cookie on the product page, so when it gets added to the BASK or Checkout page I can check if the category matches a defined set and then apply a coupon code.

          But I am not seeing the cookie value in the BASK page.

          Comment


            #6
            That's not the code you where shown before...you are missing "expires". You'll want to use something like "g.product_code" as the value (appended to the current list of products). So, you'll want to read the cookie first and then do something like:

            <mvt:if expr="NOT(g.product_code$',' IN g.request_cookies:my_cookie)"> (looks to see if the code is already there)
            <mvt:assign name="g.request_cookies:my_cookie" value="g.request_cookies:my_cookie $ ',' $g.product_code" /> (adds the product)
            </mvt:if>

            THEN use g.request_cookies:my_cookie as the g.cookie_value.

            (That being said, a more classical coding style would be to assign g.request_cookies:my_cookie to g.visitedProducts or something.)
            Bruce Golub
            Phosphor Media - "Your Success is our Business"

            Improve Your Customer Service | Get MORE Customers | Edit CSS/Javascript/HTML Easily | Make Your Site Faster | Get Indexed by Google | Free Modules | Follow Us on Facebook
            phosphormedia.com

            Comment


              #7
              Originally posted by Bruce - PhosphorMedia View Post
              That's not the code you where shown before...
              My question is a follow on to the original post. I bumped the thread because I it looked like you all were solving a similar problem.

              I'm still unpacking the code in your reply. I'll post back in a bit.

              Comment


                #8
                A little unrelated, but thought I just throw it out there. Miva has a "recently viewed products" solution on GitHub that's easy to install / edit. It can be found here:

                https://gist.github.com/steveosoule/...f614b9b2927741

                https://gist.githubusercontent.com/s...d-products.xml - raw

                Comment


                  #9
                  Ryan, nice spot. Only issue is that its not cookie based...but that'd be easy enough to add. We have something similar (but mivascript/module based code) that we've been meaning to convert to SMT...but this should get most developer types a head start.
                  Bruce Golub
                  Phosphor Media - "Your Success is our Business"

                  Improve Your Customer Service | Get MORE Customers | Edit CSS/Javascript/HTML Easily | Make Your Site Faster | Get Indexed by Google | Free Modules | Follow Us on Facebook
                  phosphormedia.com

                  Comment


                    #10
                    Originally posted by RTHOMASDESIGN View Post
                    A little unrelated, but thought I just throw it out there. Miva has a "recently viewed products" solution on GitHub that's easy to install / edit. It can be found here:

                    https://gist.github.com/steveosoule/...f614b9b2927741

                    https://gist.githubusercontent.com/s...d-products.xml - raw
                    I decided to start with this solution - relatively easy to implement (thanks SteveoSoule ) but I've come up upon my first gotcha. It looks like (to me) the code is "assuming" the product image type is main. How do I tell it otherwise?


                    Code:
                    <mvt:comment> Load Resized `main` Image </mvt:comment>
                    <mvt:do file="g.Module_Library_DB" name="l.result" value="ProductImage_Load_Type(l.settings:recent_product:id, l.settings:recently_viewed:image:type:id, l.settings:recent_product:imagetypes_data:type)" /> <mvt:do file="g.Module_Library_DB" name="l.result" value="Image_Load_ID( l.settings:recent_product:imagetypes_data:type:image_id, l.settings:recent_product:imagetypes_data:image )" />
                    <mvt:do file="g.Module_Library_DB" name="l.result" value="GeneratedImage_FindOrInsert_Image_Dimension s( l.settings:recent_product:imagetypes_data:image, l.settings:recently_viewed:image:width, l.settings:recently_viewed:image:height, l.settings:recent_product:imagetypes_data:generated )" />
                    <mvt:if expr="l.settings:recent_product:imagetypes_data:generated:id">
                    <mvt:assign name="l.settings:recent_product:src" value="l.settings:recent_product:imagetypes_data:generated:image" />
                    <mvt:else> <mvt:assign name="l.settings:recent_product:src" value="g.theme_path $ '/images/img_no_thumb.jpg'" />
                    </mvt:if>
                    (FYI the copy & paste might make the code appear broken but it's not)


                    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


                      #11
                      Hey Leslie,
                      Looks like it's referenced near the top: <mvt:do file="g.Module_Library_DB" name="l.result" value="ImageType_Load_Code( 'main', l.settings:recently_viewed:image:type )" />

                      Comment


                        #12
                        Thanks Ryan. Have you been able to implement this with the "cookie" code?
                        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


                          #13
                          Personally I haven't needed to... I think Steve's code works right off the bat well enough for most stores -- at least if you just need "recently viewed products" to display. I have his code installed on a few sites. You can see it in action if you bounce around a few products here: https://www.tacer.biz/chargers.html.

                          Comment


                            #14
                            Originally posted by RTHOMASDESIGN View Post
                            Personally I haven't needed to... I think Steve's code works right off the bat well enough for most stores -- at least if you just need "recently viewed products" to display. I have his code installed on a few sites. You can see it in action if you bounce around a few products here: https://www.tacer.biz/chargers.html.
                            Very nice, Ryan, very nice.
                            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