AMP
  • websites

Product Browse Page

Introduction

This is a sample template for a Product Browse Page. It demonstrates the usage of AMP components which works well in e-commerce websites. Examples include social sharing, image galleries, personalized content, ads, and more.

Additionally used AMP components must be imported in the header. Import amp-carousel to create a list of auto advancing products.

<script async custom-element="amp-carousel" src="https://cdn.ampproject.org/v0/amp-carousel-0.1.js"></script>

Import amp-list to implement auto suggest search

<script async custom-element="amp-list" src="https://cdn.ampproject.org/v0/amp-list-0.1.js"></script>

Import amp-mustache as a format template for amp-list

<script async custom-template="amp-mustache" src="https://cdn.ampproject.org/v0/amp-mustache-0.2.js"></script>

Import amp-form to implement auto suggest search

<script async custom-element="amp-form" src="https://cdn.ampproject.org/v0/amp-form-0.1.js"></script>

Import amp-analytics to monitor usage

<script async custom-element="amp-analytics" src="https://cdn.ampproject.org/v0/amp-analytics-0.1.js"></script>

Import amp-fit-text to fit text in the provided space

<script async custom-element="amp-fit-text" src="https://cdn.ampproject.org/v0/amp-fit-text-0.1.js"></script>

Import amp-sidebar to implement a menu

<script async custom-element="amp-sidebar" src="https://cdn.ampproject.org/v0/amp-sidebar-0.1.js"></script>

Import amp-bind to implement filtering and sorting

<script async custom-element="amp-bind" src="https://cdn.ampproject.org/v0/amp-bind-0.1.js"></script>

Import amp-lightbox to show the filtering and sorting section

<script async custom-element="amp-lightbox" src="https://cdn.ampproject.org/v0/amp-lightbox-0.1.js"></script>

Import amp-selector to implement auto suggest search

<script async custom-element="amp-selector" src="https://cdn.ampproject.org/v0/amp-selector-0.1.js"></script>

Use the amp-sidebar component to give customers the chance to quickly jump to any of your product categories

<amp-sidebar id="drawermenu" layout="nodisplay">
  <a href="/documentation/examples/e-commerce/product_browse_page">Products</a>
  <hr>
  <a href="#">Fruit</a>
  <a href="#">Vegetable</a>
  <a href="#">More</a>
</amp-sidebar>

AMP supports forms, which means you can directly integrate a product search into your AMPs. Here we implemented autocomplete, try searching for "Apple" and you should be able to select "Apple". Take a look at autosuggest sample for more details how autosuggest is implemented.

AMP doesn't support custom Javascript, but you can use CSS3 animations to enrich your page. The expanding text field when the search box is focused is implemented with CSS only.

<div class="header">
  <div class="menu-wrap">
    <a id="sample-menu" on="tap:drawermenu.toggle">
      <amp-img srcset="/static/samples/img/ic_menu_black_1x_web_24dp.png 1x, /static/samples/img/ic_menu_black_2x_web_24dp.png 2x" width="24" height="24" alt="navigation"></amp-img>
    </a>
    <input name="search" type="search" placeholder="Search" on="change:AMP.setState({
      products: {
      listSrc: '/documentation/examples/api/products?searchProduct='+ event.value +'&sort='+ products.sortChoiceValue +'&searchColor='+ products.searchColor +'&_=RANDOM',
      searchProduct: event.value
    }});
      input-debounced:
        AMP.setState({
          autosuggest: {
            query: event.value,
            showDropdown: event.value
          }
        }),
      autosuggest-list.show;
      tap:
        AMP.setState({
          autosuggest: {
            query: autosuggest.query == null ? '' : autosuggest.query,
            showDropdown: 'true'
          }
        }),
        autosuggest-list.show" [value]="autosuggest.query || ''" value>
<!--START_HINT_0-->
    <input type="submit" value>
<!--END_HINT-->
  </div>
  <div class="suggest">
      <div class="autosuggest-container" hidden [hidden]="!(autosuggest.showDropdown && autosuggest.query)">
        <amp-list class="autosuggest-box" layout="fixed-height" height="120" src="/documentation/examples/api/products-autosuggest" [src]="autosuggest.query ?
            autosuggest.endpoint + autosuggest.query :
            autosuggest.emptyAndInitialTemplateJson" id="autosuggest-list" binding="refresh">
          <template type="amp-mustache">
            <amp-selector id="autosuggest-selector" keyboard-select-mode="focus" layout="container" on="
                select:
                  AMP.setState({
                    autosuggest: {
                      query: event.targetOption,
                      showDropdown: false
                    },
                    products: {
                      listSrc: '/documentation/examples/api/products?searchProduct='+ event.targetOption +'&sort='+ products.sortChoiceValue +'&searchColor='+ products.searchColor +'&_=RANDOM',
                      searchProduct: event.targetOption
                    }
                  }),
                  autosuggest-list.hide">
              {{#results}}
                <div role="option" tabindex="0" on="tap:autosuggest-list.hide" option="{{.}}">{{.}}</div>
              {{/results}}
              {{^results}}
                <div class="{{#autosuggest.query}}empty{{/ autosuggest.query}}">
                  {{#autosuggest.query}}Sorry! We couldn't find the product 😰{{/autosuggest.query}}
                </div>
              {{/results}}
            </amp-selector>
          </template>
        </amp-list>
      </div>
    </div>
</div>

A hero slider

The amp-carousel component can be used to quickly create full-bleed, auto-advancing slideshows showcasing your latest promotions. We

use the amp-fit-text component for the slide headers to automatically size the text to fit into the carousel.

<amp-carousel id="hero-images" width="1024" height="480" layout="responsive" type="slides" autoplay>
  <a href="#">
    <amp-img src="/static/samples/img/product_hero1_1024x683.jpg" layout="fill" alt="product hero 1" attribution="visualhunt"></amp-img>

    <amp-fit-text class="caption" width="300" height="200" layout="responsive" max-font-size="36">
      The 10 best Apples
    </amp-fit-text>
  </a>
  <a href="#">
    <amp-img src="/static/samples/img/product_hero2_1024x683.jpg" layout="fill" alt="product hero 2" attribution="visualhunt"></amp-img>
    <amp-fit-text class="caption" width="300" height="200" layout="responsive" max-font-size="36">
      So much orange!
    </amp-fit-text>
  </a>
  <a href="#">
    <amp-img src="/static/samples/img/product_hero3_1024x683.jpg" layout="fill" alt="product hero 3" attribution="visualhunt"></amp-img>
    <amp-fit-text class="caption" width="300" height="200" layout="responsive" max-font-size="36">
      So healthy!
    </amp-fit-text>
  </a>
</amp-carousel>

Products configuration

The initial state and helper variables for the products are configured by amp-state.

<amp-state id="products">
  <script type="application/json">
{
        "sortChoiceValue": "price-descendent",
        "searchProduct": "",
        "searchColor": "all",
        "listSrc": "/documentation/examples/api/products?sort=price-descendent&searchColor=all&_=RANDOM"
      }
  </script>
</amp-state>

Filter and sorting

We implemented filtering and ordering by combining amp-bind and amp-list. For filtering, we update, via amp-bind, the src of amp-list based on the color selection.

Similarly, we use a select option to allow the user to sort products based on price and we then modify the src url of amp-list.

Filter results

Color

<div id="main-wrap">
  <div class="refine">
    <h3>Filter results</h3>
    <h4>Color</h4>
      <div><label><input type="radio" id="yellow" name="colorFilter" value="Yellow" on="change:AMP.setState({
        products: {
        searchColor: event.checked == true ? 'yellow' : '',
        listSrc: '/documentation/examples/api/products?searchProduct='+ products.searchProduct +'&sort='+ products.sortChoiceValue +'&searchColor='+ (event.checked == true ? 'yellow' : '') +'&_=RANDOM'
        }
        })"> yellow </label></div>
        <div><label><input type="radio" id="orange" name="colorFilter" value="Orange" on="change:AMP.setState({
        products: {
        searchColor: event.checked == true ? 'orange' : '',
        listSrc: '/documentation/examples/api/products?searchProduct='+ products.searchProduct +'&sort='+ products.sortChoiceValue +'&searchColor='+ (event.checked == true ? 'orange' : '') +'&_=RANDOM'
        }
        })"> orange </label></div>

        <div><label><input type="radio" id="green" name="colorFilter" value="Green" on="change:AMP.setState({
        products: {
        searchColor: event.checked == true ? 'green' : '',
        listSrc: '/documentation/examples/api/products?searchProduct='+ products.searchProduct +'&sort='+ products.sortChoiceValue +'&searchColor='+ (event.checked == true ? 'green' : '') +'&_=RANDOM'
        }
        })"> green </label></div>
        <div><label><input type="radio" id="all" name="colorFilter" value="all" on="change:AMP.setState({
        products: {
        searchColor: event.checked == true ? 'all' : '',
        listSrc: '/documentation/examples/api/products?searchProduct='+ products.searchProduct +'&sort='+ products.sortChoiceValue +'&searchColor='+ (event.checked == true ? 'all' : '') +'&_=RANDOM'
        }
        })"> all </label></div>
  </div>
  <div class="content">
    <div id="info-wrap">
      <div>
        <label for="sort">Sort</label>
        <select id="sort" on="change:AMP.setState({
        products: {
        sortChoiceValue: event.value,
        listSrc: '/documentation/examples/api/products?searchProduct='+products.searchProduct+'&sort='+event.value+'&searchColor='+products.searchColor+'&_=RANDOM'
        }
        })">
          <option value="price-descendent">Price high to low
          </option>
          <option value="price-ascendent">Price low to high
          </option>
        </select>
      </div>
      <button class="filter-mobile" on="tap:filter-lightbox">
        Filter
      </button>
    </div>
    <amp-list width="auto" height="600" layout="fixed-height" src="/documentation/examples/api/products?sort=price-descendent&searchColor=all&_=RANDOM'" [src]="products.listSrc" binding="no" class="grid" data-amp-replace="RANDOM">
      <template type="amp-mustache">
        <a class="products" href="#">
          <amp-img width="150" height="100" alt="{{name}}" src="{{img}}"></amp-img>
                 <p class="name">{{name}}</p>
                 <p class="star">{{{stars}}}</p>
                 <p class="price">${{price}}</p>
        </a>
      </template>
    </amp-list>
  </div>
</div>

We implemented an additional lightbox for the filter section on mobile.

<amp-lightbox id="filter-lightbox" layout="nodisplay">
  <div class="filter-section">
    <h3>Filter results</h3>
    <h4>Color</h4>
    <div>
      <label> yellow <input type="radio" name="colorFilter" value="Yellow" on="change:AMP.setState({
        products: {
        searchColor: event.checked == true ? 'yellow' : '',
        listSrc: '/documentation/examples/api/products?searchProduct='+ products.searchProduct +'&sort='+ products.sortChoiceValue +'&searchColor='+ (event.checked == true ? 'yellow' : '') +'&_=RANDOM'
        }
        }), filter-lightbox.close"></label>
      <label> orange <input type="radio" name="colorFilter" value="Orange" on="change:AMP.setState({
        products: {
        searchColor: event.checked == true ? 'orange' : '',
        listSrc: '/documentation/examples/api/products?searchProduct='+ products.searchProduct +'&sort='+ products.sortChoiceValue +'&searchColor='+ (event.checked == true ? 'orange' : '') +'&_=RANDOM'
        }
        }), filter-lightbox.close"></label>
      <label> green <input type="radio" name="colorFilter" value="Green" on="change:AMP.setState({
        products: {
        searchColor: event.checked == true ? 'green' : '',
        listSrc: '/documentation/examples/api/products?searchProduct='+ products.searchProduct +'&sort='+ products.sortChoiceValue +'&searchColor='+ (event.checked == true ? 'green' : '') +'&_=RANDOM'
        }
        }), filter-lightbox.close"> </label>
      <label> all <input type="radio" name="colorFilter" value="all" on="change:AMP.setState({
        products: {
        searchColor: event.checked == true ? 'all' : '',
        listSrc: '/documentation/examples/api/products?searchProduct='+ products.searchProduct +'&sort='+ products.sortChoiceValue +'&searchColor='+ (event.checked == true ? 'all' : '') +'&_=RANDOM'
        }
        }), filter-lightbox.close"> </label>
        </div>
    <div>
      <a on="tap:filter-lightbox.close" role="button" tabindex="0">
        Close
      </a>
    </div>
  </div>
</amp-lightbox>

Offers & highlights

The amp-carousel supports also a more traditional carousel mode.

<div class="recommendations">
  <h3>Recommendations</h3>
  <amp-carousel width="auto" height="160" layout="fixed-height" type="carousel">
    <a href="#" role="listitem">
      <amp-img width="640" height="426" layout="responsive" alt="Apple" src="/static/samples/img/product1_640x426.jpg"></amp-img>
      <p class="name">Apple</p>
      <p class="star">★★★★★</p>
      <p class="price">$1.99</p>
    </a>
    <a href="#" role="listitem">
      <amp-img width="640" height="426" layout="responsive" alt="Orange" src="/static/samples/img/product2_640x426.jpg"></amp-img>
      <p class="name">Orange</p>
      <p class="star">★★★★☆</p>
      <p class="price">$0.99</p>
    </a>
    <a href="#" role="listitem">
      <amp-img width="640" height="426" layout="responsive" alt="Pear" src="/static/samples/img/product3_640x426.jpg"></amp-img>
      <p class="name">Pear</p>
      <p class="star">★★★☆☆</p>
      <p class="price">$1.50</p>
    </a>
    <a href="#" role="listitem">
      <amp-img width="640" height="426" layout="responsive" alt="Banana" src="/static/samples/img/product4_640x426.jpg"></amp-img>
      <p class="name">Banana</p>
      <p class="star">★★★★★</p>
      <p class="price">$1.50</p>
    </a>
    <a href="#" role="listitem">
      <amp-img width="640" height="426" layout="responsive" alt="Apple" src="/static/samples/img/product1_640x426.jpg"></amp-img>
      <p class="name">Apple 2</p>
      <p class="star">★★★★★</p>
      <p class="price">$1.99</p>
    </a>
    <a href="#" role="listitem">
      <amp-img width="640" height="426" layout="responsive" alt="Orange" src="/static/samples/img/product2_640x426.jpg"></amp-img>
      <p class="name">Orange 2</p>
      <p class="star">★★★★☆</p>
      <p class="price">$0.99</p>
    </a>
    <a href="#" role="listitem">
      <amp-img width="640" height="426" layout="responsive" alt="Pear" src="/static/samples/img/product3_640x426.jpg"></amp-img>
      <p class="name">Pear 2</p>
      <p class="star">★★★☆☆</p>
      <p class="price">$1.50</p>
    </a>
    <a href="#" role="listitem">
      <amp-img width="640" height="426" layout="responsive" alt="Banana" src="/static/samples/img/product4_640x426.jpg"></amp-img>
      <p class="name">Banana 2</p>
      <p class="star">★★★★★</p>
      <p class="price">$1.50</p>
    </a>
  </amp-carousel>
</div>

Bestsellers

With AMP, you can easily pull in different latest offers or highlights without changing the page. To do so, just use amp-list to fire a CORS request to a JSON endpoint which supplies the list of related products. These are populated into an amp-mustache template on the client. Learn more about amp-list here

Bestsellers

<div class="bestsellers">
  <h3>Bestsellers</h3>
  <amp-list class="items" width="auto" height="160" layout="fixed-height" src="/static/samples/json/related_products.json" binding="no">
    <template type="amp-mustache">
      <a href="#">
        <amp-img width="640" height="426" layout="responsive" alt="{{name}}" src="{{img}}"></amp-img>
              <p class="name">{{name}}</p>
              <p class="star">{{{stars}}}</p>
              <p class="price">${{price}}</p>
      </a>
    </template>
  </amp-list>
</div>

Autosuggest configuration

The initial state and helper variables for the product autosuggest are configured by amp-state.

<amp-state id="autosuggest">
    <script type="application/json">
    {
      "endpoint": "/documentation/examples/api/products-autosuggest?q=",
      "emptyAndInitialTemplateJson": [{
        "query": "",
        "results": []
      }],
      "showDropdown":false,
      "query":""
    }
    </script>
</amp-state>

User analytics

Analytics must be configured in the body. Here we use Google Analytics to track pageviews.

<amp-analytics type="googleanalytics">
  <script type="application/json">
{
              "vars": {
                  "account": "UA-80609902-1"
              },
              "triggers": {
                  "default pageview": {
                      "on": "visible",
                      "request": "pageview",
                      "vars": {
                          "title": "{{title}}"
                      }
                  }
              }
          }

  </script>
</amp-analytics>
需要进一步说明?

如果此页面上的说明未能涵盖您的所有问题,欢迎与其他 AMP 用户取得联系,讨论您的具体用例。

前往 Stack Overflow
一项无法解释的功能?

AMP 项目强烈鼓励您参与并做出贡献!我们希望您能成为我们开放源代码社区的持续参与者,但我们也欢迎您对所热衷问题做出一次性贡献。

编辑 GitHub 上的示例