Add Schema.org product markup and change title of details pages
authorMarius Gavrilescu <marius@ieval.ro>
Tue, 3 May 2016 16:08:58 +0000 (17:08 +0100)
committerMarius Gavrilescu <marius@ieval.ro>
Tue, 3 May 2016 16:08:58 +0000 (17:08 +0100)
lib/App/Web/Oof.pm
tmpl/details.html

index dffaaa57136a65d91d55151eb090fc75e2b2c775..fa256a6965411da2ec9fca71097846d51960f8c0 100644 (file)
@@ -62,6 +62,25 @@ sub make_slug {
        $slug
 }
 
+sub product_to_schemaorg {
+       my ($include_url, %data) = @_;
+       my $stock = $data{stock} > 0 ? 'InStock' : 'OutOfStock';
+       +{
+               '@context' => 'http://schema.org/',
+               '@type'    => 'Product',
+               name => $data{title},
+               image => "/static/fullpics/$data{product}-1.jpg",
+               description => $data{summary},
+               offers => {
+                       '@type' => 'Offer',
+                       price => ($data{price} =~ s/(..)$/\.$1/r),
+                       priceCurrency => 'GBP',
+                       availability => "http://schema.org/$stock",
+                       ($include_url ? (url => "/details/$data{product}/" . make_slug $data{title}) : ())
+               }
+       }
+}
+
 our %highlight;
 sub form_table_row {
        my ($data, $tr) = @_;
@@ -230,14 +249,15 @@ sub details_app {
        $db{$$} //= DBIx::Simple->connect($ENV{OOF_DSN} // 'dbi:Pg:');
        my $tree = $details->clone;
        my ($id) = $env->{PATH_INFO} =~ m,^/(\d+),;
-       my ($title, $summary) = $db{$$}->select(products => [qw/title summary/], {product => $id})->list;
+       my %data = %{$db{$$}->select(products => '*', {product => $id})->hash};
        my @pics = <static/fullpics/$id-*>;
-       my $slug = make_slug $title;
-       $tree->find('title')->replace_content("Pictures of $title | ledparts4you");
-       $tree->find('h2')->replace_content($title);
-       $tree->fid('summary')->replace_content($summary);
+       my $slug = make_slug $data{title};
+       $tree->find('title')->replace_content("$data{title} | ledparts4you");
+       $tree->find('h2')->replace_content($data{title});
+       $tree->fid('summary')->replace_content($data{summary});
        $tree->look_down(rel => 'canonical')->attr(href => "/details/$id/$slug");
        $tree->fid('pictures')->find('li')->iter3(\@pics, \&details_list_element);
+       $tree->fid('jsonld')->replace_content(encode_json product_to_schemaorg '', %data);
 
        for my $ahref ($tree->find('a')) {
                $ahref->attr(href => "/form?highlight=$id") if $ahref->attr('href') eq '/';
index ec99046a5b899ae69194c3bbbe83ea287b6385ce..aa9b4cd3d20d6a698d18f065e243262ca342b0fd 100644 (file)
@@ -2,7 +2,10 @@
 <meta charset="utf-8">
 <link rel="stylesheet" href="/static/style.css">
 <link rel="canonical" href="https://example.org">
-<title>Pictures of an item | ledparts4you</title>
+<title>Item | ledparts4you</title>
+
+<script id="jsonld" type="application/ld+json">
+</script>
 
 <h1 id="title">ledparts4you</h1>
 <div id="subtitle">Issues/Questions? Contact us at <a href="mailto:orders@ledparts4you.uk.to">orders@ledparts4you.uk.to</a></div>
This page took 0.01314 seconds and 4 git commands to generate.