MivaLearn
6/22/2023 - 2:37 PM

Load Price Range for Product Options

<mvt:comment>
	------------------------------------------------
	Variant Price-Range Generator
	Code: TEMPLATEFEED_VPRG
	Iterator Template

	Instructions:
		1. Create a new Marketing > Feed and configure it as follows:
			- Code: VPRG
			- Name: Variant Price-Range Generator
			- Feed Module: Template Based Feed
			- Enable URI Access (optional, if you'd like to use it for testing/debugging)
			- Enable File Creation (optional, if you'd like to be able to click "Process Now" from the Marketing > Feeds page in the admin)
			- Template: Product
		2. Navigate to the `TEMPLATEFEED_VPRG` page template code in the Admin
		3. Switch to Advanced Mode
		4. Clear out the "Header Template"
		5. Populate the "Iterator Template" with this snippet's code.
		6. Assign the `customfields` item to the Feed's page.
		7. Create the following product custom fields (you can import this XML provisioning if you'd like, or just manually create them with the same codes):
			```
			<Module code="customfields" feature="util">
				<ProductField_Add>
					<code>variant_price_range</code>
					<name>Variant Prices: Price Range</name>
					<info>Automatically populated by "Variant Price-Range Generator" Marketing Feed.</info>
				</ProductField_Add>
				<ProductField_Add>
					<code>variant_price_low</code>
					<name>Variant Prices: Lowest Price</name>
					<info>Automatically populated by "Variant Price-Range Generator" Marketing Feed.</info>
				</ProductField_Add>
				<ProductField_Add>
					<code>variant_price_high</code>
					<name>Variant Prices: Highest Price</name>
					<info>Automatically populated by "Variant Price-Range Generator" Marketing Feed.</info>
				</ProductField_Add>
			</Module>
			```
	------------------------------------------------
</mvt:comment>

<h2>&mvt:record:name;</h2>


<mvt:comment>
====Load Product Attributes====
</mvt:comment>
<mvt:do file="g.Module_Library_DB" name="l.success" value="AttributeList_Load_Product(l.settings:record:id, l.settings:record:attributes)" />

<mvt:if expr="l.settings:record:attributes[1]:attemp_id GT 0">
	<mvt:do file="g.Module_Feature_ATT_DB" name="l.success" value="AttributeTemplateAttrList_Load_Template(l.settings:record:attributes[1]:attemp_id, l.settings:record:attributes)" />
</mvt:if>

<mvt:comment>
====Load Product Attribute Options + Add Option Prices to Array====
</mvt:comment>
<mvt:foreach iterator="attribute" array="record:attributes">

	<mvt:do file="g.Module_Library_DB" name="l.success" value="OptionList_Load_Attribute(l.settings:attribute:id, l.settings:attribute:options)" />
	<mvt:foreach iterator="option" array="attribute:options">

		<mvt:comment>
		====Check if Product has Variants + Load the Variant List====
		</mvt:comment>
		<mvt:do file="g.Module_Library_DB" name="l.success" value="ProductVariantList_Load_Product_Option( l.settings:record:id, l.settings:attribute:id, l.settings:option:id, l.settings:option:variants )" />


		<mvt:if expr="l.settings:option:variants[1]">
			<mvt:comment>
			====Load the Variant Parts====
			</mvt:comment>
			<mvt:foreach iterator="variant" array="option:variants">
				<mvt:do file="g.Module_Library_DB" name="l.success" value="ProductList_Load_Variant( l.settings:record:id, l.settings:variant:variant_id, l.settings:variant:parts )" />

				<mvt:foreach iterator="part" array="variant:parts">
                    <mvt:comment>
                    ====Only check parts that have inventory greater than 0 if active or have inventory inactive====
                    </mvt:comment>
                   	<mvt:if expr="( l.settings:part:inv_active EQ 1 AND l.settings:part:inv_available GT 0) OR ( l.settings:part:inv_active EQ 0 )">
                        <mvt:comment>
                        ====Determine Highest & Lowest Variant Part Prices====
                        </mvt:comment>
                        <mvt:if expr="l.settings:part:price GT 0">
                            <mvt:if expr="ISNULL l.settings:record:price_range:lowest OR l.settings:part:price LT l.settings:record:price_range:lowest">
                                <mvt:assign name="l.settings:record:price_range:lowest" value="l.settings:part:price" />
                            </mvt:if>

                            <mvt:if expr="ISNULL l.settings:record:price_range:highest OR l.settings:part:price GT l.settings:record:price_range:highest">
                                <mvt:assign name="l.settings:record:price_range:highest" value="l.settings:part:price" />
                            </mvt:if>
                        </mvt:if>
                    </mvt:if>
				</mvt:foreach>
			</mvt:foreach>

		</mvt:if>

	</mvt:foreach>

</mvt:foreach>

<mvt:if expr="l.settings:record:price_range:lowest AND l.settings:record:price_range:highest">
	<mvt:do file="'/mm5/5.00/modules/currency/usmoney.mvc'" name="l.settings:record:price_range:lowest_formatted" value="CurrencyModule_AddFormatting( g.Store:currncy_mod, l.settings:record:price_range:lowest )" />
	<mvt:do file="'/mm5/5.00/modules/currency/usmoney.mvc'" name="l.settings:record:price_range:highest_formatted" value="CurrencyModule_AddFormatting( g.Store:currncy_mod, l.settings:record:price_range:highest )" />

	<mvt:assign name="l.settings:record:price_range:text" value="l.settings:record:price_range:lowest_formatted $ ' - ' $ l.settings:record:price_range:highest_formatted" />

	<mvt:item name="customfields" param="Write_Product_ID(l.settings:record:id, 'variant_price_range', l.settings:record:price_range:text)" />
	<mvt:item name="customfields" param="Write_Product_ID(l.settings:record:id, 'variant_price_low', l.settings:record:price_range:lowest)" />
	<mvt:item name="customfields" param="Write_Product_ID(l.settings:record:id, 'variant_price_high', l.settings:record:price_range:highest)" />
</mvt:if>

<mvt:eval expr="glosub(miva_array_serialize(l.settings:record), ',', '<br>')" />