Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

商品詳細ページに構造化データを追加 #4986

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 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,30 @@ 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') }}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

画像のURLが以下のように出力されてました。
http://127.0.0.1:8080/html/upload/save_image/

no_image_productはtwigフィルタなので、以下のように渡してください
{{ asset(''|no_image_product, 'save_image') }}

{% endfor %}
],
"description": "{{ Product.description_list | default(Product.description_detail) | striptags | replace({'\n': '', '\r': ''}) }}",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

descriptionは文字長の制限はありますか?
制限あれば文字長内に丸めるなどしたほうがよいかもしれません。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

descriptionは文字長の制限はありますか?

仕様上の上限が見当たりませんでしたが、確かに全文出す必要はないと思いますので metaタグのdescription などで一般的な 300文字に丸めます

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

いくつかの文字種が期待通りに出力されないかもです。

'あああ' -> &#039;あああ&#039;
"あああ" -> &quot;あああ&quot;
<今ならお買い得!> ->

そこまで厳密にする必要はないかもしれませんがいちおう。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

' " については、テストツールで検証したところ正しく解析されていたようのですので、このままにします。
https://search.google.com/test/rich-results
<> について、striptags は副作用ありそうなので削除しました。(description内の不等号はエスケープされて出力される形)

"sku": "{{ Product.code_min }}",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

商品コードは未設定の場合がありますが、skuは空でも問題ないですか?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sku は構造化データの仕様では「任意」でしたので、未設定の場合は出力しないよう変更しました

"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);
}
}