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

Feature/seo structured data #78

Merged
merged 3 commits into from
Apr 5, 2021
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
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 %}
"{{ url('homepage') }}{{ asset(img, 'save_image') }}" {% if not loop.last %},{% endif %}

{% else %}
"{{ url('homepage') }}{{ asset(no_image_product, 'save_image') }}"
{% endfor %}
],
"description": "{{ Product.description_list | default(Product.description_detail) | striptags | replace({'\n': '', '\r': ''}) }}",
"sku": "{{ Product.code_min }}",
"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 @@ -258,6 +258,33 @@ public function testProductFavoriteAddThroughLogin()
$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);
}

/**
* 一覧ページ metaタグのテスト
*/
Expand Down