Skip to content

Commit

Permalink
Merge pull request #4986 from matsuoshi/feature/seo-structured-data
Browse files Browse the repository at this point in the history
商品詳細ページに構造化データを追加
  • Loading branch information
chihiro-adachi authored Jul 2, 2021
2 parents 60f8669 + b46b162 commit 48c6ca6
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/Eccube/Resource/template/default/Product/detail.twig
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,32 @@ file that was distributed with this source code.
$('.ec-modal').hide()
});
</script>
<script type="application/ld+json">
{
"@context": "https://schema.org/",
"@type": "Product",
"name": "{{ Product.name }}",
"image": [
{% for img in Product.ProductImage %}
"{{ app.request.schemeAndHttpHost }}{{ asset(img, 'save_image') }}"{% if not loop.last %},{% endif %}
{% else %}
"{{ app.request.schemeAndHttpHost }}{{ asset(''|no_image_product, 'save_image') }}"
{% endfor %}
],
"description": "{{ Product.description_list | default(Product.description_detail) | replace({'\n': '', '\r': ''}) | slice(0,300) }}",
{% if Product.code_min %}
"sku": "{{ Product.code_min }}",
{% endif %}
"offers": {
"@type": "Offer",
"url": "{{ url('product_detail', {'id': Product.id}) }}",
"priceCurrency": "{{ eccube_config.currency }}",
"price": {{ Product.getPrice02IncTaxMin }},
"availability": "{{ Product.stock_find ? "InStock" : "OutOfStock" }}"
}
}
</script>
{% endblock %}

{% block main %}
Expand Down
27 changes: 27 additions & 0 deletions tests/Eccube/Tests/Web/ProductControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,31 @@ public function testProductFavoriteAddThroughLogin()
$html = $crawler->filter('div.ec-productRole__profile')->html();
$this->assertContains('お気に入りに追加済です', $html);
}

/**
* 商品詳細ページの構造化データ
*/
public function testProductStructureData()
{
$crawler = $this->client->request('GET', $this->generateUrl('product_detail', ['id' => 2]));
$json = json_decode(html_entity_decode($crawler->filter('script[type="application/ld+json"]')->html()));
$this->assertEquals('Product', $json->{'@type'});
$this->assertEquals('チェリーアイスサンド', $json->name);
$this->assertEquals(3080, $json->offers->price);
$this->assertEquals('InStock', $json->offers->availability);

// 在庫なし商品のテスト
$Product = $this->createProduct('Product no stock', 1);
$ProductClass = $Product->getProductClasses()->first();
$ProductClass->setStockUnlimited(false);
$ProductClass->setStock(0);
$ProductStock = $ProductClass->getProductStock();
$ProductStock->setStock(0);
$this->entityManager->flush();

$crawler = $this->client->request('GET', $this->generateUrl('product_detail', ['id' => $Product->getId()]));
$json = json_decode(html_entity_decode($crawler->filter('script[type="application/ld+json"]')->html()));
$this->assertEquals('Product no stock', $json->name);
$this->assertEquals('OutOfStock', $json->offers->availability);
}
}

0 comments on commit 48c6ca6

Please sign in to comment.