Skip to content

Create Swatch Image#


Description#

Create Swatch Image based on variant’s main image

Snippet#

<mvt:do file="g.Module_Library_DB" name="l.success" value="ImageType_Load_Code( 'main', l.imagetype:main )" />
<mvt:assign name="l.width" value="52" />
<mvt:assign name="l.height" value="52" />
<mvt:foreach iterator="attribute" array="attributes">
    <mvt:if expr="l.settings:attribute:type EQ 'swatch-select'">
        <mvt:foreach iterator="option" array="attribute:options">
            <mvt:if expr="ISNULL l.settings:option:image">
                <mvt:comment> NULL Out Variables </mvt:comment>
                <mvt:assign name="l.settings:variants" value="NULL" />
                <mvt:do file="g.Module_Library_DB" name="l.success" value="ProductVariantList_Load_Product_Option(l.settings:product:id, l.settings:option:attr_id, l.settings:option:id, l.settings:variants)"/>
                <mvt:foreach iterator="variant" array="variants">
                    <mvt:comment> NULL Out Variables </mvt:comment>
                    <mvt:assign name="l.loaded_variants" value="NULL" />
                    <mvt:assign name="l.loaded_type" value="NULL" />
                    <mvt:assign name="l.imagedata" value="NULL" />
                    <mvt:assign name="l.image" value="NULL" />
                    <mvt:assign name="l.generated_image:image" value="NULL" />
                    <mvt:do file="g.Module_Library_DB" name="l.success" value="ProductList_Load_Variant(l.settings:product:id, l.settings:variant:variant_id, l.loaded_variants)" />
                    <mvt:if expr="l.loaded_variants[1]:id GT 0">
                        <mvt:do file="g.Module_Library_DB" name="l.success" value="ProductImage_Load_Type( l.loaded_variants[1]:id, l.imagetype:main:id, l.loaded_type )" />
                        <mvt:do file="g.Module_Library_DB" name="l.success" value="Image_Load_ID( l.loaded_type:image_id, l.imagedata )" />
                        <mvt:do file="g.Module_Library_DB" name="l.image_loaded_success" value="Image_Load_File( l.imagedata:image, l.image )" />
                        <mvt:if expr="l.image_loaded_success EQ 1">
                            <mvt:do file="g.Module_Library_DB" name="l.success" value="GeneratedImage_FindOrInsert_Image_Dimensions( l.image, l.width, l.height, l.generated_image )" />
                            <mvt:if expr="l.generated_image">
                                <mvt:assign name="l.settings:option:image" value="l.generated_image:image" />
                                <mvt:do file="g.Module_Library_DB" name="l.success" value="Option_Update( l.settings:option )" />
                            </mvt:if>
                        </mvt:if>
                        <mvt:foreachstop />
                    </mvt:if>
                </mvt:foreach>
            </mvt:if>
        </mvt:foreach>
    </mvt:if>
</mvt:foreach>