Skip to content
ms3Variants
ms3Variants
Product variants component for MiniShop3
  • MODX 3
  • PHP 8.2
  • miniShop3
  • Vue 3
  1. Extras
  2. ms3Variants
  3. Snippets

Snippets ​

msProductVariants ​

Outputs product variants on the product page: ready HTML from chunks, or a data array for your own markup.

Call the snippet uncached

Write '!msProductVariants', with the exclamation mark. Without it, variant prices and stock are stored in the page cache: editing a variant in the manager and deducting stock do not clear that cache, so the customer sees an old price and availability. With the returnData parameter, a cached call causes an error 500 when the page is loaded again.

Parameters ​

ParameterDefaultDescription
productcurrent resourceProduct ID. If the resource is not a product, the output is empty (see below)
tplms3_variantsWrapper chunk for all variants
tplRowms3_variants_rowChunk for one variant
activeOnly11 — active variants only, 0 — all. An inactive variant counts as out of stock, so with ms3variants_show_out_of_stock = No it is hidden even with 0. The setting defaults to Yes
sortbypositionAny variant field: position, price, old_price, sku, count, weight, id. A typo in the field name makes the snippet output nothing, and an Error … executing query entry appears in the MODX error log
sortdirASCDirection: ASC or DESC
includeJs1Include JavaScript (1/0). Not included with returnData
includeCss1Include CSS (1/0). Not included with returnData
outputSeparator\nSeparator between variants
returnData0Return a data array instead of HTML (1/0)

Call ​

fenom
{'!msProductVariants' | snippet}

Custom chunks, sorting, another product:

fenom
{'!msProductVariants' | snippet : [
    'tpl' => 'my_variants_wrapper',
    'tplRow' => 'my_variant_item',
    'sortby' => 'price',
    'sortdir' => 'ASC',
    'product' => 42
]}

Data into a variable ​

With returnData the snippet returns an array, and you write the markup yourself.

fenom
{set $variantsData = '!msProductVariants' | snippet : ['returnData' => 1]}

{if $variantsData.total > 0}
    {foreach $variantsData.variants as $variant}
        <div>{$variant.sku} — {$variant.price}</div>
    {/foreach}
{/if}
KeyTypeDescription
product_idintProduct ID
variantsarrayVariants with all fields of the variant row, including idx and options_string
available_optionsarrayVariant option values — as in the wrapper
totalintNumber of variants

JavaScript and CSS are not included in this mode. If your markup uses data-ms3v-*, include assets/components/ms3variants/js/web/ms3variants.js and assets/components/ms3variants/css/web/ms3variants.css yourself.

When there are no variants ​

CaseRegular callWith returnData
The product has no variants, or all are hiddenempty string, the tpl chunk is not outputempty array []
The resource is not a productempty string and an entry in the MODX error logempty string, not an array

Only a resource with class_key = MiniShop3\Model\msProduct counts as a product. Do not call the snippet in a template shared by all pages: every non-product page adds an entry to the error log.

Placeholders in tpl (wrapper) ​

PlaceholderTypeDescription
{$product_id}intProduct ID
{$rows}stringVariant rows rendered with the tplRow chunk
{$variants}arrayVariants array for Fenom
{$available_options}arrayVariant option values by key: {color: ['Red', 'Blue']}
{$total}intNumber of variants
{$options_json}stringavailable_options as JSON
{$variants_json}stringOptions of each variant as JSON: {"12": {"color": "Red"}}

available_options is built from all active variants of the product. It ignores the ms3variants_show_out_of_stock setting: a value whose variants are all out of stock stays in it, although there are no rows with it.

Placeholders in tplRow (variant row) ​

PlaceholderTypeDescription
{$id}intVariant ID
{$product_id}intProduct ID
{$sku}string, may be emptySKU
{$price}floatPrice
{$old_price}float, may be emptyOld price
{$count}intStock
{$weight}float, may be emptyWeight
{$active}boolThe variant is active
{$position}intPosition
{$in_stock}boolIn stock: the variant is active and its stock is above zero. With ms3variants_check_stock = No — any active variant. The setting defaults to Yes
{$file_id}int, may be emptyID of the image from the product gallery
{$image_url}string, may be emptyImage URL. Empty if no image is selected
{$options}arrayVariant options as a list: [{key, value}, ...]
{$options_string}stringOption values as a string: "Red, XL"
{$options_array}arrayOptions by key: {color: 'Red', size: 'XL'}
{$created_at}, {$updated_at}stringCreation and update dates
{$idx}intSequence number, starting from 0

Prices are plain numbers without formatting.

Variants in the catalog (msProducts) ​

To output variants in the catalog, pass the usePackages parameter to the standard msProducts snippet:

fenom
{'msProducts' | snippet : [
    'parents' => 0,
    'usePackages' => 'ms3Variants',
    'tpl' => 'ms3_products_row_variants'
]}

The value ms3Variants is case-sensitive: with ms3variants, variants are not loaded.

The catalog gets only active variants ordered by position. The activeOnly and sortby parameters of msProductVariants do not apply here. Out-of-stock variants are hidden by the same ms3variants_show_out_of_stock setting as on the product page.

Without replacing ProductCardUI, the cart gets the product, not the variant

For the catalog buttons to add the selected variant, replace the standard MiniShop3 module with the ms3Variants module — see the "ProductCardUI module" section on the Product catalog page.

Product placeholders with variants ​

PlaceholderTypeDescription
{$has_variants}boolWhether the product has variants
{$variants_count}intNumber of variants
{$variants_json}stringThe same data as in {$variants}, as a JSON array. The structure differs from {$variants_json} in the msProductVariants wrapper
{$variants}arrayVariants array for Fenom

A product without variants has {$has_variants} = false, {$variants_count} = 0, {$variants} — an empty array, {$variants_json} — [].

Variant fields in {$variants} ​

php
[
    'id' => 1,
    'product_id' => 42,
    'sku' => 'ABC-123-red-XL',
    'price' => 1500.00,
    'old_price' => 2000.00,
    'count' => 10,
    'weight' => 0.5,
    'active' => true,
    'position' => 0,
    'in_stock' => true,
    'file_id' => 7,
    'image_url' => '/assets/images/products/42/product.jpg',
    'small' => '/assets/images/products/42/small/product.jpg', // only with includeThumbs
    'options' => [['key' => 'color', 'value' => 'red'], ['key' => 'size', 'value' => 'XL']],
    'options_array' => ['color' => 'red', 'size' => 'XL'],
]

With image thumbnails ​

includeThumbs is a standard msProducts parameter. Variant thumbnails are loaded only if usePackages is set too. Pass thumbnail names from the product media source settings:

fenom
{'msProducts' | snippet : [
    'parents' => 0,
    'usePackages' => 'ms3Variants',
    'includeThumbs' => 'small,medium'
]}

A variant with an image gets small and medium fields with thumbnail URLs. A variant without an image has no such fields. The msProductVariants snippet does not load thumbnails.