Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steveosoule/28f53b92dba3b716e7a4b352392fd47e to your computer and use it in GitHub Desktop.
Save steveosoule/28f53b92dba3b716e7a4b352392fd47e to your computer and use it in GitHub Desktop.
Miva - Variant Basket Item Replacement with Fix for Skipped Products
<mvt:comment>
| ------------------------------------------------
| Variant Basket Item Replacement for Upsold Variants
| ------------------------------------------------
</mvt:comment>
<mvt:foreach iterator="item" array="basket:items">
<mvt:assign name="l.settings:item:options_count" value="miva_array_elements( l.settings:item:options )" />
<mvt:comment>
We only need to update upsold items with variants
</mvt:comment>
<mvt:if expr="l.settings:item:upsold AND l.settings:item:options_count AND NOT l.settings:item:variant_id">
<mvt:comment>Proceed with look-up</mvt:comment>
<mvt:else>
<mvt:foreachcontinue />
</mvt:if>
<mvt:comment>
Load Variant ID based on the items options
</mvt:comment>
<mvt:do file="g.Module_Library_Utilities" name="l.result" value="OrderItem_DetermineVariant_WithOptions(l.settings:item, l.settings:item:options, l.settings:item:options_count, l.settings:item:loaded:product, l.settings:item:loaded:variant_id)" />
<mvt:if expr="NOT l.settings:item:loaded:variant_id">
<mvt:foreachcontinue />
</mvt:if>
<mvt:comment>
With the Variant ID, load the Variant Parts
</mvt:comment>
<mvt:do file="g.Module_Library_DB" name="l.result" value="ProductList_Load_Variant(l.settings:item:loaded:product:id, l.settings:item:loaded:variant_id, l.settings:item:loaded:variant_parts)" />
<mvt:comment>
Populate the default `item:parts[]` if they don't exist
</mvt:comment>
<mvt:if expr="miva_array_elements( l.settings:item:loaded:variant_parts )">
<mvt:assign name="l.settings:item:variant_id" value="l.settings:item:loaded:variant_id" />
<mvt:assign name="l.settings:item:parts" value="l.settings:item:loaded:variant_parts" />
</mvt:if>
</mvt:foreach>
<mvt:comment>
| ------------------------------------------------
| Variant Basket Item Replacement for Skipped Replacements
| ------------------------------------------------
| This custom functionality replicates the VBIR module,
| The main difference is that the standard VBIR module ties into the ADPR, ADPM, actions and sometimes products item don't get updated.
| This code will always ensure that the basket item codes are updated.
</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>
| 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:foreach iterator="group" array="basket:groups">
<mvt:if expr="POS2 NE POS1">
<mvt:foreachcontinue />
</mvt:if>
<mvt:assign name="l.settings:group:code" value="l.settings:item:code" />
<mvt:assign name="l.settings:group:variant_id" value="l.settings:item:variant_id" />
<mvt:assign name="l.settings:group:parts" value="l.settings:item:parts" />
</mvt:foreach>
</mvt:if>
</mvt:foreach>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment