amp-story-shopping
Introduction
The amp-story-shopping
component is used to create shopping experiences within Web Stories.
To begin, import amp-story-shopping
along with your other stories imports.
<script async custom-element="amp-story-shopping" src="https://cdn.ampproject.org/v0/amp-story-shopping-0.1.js"></script>
Basic usage
amp-story-shopping
is defined in the document using two custom elements, amp-story-shopping-attachment
and amp-story-shopping-tag
.
Specify a shopping experience by defining one amp-story-shopping-attachment
as the last element on the page.
The amp-story-shopping-attachment
component accepts 1 descendant, a script element that with product JSON that configures the experience.
Configuring inline product JSON as a child script tag is required. An optional src
attribute will fetch data from an endpoint at render time. If src
is defined it overrides the inline configuration. If the remote configuration request fails the inline configuration will be used as a fallback.
For a full description of the product JSON see the documentation.
At least one amp-story-shopping-tag
element must be on the page for the experience to display.
The amp-story-shopping-tag
component displays a tappable button. When tapped, it opens more information about the product in an page attachment.
The data-product-id
attribute must match the productId
in the product JSON. Pay special attention to this to be sure the button displays the correct data.
Custom CSS is used to position the button on the page. The button is positioned absolute by default. For responsive placement use left
and right
CSS rules with percentage based values.
The amp-story-shopping-tag
must be a descendant of amp-story-grid-layer
.
Notice that a "Shop now" CTA button displays at the bottom of the page when the experience is configured.
<amp-story-page id="page-1">
<amp-story-grid-layer template="fill">
<amp-img layout="fill" src="https://source.unsplash.com/_HqHX3LBN18/1000x2000"></amp-img>
</amp-story-grid-layer>
<amp-story-grid-layer template="vertical">
<amp-story-shopping-tag data-product-id="lamp"></amp-story-shopping-tag>
</amp-story-grid-layer>
<amp-story-shopping-attachment>
<script type="application/json">
{
"items": [
{
"productUrl": "https://www.google.com",
"productId": "lamp",
"productTitle": "Brass Lamp",
"productBrand": "Lamp Co",
"productPrice": 799.0,
"productPriceCurrency": "USD",
"productImages": [
{"url": "https://source.unsplash.com/Ry9WBo3qmoc/500x500", "alt": "lamp 1"},
{"url": "https://source.unsplash.com/KP7p0-DRGbg", "alt": "lamp 2"},
{"url": "https://source.unsplash.com/mFnbFaCIu1I", "alt": "lamp 3"},
{"url": "https://source.unsplash.com/py9sH2rThWs", "alt": "lamp 4"},
{"url": "https://source.unsplash.com/VDPauwJ_sHo", "alt": "lamp 5"},
{"url": "https://source.unsplash.com/3LTht2nxd34", "alt": "lamp 6"}
],
"aggregateRating": {
"ratingValue": 4.4,
"reviewCount": 89,
"reviewUrl": "https://www.google.com"
},
"productDetails": "Lorem ipsum dolor sit amet consectetur adipisicing elit. Facere error deserunt dignissimos in laborum ea molestias veritatis sint laudantium iusto expedita atque provident doloremque, ad voluptatem culpa adipisci."
}
]
}
</script>
</amp-story-shopping-attachment>
</amp-story-page>
Remote product JSON with src
attribute
Add a src
attribute to the amp-story-shopping-attachment
to remotely fetch the data at render time.
Using src
ensures product data will be up to date, even when served from the cache.
It is important to note that product data is specific to each page. It cannot be shared between pages. This ensures data is only loaded when needed.
<amp-story-page id="page-2">
<amp-story-grid-layer template="fill">
<amp-img layout="fill" src="https://source.unsplash.com/_HqHX3LBN18/1000x2000"></amp-img>
</amp-story-grid-layer>
<amp-story-grid-layer template="vertical">
<amp-story-shopping-tag data-product-id="chair"></amp-story-shopping-tag>
<amp-story-shopping-tag data-product-id="lamp"></amp-story-shopping-tag>
</amp-story-grid-layer>
<amp-story-shopping-attachment src="/static/samples/json/amp-story-shopping.json">
<script type="application/json">
{
"items": [
{
"productUrl": "https://www.google.com",
"productId": "chair",
"productTitle": "Yellow chair",
"productBrand": "Chair Co.",
"productPrice": 1000.0,
"productPriceCurrency": "USD",
"productImages": [
{"url": "https://source.unsplash.com/DgQGKKLaVhY/500x500", "alt": "chair"}
],
"aggregateRating": {
"ratingValue": 4.4,
"reviewCount": 89,
"reviewUrl": "https://www.google.com"
},
"productDetails": "Lorem ipsum dolor sit amet consectetur adipisicing elit. Facere error deserunt dignissimos in laborum ea molestias veritatis sint laudantium iusto expedita atque provident doloremque, ad voluptatem culpa adipisci."
},
{
"productUrl": "https://www.google.com",
"productId": "lamp",
"productTitle": "Brass Lamp",
"productBrand": "Lamp Co",
"productPrice": 799.0,
"productPriceCurrency": "USD",
"productImages": [
{"url": "https://source.unsplash.com/Ry9WBo3qmoc/500x500", "alt": "lamp 1"},
{"url": "https://source.unsplash.com/KP7p0-DRGbg", "alt": "lamp 2"},
{"url": "https://source.unsplash.com/mFnbFaCIu1I", "alt": "lamp 3"},
{"url": "https://source.unsplash.com/py9sH2rThWs", "alt": "lamp 4"},
{"url": "https://source.unsplash.com/VDPauwJ_sHo", "alt": "lamp 5"},
{"url": "https://source.unsplash.com/3LTht2nxd34", "alt": "lamp 6"}
],
"aggregateRating": {
"ratingValue": 4.4,
"reviewCount": 89,
"reviewUrl": "https://www.google.com"
},
"productDetails": "Lorem ipsum dolor sit amet consectetur adipisicing elit. Facere error deserunt dignissimos in laborum ea molestias veritatis sint laudantium iusto expedita atque provident doloremque, ad voluptatem culpa adipisci."
}
]
}
</script>
</amp-story-shopping-attachment>
</amp-story-page>
Optional configuration
In this example we display a custom icon and text in the amp-story-shopping-tag
by defining productIcon
and productTagText
within the product JSON.
We recommend using a 48 x 48px image for productIcon
. We recommend using 1-3 words for productTagText
. If the text is too long it will become two lines and display ellipses.
We also set theme="dark"
on the amp-story-shopping-attachment
to render the "Shop now" cta button and page attachment in a dark palette.
<amp-story-page id="page-3">
<amp-story-grid-layer template="fill">
<amp-img layout="fill" src="https://source.unsplash.com/_HqHX3LBN18/1000x2000"></amp-img>
</amp-story-grid-layer>
<amp-story-grid-layer template="vertical">
<amp-story-shopping-tag data-product-id="flowers"></amp-story-shopping-tag>
<amp-story-shopping-tag data-product-id="chair"></amp-story-shopping-tag>
<amp-story-shopping-tag data-product-id="lamp"></amp-story-shopping-tag>
</amp-story-grid-layer>
<amp-story-shopping-attachment theme="dark" src="/static/samples/json/amp-story-shopping.json">
<script type="application/json">
{
"items": [
{
"productIcon": "/static/samples/img/amp-story-shopping-icon.png",
"productTagText": "Flowers",
"productUrl": "https://www.google.com",
"productId": "flowers",
"productTitle": "Flowers",
"productBrand": "Flower Company Name",
"productPrice": 10.0,
"productPriceCurrency": "USD",
"productImages": [
{"url": "https://source.unsplash.com/SavQfLRm4Do/500x500", "alt": "flowers"}
],
"aggregateRating": {
"ratingValue": 4.4,
"reviewCount": 89,
"reviewUrl": "https://www.google.com"
},
"productDetails": "Lorem ipsum dolor sit amet consectetur adipisicing elit. \n Facere error deserunt dignissimos in laborum ea molestias veritatis sint laudantium iusto expedita atque provident doloremque, ad voluptatem culpa adipisci."
},
{
"productUrl": "https://www.google.com",
"productId": "chair",
"productTitle": "Yellow chair",
"productBrand": "Chair Co.",
"productPrice": 1000.0,
"productPriceCurrency": "USD",
"productImages": [
{"url": "https://source.unsplash.com/DgQGKKLaVhY/500x500", "alt": "chair"}
],
"aggregateRating": {
"ratingValue": 4.4,
"reviewCount": 89,
"reviewUrl": "https://www.google.com"
},
"productDetails": "Lorem ipsum dolor sit amet consectetur adipisicing elit. \n Facere error deserunt dignissimos in laborum ea molestias veritatis sint laudantium iusto expedita atque provident doloremque, ad voluptatem culpa adipisci."
},
{
"productUrl": "https://www.google.com",
"productId": "lamp",
"productTitle": "Brass Lamp",
"productBrand": "Lamp Co",
"productPrice": 799.0,
"productPriceCurrency": "USD",
"productImages": [
{"url": "https://source.unsplash.com/Ry9WBo3qmoc/500x500", "alt": "lamp 1"},
{"url": "https://source.unsplash.com/KP7p0-DRGbg", "alt": "lamp 2"},
{"url": "https://source.unsplash.com/mFnbFaCIu1I", "alt": "lamp 3"},
{"url": "https://source.unsplash.com/py9sH2rThWs", "alt": "lamp 4"},
{"url": "https://source.unsplash.com/VDPauwJ_sHo", "alt": "lamp 5"},
{"url": "https://source.unsplash.com/3LTht2nxd34", "alt": "lamp 6"}
],
"aggregateRating": {
"ratingValue": 4.4,
"reviewCount": 89,
"reviewUrl": "https://www.google.com"
},
"productDetails": "Lorem ipsum dolor sit amet consectetur adipisicing elit. \n Facere error deserunt dignissimos in laborum ea molestias veritatis sint laudantium iusto expedita atque provident doloremque, ad voluptatem culpa adipisci."
}
]
}
</script>
</amp-story-shopping-attachment>
</amp-story-page>
More Resources
If you want to learn more about shopping within Web Stories, head over to the documentation.
如果此页面上的说明未能涵盖您的所有问题,欢迎与其他 AMP 用户取得联系,讨论您的具体用例。
前往 Stack Overflow 一项无法解释的功能?AMP 项目强烈鼓励您参与并做出贡献!我们希望您能成为我们开放源代码社区的持续参与者,但我们也欢迎您对所热衷问题做出一次性贡献。
编辑 GitHub 上的示例-
Written by @philipbell