├── Block └── AmpProductBlock.php ├── Controller └── Index │ └── Index.php ├── LICENSE ├── NOTICE ├── README.md ├── composer.json ├── etc ├── frontend │ └── routes.xml └── module.xml ├── registration.php └── view └── frontend ├── templates └── index │ └── index.phtml └── web └── img ├── ic_menu_white_1x_web_24dp.png └── ic_menu_white_2x_web_24dp.png /Block/AmpProductBlock.php: -------------------------------------------------------------------------------- 1 | productRepo = $productRepo; 63 | $this->productHelper = $productHelper; 64 | $this->categoryModel = $categoryModel; 65 | $this->formKey = $formKey; 66 | $this->moduleReader = $moduleReader; 67 | parent::__construct( 68 | $context, 69 | $data 70 | ); 71 | } 72 | 73 | /** 74 | * Get product SKU from URL 75 | * Example: http://domain.com/amp/?sku=abc will retrieve the product with SKU 'abc' 76 | * 77 | * @return string 78 | */ 79 | public function getProductParam() 80 | { 81 | $sku = $_GET["sku"]; 82 | if ($sku === "") { 83 | throw new \Exception("Product SKU missing."); 84 | } 85 | 86 | $this->product = $this->productRepo->get($sku); 87 | if ($this->product === null) { 88 | throw new \Exception("Failed to fetch product with SKU '$sku'."); 89 | } 90 | } 91 | 92 | /** 93 | * Get product 94 | * 95 | * @return ProductInterface 96 | */ 97 | public function getProduct() 98 | { 99 | return $this->product; 100 | } 101 | 102 | /** 103 | * Product id 104 | * 105 | * @return int|null 106 | */ 107 | public function getProductId() 108 | { 109 | return $this->product->getId(); 110 | } 111 | 112 | /** 113 | * Product price (formatted number) 114 | * Example: float 1111.55555 becomes 1111.56 115 | * 116 | * @return string 117 | */ 118 | public function getProductPrice() 119 | { 120 | return number_format($this->product->getPrice(), 2, null, ''); 121 | } 122 | 123 | /** 124 | * Canonical URL to product 125 | * Reference: https://www.ampproject.org/docs/guides/discovery 126 | * 127 | * @return string|bool 128 | */ 129 | public function getProductCanonicalUrl() 130 | { 131 | return $this->productHelper->getProductUrl($this->product); 132 | } 133 | 134 | /** 135 | * Retrieve base image url 136 | * 137 | * @return string|bool 138 | */ 139 | public function getProductImageUrl() 140 | { 141 | return $this->productHelper->getImageUrl($this->product); 142 | } 143 | 144 | /** 145 | * Get basic information about all categories in an associative array: 146 | * int 'id', string 'name', string 'url', int 'level', array 'children' 147 | * 148 | * @return array 149 | */ 150 | private function getCategoriesInfo() 151 | { 152 | $category = $this->categoryModel; 153 | $tree = $category->getTreeModel()->load(); 154 | $ids = $tree->getCollection()->getAllIds(); 155 | 156 | $categoriesInfo = array(); 157 | $keys = array('id', 'name', 'url', 'level', 'children'); 158 | 159 | foreach ($ids as $id) { 160 | if ($id == \Magento\Catalog\Model\Category::TREE_ROOT_ID) { 161 | continue; 162 | } 163 | 164 | $category->load($id); 165 | $level = $category->getLevel(); 166 | 167 | $values = array(); 168 | $values[] = $category->getId(); 169 | $values[] = $category->getName(); 170 | $values[] = $level > 1 ? $category->getCategoryIdUrl() : ""; 171 | $values[] = $level; 172 | 173 | // Why does getAllChildren() include the id of self? 174 | $children = $category->getAllChildren(true); 175 | unset($children[array_search($category->getId(), $children)]); 176 | $values[] = $children; 177 | 178 | $categoriesInfo[] = array_combine($keys, $values); 179 | } 180 | 181 | return $categoriesInfo; 182 | } 183 | 184 | /** 185 | * Generate AMP HTML markup for all children categories of a parent category 186 | * 187 | * @param array $categoriesInfo 188 | * @param array $children 189 | * @param int $level 190 | * @return array 191 | */ 192 | private function generateChildCategoriesHTML($categoriesInfo, $children, $level) 193 | { 194 | if (!$children) { 195 | return ""; 196 | } 197 | 198 | $html = ""; 199 | 200 | foreach ($categoriesInfo as $categoryInfo) { 201 | if (in_array($categoryInfo['id'], $children) && $categoryInfo['level'] == $level) { 202 | $html .= '
272 | escapeHtml(strip_tags($product->getShortDescription())) ?> 273 |
274 | 275 | 276 |305 | escapeHtml(strip_tags($product->getDescription())) ?> 306 |
307 | 308 | 309 |310 | $getProductPrice() ?> 311 |
312 | 313 | 314 | 332 | 333 | 334 | 352 | 353 | 354 |
356 | Copyright 2017 WompMobile, Inc.
357 | AMP Product Page Sample used as reference.
358 |