Skip to content

Commit

Permalink
meta noindex 対応 (検索結果0件ページ、在庫切れ商品ページ)
Browse files Browse the repository at this point in the history
  • Loading branch information
h.matsuo committed Apr 20, 2021
1 parent cca46a5 commit 6ebb726
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Eccube/Resource/template/default/meta.twig
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@
<meta property="product:price:currency" content="{{ eccube_config.currency }}"/>
<meta property="product:product_link" content="{{ url('product_detail', {'id': Product.id}) }}"/>
<meta property="product:retailer_title" content="{{ BaseInfo.shop_name }}"/>
{% if not Product.stock_find %}
<meta name="robots" content="noindex">
{% endif %}
{% elseif Category is defined %}
{% set meta_og_type = 'article' %}
{% set meta_description = Page.description %}
{% set meta_canonical = url('product_list', {'category_id': Category.id|default(null)}) %}
{% if pagination|length == 0 %}
<meta name="robots" content="noindex">
{% endif %}
{% elseif Page is defined %}
{% set meta_og_type = (app.request.get('_route') == 'homepage') ? 'website' : 'article' %}
{% set meta_description = Page.description %}
Expand Down
30 changes: 30 additions & 0 deletions tests/Eccube/Tests/Web/ProductControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,18 @@ public function testMetaTagsInListPage()
$this->assertEquals('article', $crawler->filter('meta[property="og:type"]')->attr('content'));
$this->assertEquals($url, $crawler->filter('link[rel="canonical"]')->attr('href'));
$this->assertEquals($url, $crawler->filter('meta[property="og:url"]')->attr('content'));
$this->assertCount(0, $crawler->filter('meta[name="robots"]'));

// カテゴリ指定あり
$url = $this->generateUrl('product_list', ['category_id' => 1], UrlGeneratorInterface::ABSOLUTE_URL);
$crawler = $this->client->request('GET', $url);
$this->assertEquals($url, $crawler->filter('link[rel="canonical"]')->attr('href'));

// 検索 0件 → noindex 確認
$url = $this->generateUrl('product_list', ['category_id' => 1, 'name' => 'notfoundquery'], UrlGeneratorInterface::ABSOLUTE_URL);
$crawler = $this->client->request('GET', $url);
$this->assertContains('お探しの商品は見つかりませんでした', $crawler->html());
$this->assertEquals('noindex', $crawler->filter('meta[name="robots"]')->attr('content'));
}

/**
Expand Down Expand Up @@ -303,6 +310,7 @@ public function testMetaTagsInDetailPage()
$this->assertEquals($url, $crawler->filter('link[rel="canonical"]')->attr('href'));
$this->assertEquals($url, $crawler->filter('meta[property="og:url"]')->attr('content'));
$this->assertEquals($imgPath, $crawler->filter('meta[property="og:image"]')->attr('content'));
$this->assertCount(0, $crawler->filter('meta[name="robots"]'));

// 商品の description_list を削除
// → meta description には description_detail が設定される
Expand All @@ -315,4 +323,26 @@ public function testMetaTagsInDetailPage()
$this->assertEquals($expected_desc, $crawler->filter('meta[name="description"]')->attr('content'));
$this->assertEquals($expected_desc, $crawler->filter('meta[property="og:description"]')->attr('content'));
}

/**
* 詳細ページ 在庫なし時の metaタグのテスト
*/
public function testMetaTagsInOutOfStockDetailPage()
{
$Product = $this->createProduct('Product out of stock', 1);
$id = $Product->getId();
$productUrl = $this->generateUrl('product_detail', ['id' => $id], UrlGeneratorInterface::ABSOLUTE_URL);

// 在庫切れ商品
$ProductClass = $Product->getProductClasses()->first();
$ProductClass->setStockUnlimited(false);
$ProductClass->setStock(0);
$ProductStock = $ProductClass->getProductStock();
$ProductStock->setStock(0);
$this->entityManager->flush();

$crawler = $this->client->request('GET', $productUrl);

$this->assertEquals('noindex', $crawler->filter('meta[name="robots"]')->attr('content'));
}
}

0 comments on commit 6ebb726

Please sign in to comment.