Miva Merchant Modules and Development
Want to start an online store? We work with you from start to finish, from commerce platform to design to SEO.
Experience counts, and we have a lot.

WIDGET: Recently Viewed Products

Scot Ranney • December 21, 2023


Easy and clean using custom basket fields. Create a widget with 2 pseudo functions- PROD and GLOBAL FOOTER

PROD Section

This part runs at the bottom of the product PROD page.

<mvt:comment>
#
# RECENTLY VIEWED PRODUCTS: no duplicates, shifts array for each new one.
#
# Set l.number_of_products to however many you want to show.
#
</mvt:comment>

<mvt:assign name="l.number_of_products" value="4" />

<mvt:item name="customfields" param="Read_Basket( 'last_viewed', l.settings:last_viewed_products )" />
<mvt:assign name="l.settings:last_viewed_products" value="miva_array_deserialize(l.settings:last_viewed_products)" />
<mvt:assign name="l.found" value="miva_array_find( g.product_code, l.settings:last_viewed_products, 0)" />

<mvt:if expr="NOT l.found">
	<mvt:if expr="miva_array_elements(l.settings:last_viewed_products) EQ 4">	
		<mvt:assign name="l.ok" value="miva_array_shift( l.settings:last_viewed_products )" />
	</mvt:if>
	<mvt:assign name="l.ok" value="miva_array_insert(l.settings:last_viewed_products,g.product_code,-1)" />
	<mvt:assign name="l.settings:last_viewed_products" value="miva_array_serialize(l.settings:last_viewed_products)" />
	<mvt:item name="customfields" param="Write_Basket( 'last_viewed', l.settings:last_viewed_products )" />
</mvt:if>

Global Footer - recent product display with images

This part runs in the global footer. Use l.settings:page:code conditions to set which pages it is on or not on.

<mvt:comment>
#
# RECENTLY VIEWED PRODUCTS 2023 SCOT using custom basket fields
#
# u-grids-4 below sets it to auto show 4 before a new line of 4.
#
</mvt:comment>

<mvt:if expr="NOT l.settings:in_checkout">

<mvt:item name="customfields" param="Read_Basket( 'last_viewed', l.settings:last_viewed_products )" />
<mvt:assign name="l.settings:last_viewed_products" value="miva_array_deserialize(l.settings:last_viewed_products)" />

<mvt:if expr="miva_array_elements(l.settings:last_viewed_products) GT 0"> 
	 <section>
	 <h4>Recently Viewed Products</h4>

	 <div class="o-layout u-grids-4">

	<mvt:foreach iterator="last_viewed" array="last_viewed_products">
		<mvt:item name="scotstools" param="product_load_code(l.settings:last_viewed)" />
		   <div class="o-layout__item">
				   <a href="&mvt:scotstools_product:url;">
					   <mvt:if expr="NOT ISNULL l.settings:scotstools_product:thumbnail">
					  <img src="&mvt:scotstools_product:thumbnail;">
					   <mvt:else>
					 <img src="../2011_SSL/graphics/en-US/admin/blank.gif">
					   </mvt:if>
					   <h4 class="product-name">&mvt:scotstools_product:name;</h4>
					   <h6 class="product-price">&mvt:scotstools_product:formatted_price;</h6>
					</a>
		   </div>  
	</mvt:foreach>

	 </div>
	 </section>
</mvt:if>

</mvt:if>

https://www.scotsscripts.com/mvblog/widget-recently-viewed-products.html

mvkb_widget mvkb_products