This snippet utilizes the Product Data Fields and Feeds module to add the total inventory of all variants to the master product custom field in realtime. It will also add the indiviual part inventory count to a custom field on the variant product. If the product does not have variants it will add the inventory of the product.
<mvt:comment>
------------------------------------------------
Variant Inventory Count
Code: PRODUCTDATAFIELD_VINC
Main Template
Instructions:
1. Install Product Data Fields and Feed module.
2. Add Field
- Name: Variant Inventory Count
- Code: VINC
- On Inventory Change: Queue Modified Products and Variants Masters
- Trigger: VINC
- Public: True (checked)
3. Navigate to the `PRODUCTDATAFIELD_VINC` page template code in the Admin
4. Clear out the "Main Template"
5. Populate the "Main Template" with this snippet's code.
6. Create a Scheduled Task
- Description: Variant Inventory Count
- Trigger: VINC
- Operation: Product Data Fields and Feeds: Generate Product Field Data
- Product Data Field: VINC
- Generation: Queued Products
</mvt:comment>
<mvt:comment>
====Load Variant Part Data====
</mvt:comment>
<mvt:do name="l.productvariantpart_count" file="g.Module_Library_DB" value="ProductVariantPartList_Load_Part(l.settings:product:id, l.settings:variant_data)" />
<mvt:comment>
====Set Master Product ID====
</mvt:comment>
<mvt:if expr="l.settings:variant_data[1]:product_id">
<mvt:assign name="l.settings:master_product:id" value="l.settings:variant_data[1]:product_id" />
<mvt:else>
<mvt:assign name="l.settings:master_product:id" value="l.settings:product:id" />
</mvt:if>
<mvt:comment>
====Set Master Product Total Inventory to 0====
</mvt:comment>
<mvt:assign name="l.settings:master_product:total_inv" value="0" />
<mvt:comment>
====Load Master Product Variants====
</mvt:comment>
<mvt:do file="g.Module_Library_DB" name="l.settings:master_product:variant_count" value="ProductVariantList_Load_Product(l.settings:master_product:id, l.settings:master_product:variants)" />
<mvt:foreach iterator="variant" array="master_product:variants">
<mvt:foreach iterator="part" array="variant:parts">
<mvt:comment>
====Add Parts inv_available====
</mvt:comment>
<mvt:assign name="l.settings:master_product:total_inv" value="l.settings:master_product:total_inv + l.settings:part:inv_available" />
<mvt:if expr="l.settings:product:code EQ l.settings:part:code">
<mvt:assign name="l.settings:variant_product:total_inv" value="l.settings:part:inv_available" />
</mvt:if>
</mvt:foreach>
</mvt:foreach>
<mvt:comment>
==== Output Inventory Totals ====
</mvt:comment>
<mvt:if expr="l.settings:master_product:variant_count EQ 0">
<mvt:comment>
==== Master Product Does Not Have Inventory Variants ====
==== Output Product Information with Runtime Inventory ====
</mvt:comment>
<mvt:do file="g.Module_Library_DB" name="l.void" value="Product_Load_ID_WithRuntimeInventory( l.settings:product:id, l.settings:master_product )" />
<mvt:eval expr="l.settings:master_product:inv_available" />
<mvt:else>
<mvt:comment>
==== If the Product is a Variant Then Output the Variant Part Inventory ====
==== Else the Product is a Master Product with Variants so Output the total Variant Part Inventory ====
</mvt:comment>
<mvt:if expr="l.settings:variant_data[1]:product_id">
<mvt:eval expr="l.settings:variant_product:total_inv" />
<mvt:else>
<mvt:eval expr="l.settings:master_product:total_inv" />
</mvt:if>
</mvt:if>