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.
Nếu bạn vẫn còn thắc mắc sau khi đọc hướng dẫn trên trang này, hãy liên hệ với những người dùng AMP khác để thảo luận về trường hợp sử dụng cụ thể của bạn.
Truy cập Stack Overflow Một tính năng chưa được giải thích?Dự án AMP đặc biệt khuyến khích sự tham gia và đóng góp của bạn! Chúng tôi hi vọng bạn sẽ trở thành một người tham gia tích cực trong cộng đồng mã nguồn mở của chúng tôi, nhưng chúng tôi cũng chào mừng các đóng góp đơn lẻ về vấn đề mà bạn đặc biệt quan tâm.
Chỉnh sửa mẫu trên GitHub-
Written by @philipbell