<mvt:comment>
| ------------------------------------------------
| Custom Variant Basket Item Replacement
| ------------------------------------------------
| This logic is to help aid in Synncronyzing with Quickbooks for products with multiple parts.
| Our standard Variant Basket Item Replacement module does not work for variants with multiple parts.
</mvt:comment>
<mvt:foreach iterator="item" array="basket:items">
<mvt:comment>
| We only need to do this process for items with variant parts
</mvt:comment>
<mvt:if expr="miva_array_elements( l.settings:item:parts ) EQ 0 ">
<mvt:foreachcontinue/>
</mvt:if>
<mvt:comment>
| Lets prepare a concatenation of part codes
| Also, we'll check if one of the products has been flagged as the one to use for replacement (and disregard the concatenation)
</mvt:comment>
<mvt:foreach iterator="part" array="item:parts">
<mvt:assign name="l.index" value="miva_array_insert( l.settings:item:cf:part_codes, l.settings:part:code, -1 )" />
<mvt:item name="customfields" param="Read_Product_ID(l.settings:part:id, 'use_as_variant_replacement_code', l.settings:part:cf:use_as_variant_replacement_code)" />
<mvt:if expr="l.settings:part:cf:use_as_variant_replacement_code">
<mvt:assign name="l.settings:item:cf:variant_replacement_code" value="l.settings:part:code" />
<mvt:foreachstop/>
</mvt:if>
</mvt:foreach>
<mvt:assign name="l.settings:item:cf:part_codes" value="glosub( l.settings:item:cf:part_codes, ',', '_' )" />
<mvt:comment>
| Use designated product for variant replacement,
| or use concatenated product codes
</mvt:comment>
<mvt:if expr="l.settings:item:cf:variant_replacement_code">
<mvt:assign name="l.settings:item:cf:update_code" value="l.settings:item:cf:variant_replacement_code" />
<mvt:else>
<mvt:assign name="l.settings:item:cf:update_code" value="l.settings:item:cf:part_codes" />
</mvt:if>
<mvt:comment>
| Miva's product codes cant't be longer than 50 characters.
| We'll md5 it if they are too long
</mvt:comment>
<mvt:if expr="len(l.settings:item:cf:part_codes) GT 50">
<mvt:assign name="l.settings:item:cf:part_codes" value="crypto_md5( l.settings:item:cf:part_codes )" />
</mvt:if>
<mvt:comment>
| Update the DB with our custom variant code logic
</mvt:comment>
<mvt:if expr="l.settings:item:code NE l.settings:item:cf:part_codes">
<mvt:assign name="l.settings:query:query" value="'UPDATE ' $ g.store_table_prefix $ 'BasketItems SET code = ? WHERE line_id = ?'" />
<mvt:assign name="l.settings:query:bind_parameters[1]" value="l.settings:item:cf:update_code" />
<mvt:assign name="l.settings:query:bind_parameters[2]" value="l.settings:item:line_id" />
<mvt:item name="sql" param="l.settings:query"/>
</mvt:if>
</mvt:foreach>