spz-variants
布局
Layouts
跳转到“Layouts”Description
跳转到“Description”You add variants to a product that comes in more than one option, such as size
or color
. Each combination of option values for a product can be a variant for that product. For example, suppose that you sell T-shirts with two options:size
and color
. The size has three optional values:S
, M
, and L
. The color has two optional values:Orange
and White
. So you can select an M
and Orange
T-shirt to specific a variant.
Usage
跳转到“Usage”- The child elements of the
spz-variant
must have arole="variant"
attribute.
The following is an example without templates:
<spz-variants
manual
layout="container"
id="product-info-variants"
name="product-info-variant"
src="script:product-json"
@variantChange="product-info-quantity.update(value=event.quantity,max=event.max,min=event.min);"
slide="product-main-images;"
switch-slide='["color"]'
>
<!-- ... -->
<spz-selector
role="variant"
layout="container"
name="optionName"
@select="product-info-variants.change(data=event);"
default-value="selectedOptValue"
>
<!-- ... -->
</spz-selector>
</spz-variants>
The following is an example with templates:
<spz-variants
layout="container"
id="product-info-variants"
name="product-info-variant"
src="/api/products/f4138308-ac57-46d5-bba7-ac60b2c6aefa"
@variantChange="product-info-quantity.update(value=event.quantity,max=event.max,min=event.min);"
slide="product-main-images;"
switch-slide='["color"]'
>
<template>
<div>
${function() {
// ...
return (data.product.options || []).map((option, index) => {
const optionName = option.name || '';
return `
<spz-selector
role="variant"
layout="container"
name="${optionName}"
@select="product-info-variants.change(data=event);"
default-value="${data.selectedValues[optionName]}"
>
<div>
<!-- ... -->
</div>
</spz-selector>
`;
}).join('');
}()}
</div>
</template>
</spz-variants>
Attributes
跳转到“Attributes”id
跳转到“id”required
A string of the DOM ID. The id
defines an identifier (ID) which must be unique in the whole document.
src
跳转到“src”required
The value of this attribute can take three forms, one is using an API URL, the other is using script
data, and the other is calling the render
action. Src attribute usage.
The element must have a template
when it is using an API URL and calling the render
action.
<!-- an API URL -->
<spz-variants
layout="container"
id="variants"
src="/api/products/f4138308-ac57-46d5-bba7-ac60b2c6aefa"
>
<template>
<div>
<!-- ... -->
</div>
</template>
</spz-variants>
<!-- render action -->
<spz-render
layout="container"
src="/api/products/f4138308-ac57-46d5-bba7-ac60b2c6aefa"
items="data.product"
@finish="variants.render(product=event.data);"
></spz-render>
<spz-variants
layout="container"
id="variants"
manual
>
<template>
<div>
<!-- ... -->
</div>
</template>
</spz-variants>
manual
跳转到“manual”If this attribute is present, the template doesn’t render at the beginning unless triggered by render
, rerender
, etc.
relative
跳转到“relative”If this attribute is present, it will determine whether the current variant combination exists when the user clicks on any of the variant options. If it doesn’t exist, it will set whether the combination of all the options of another variant and the currently selected variant is optional. If not, then add a disabled
attribute.
keep-behavior
跳转到“keep-behavior”A string of a URL parameter. Get default variant combination id from URL parameter. It only works in template form.
For example, in this URL https://duck.dev.myshoplaza.com/products/haldon-cashmere-crew?variant=be2d8528-8918-466b-93f6-f9b097d330c5&preview_theme_id=
, the keep-behavior
value will be set to variant
which will specify a variant combination with an id of be2d8528-8918-466b-93f6-f9b097d330c5
that is selected by default.
keep-initial
跳转到“keep-initial”If this attribute is present, the template data doesn’t have variant data selected by default. It only works in template form.
slide
跳转到“slide”Specify which carousel to switch. It is a semicolon separated string. For example slide="product-images;product-images-mobile;"
.
It must be used with the switch-slide
attribute.
switch-slide
跳转到“switch-slide”If it is specified, the corresponding product images will switch to the current variant image when the product has turned on the variant image and the user clicks on any variant option. It is an array of strings. For example switch-slide="switch-slide="["color","size"]"
.
It must be used with the slide
attribute.
quantity
跳转到“quantity”A class of the quantity elements. It can be used to specify multiple quantity selector elements to get the quantity of the variant selected by the user. For example quantity=".product-info-quantity"
.
<!--
Displayed when the value of the quantity selector is less than 10.
-->
<spz-quantity
layout="container"
class="product-info-quantity"
name="quantity"
variant-id="variants"
...
>
<!-- ... -->
<spz-quantity>
<!--
Displayed when the value of the quantity selector is greater than or equal to 10.
-->
<spz-selector
layout="container"
class="product-info-quantity"
name="quantity"
variant-id="variants"
>
<!-- ... -->
</spz-selector>
locale-unavailable-toast
跳转到“locale-unavailable-toast”Specifies the text of the toast when the current selected variant combination doesn’t exist.
Actions
跳转到“Actions”render
跳转到“render”render(product=<Object | Array>)
Use the passed data to render the template. It has a parameter:
product
: A Object or Array of the product data.
rerender
跳转到“rerender”rerender(product=<String>)
Use the src
attribute to re-render the template. It has a parameter:
product
: A string of the product id.
change
跳转到“change”To trigger the spz-variant
change. No parameter required.
Events
跳转到“Events”event object data
跳转到“event object data”{
"disabledValues": [],
"max": 90, // the current variant available quantity
"name": "Size", // the current variant name
"product": { // the current product data
// ...
},
"selectedValues": { // the current selected content
"Size": ["XS"],
"Color": ["Red"]
},
"variant": { // the current selected variant data
// ...
},
"variantId": "f0571d4e-b5af-4656-ad5c-2a8c5d030840", // the current selected variant id
"originData": { // original data
// ...
}
}
variantChange
跳转到“variantChange”Be automatically triggered when the user selects an option.