<mvt:comment>
| ------------------------------------------------
| Variant Basket Item Replacement (VBIR) with Exclusion
| ------------------------------------------------
| This custom functionality replicates the VBIR module,
| EXCEPT it allows line items to not have their variant code replaced based on a `disable_variant_basket_item_replacement` custom-field
</mvt:comment>
<mvt:foreach iterator="item" array="basket:items">
<mvt:comment>
| We only need to do this process for items with a single variant part
</mvt:comment>
<mvt:if expr="miva_array_elements( l.settings:item:parts ) NE 1 ">
<mvt:foreachcontinue/>
</mvt:if>
<mvt:comment>
| Check if the current item woudld like to disable the VBIR functionality
</mvt:comment>
<mvt:item name="customfields" param="Read_Product_ID(l.settings:item:product:id, 'disable_variant_basket_item_replacement', l.settings:item:cf:disable_variant_basket_item_replacement)" />
<mvt:if expr="l.settings:item:cf:disable_variant_basket_item_replacement">
<mvt:foreachcontinue/>
</mvt:if>
<mvt:comment>
| Update the DB with our custom variant code logic
</mvt:comment>
<mvt:if expr="l.settings:item:parts[1]:code NE l.settings:item:code">
<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:parts[1]: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:comment>
| Update the current page's `l.settings:basket:items[]:code` and `l.settings:basket:groups[]:code` values for the current element
</mvt:comment>
<mvt:assign name="l.settings:item:code" value="l.settings:item:parts[1]:code" />
<mvt:assign name="l.result" value="miva_array_search( l.settings:basket:groups, POS1, l.group, 'l.settings:item:code EQ l.group:code' )" />
<mvt:assign name="l.group:code" value="l.settings:item:parts[1]:code" />
</mvt:if>
</mvt:foreach>