steveosoule
7/20/2017 - 6:12 PM

Miva - Recently Viewed Products

Miva - Recently Viewed Products

<mvt:comment>
<!--
	Instructions:
	1. Create a "recently_viewed" ReadyTheme Content Section
	2. Put the following code within that content section.
	3. Add the <mvt:item name="readytheme" param="contentsection( 'recently_viewed' )" /> item to the PROD page where you want them to display.
-->
</mvt:comment>


<mvt:comment>
<!--
	Instructions:
	1. Create a "recently_viewed" ReadyTheme Content Section
	2. Put the following code within that content section.
	3. Add the <mvt:item name="readytheme" param="contentsection( 'recently_viewed' )" /> item to the PROD page where you want them to display.
-->
</mvt:comment>


<mvt:comment>
<!--
	| Initialization & Settings
-->
</mvt:comment>
<mvt:assign name="l.settings:recently_viewed:max_product_count" value="9" />
<mvt:assign name="l.settings:recently_viewed:image:width" value="150" />
<mvt:assign name="l.settings:recently_viewed:image:height" value="150" />
<mvt:do file="g.Module_Library_DB" name="l.result" value="ImageType_Load_Code( 'main', l.settings:recently_viewed:image:type )" />


<mvt:comment>
<!--
	| Load Recently Viewed Items from Basekt Custom Fields
-->
</mvt:comment>
<mvt:item name="customfields" param="Read_Basket('recently_viewed', l.settings:recently_viewed:product_codes)" />
<mvt:assign name="l.settings:recently_viewed:product_codes" value="miva_array_deserialize( l.settings:recently_viewed:product_codes )" />


<mvt:comment>
<!--
	| Check if the current PROD page's `:product:code` exists within the list of recently viewed items.
	| If it does exist, then remove it from the list.
-->
</mvt:comment>
<mvt:assign name="l.settings:recently_viewed:existing_index" value="miva_array_find( l.settings:product:code, l.settings:recently_viewed:product_codes, 0 )" />
<mvt:if expr="l.settings:recently_viewed:existing_index GT 0">
	<mvt:assign name="l.settings:recently_viewed:deleted_existing_index" value="miva_array_delete( l.settings:recently_viewed:product_codes, l.settings:recently_viewed:existing_index, 1 )" />
</mvt:if>


<mvt:comment>
<!--
	| Add the currently viewed product to the front of the list, and remove the last item in the list
-->
</mvt:comment>
<mvt:assign name="l.insert_index" value="miva_array_insert( l.settings:recently_viewed:product_codes, l.settings:product:code, 1 )" />
<mvt:assign name="l.settings:recently_viewed:product_count" value="miva_array_elements( l.settings:recently_viewed:product_codes )" />
<mvt:assign name="l.settings:recently_viewed:product_count_exceded" value="l.settings:recently_viewed:product_count - l.settings:recently_viewed:max_product_count" />
<mvt:if expr="l.settings:recently_viewed:product_count_exceded GT 0">
	<mvt:assign name="l.settings:recently_viewed:_product_codes" value="l.settings:recently_viewed:product_codes" />
	<mvt:assign name="l.settings:recently_viewed:product_codes" value="''" />
	<mvt:assign name="l.settings:recently_viewed:truncated_list" value="miva_array_copy( l.settings:recently_viewed:_product_codes, 1, l.settings:recently_viewed:max_product_count, l.settings:recently_viewed:product_codes, 1)" />
</mvt:if>


<mvt:comment>
<!--
	| Save the new state of recently viewed items ot the Basket Custom Field
-->
</mvt:comment>
<mvt:item name="customfields" param="Write_Basket('recently_viewed', miva_array_serialize(l.settings:recently_viewed:product_codes))" />


<mvt:comment>
<!--
	| Transform the list of product codes into arrays of products with all the product data (:active, :name, :price, :link, :imagetype:main, etc.)
-->
</mvt:comment>
<mvt:foreach iterator="product_code" array="recently_viewed:product_codes">
	<mvt:do file="g.Module_Library_DB" name="l.result" value="Runtime_Product_Load_Code( l.settings:product_code, l.settings:recent_product )" />

	<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_Dimensions( 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>

	<mvt:comment>
		Load other product data (URI, Currency Formatting)
	</mvt:comment>
	<mvt:do file="g.Module_Feature_URI_UT" name="l.settings:recent_product:link" value="Store_Product_URL( l.settings:recent_product, NULL )" />
	<mvt:do file="g.Module_Store_Module_Currency" name="l.settings:recent_product:formatted_price" value="CurrencyModule_AddFormatting( g.Store:currncy_mod, l.settings:recent_product:price )" />

	<mvt:comment>
		Push product data into array
	</mvt:comment>
	<mvt:assign name="l.result" value="miva_array_insert( l.settings:recently_viewed:products, l.settings:recent_product, -1 )" />

</mvt:foreach>
<mvt:do file="g.Module_Feature_TUI_UT" name="l.result" value="CommonComponentFields_Initialize_Product_Discounts_Runtime(l.settings:recently_viewed:products, miva_array_elements(l.settings:recently_viewed:products))" />


<mvt:comment>
<!--
	| Debugging Helper
-->
</mvt:comment>
<mvt:if expr="g.debug">
	<mvt:assign name="g.mvt_debug" value="glosub( miva_array_serialize( l.settings:recently_viewed ), ',', asciichar( 10 ) )" />
	<!--
	@@recently_viewed
	=============================
	&mvt:global:mvt_debug;
	-->
</mvt:if>


<mvt:comment>
<!--
	| If the current product is the only item in the recently_viewed product list, then don't output any recently viewed items.
-->
</mvt:comment>
<mvt:if expr="miva_array_elements( l.settings:recently_viewed:product_codes ) LE 1">
	<mvt:exit/>
</mvt:if>


<mvt:comment>
<!--
	| Output Recently Viewed Items to the Page
-->
</mvt:comment>
<h2 class="carousel-products-heading">Recently Viewed</h2>
<div class="carousel-products carousel-products row">
	<mvt:foreach iterator="recent_product" array="recently_viewed:products">
		<mvt:if expr="l.settings:recent_product:code EQ g.Product_Code">
			<mvt:foreachcontinue/>
		</mvt:if>
		<div class="carousel-product column half medium-one-fourth align-center">
			<a href="&mvt:recent_product:link;">
				<img class="carousel-product-image" src="&mvt:recent_product:src;" alt="&mvt:recent_product:name;">
			</a>
			<div class="carousel-product-price">&mvt:recent_product:formatted_price;</div>
			<div class="carousel-product-name">
				<a href="&mvt:recent_product:link;">
					&mvt:recent_product:name;
				</a>
			</div>
		</div>
	</mvt:foreach>
</div>