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 all 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
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 }}",
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 は構造化データの仕様では「任意」でしたので、未設定の場合は出力しないよう変更しました

{% 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);
}
}