Developer forum

Forum » Swift » Swift v1 - og:image tag for single product page

Swift v1 - og:image tag for single product page

Didzis Kuzmans
Reply

Hello

We have noticed that we don't have og:image tag for products in Swift websites.

In Swift files, there are some code in Swift\eCom\ProductCatalog\ProductViewDetail.cshtml:

Pageview.Meta.AddTag($"<meta property=\"og:image\" content=\"{Dynamicweb.Context.Current.Request.Url.Scheme}://{Dynamicweb.Context.Current.Request.Url.Host}{Model.DefaultImage.Value}\">");

But it seems like this code have no impact on end result in product details page.

Is there simple fix for it or maybe I am missing something ?


Replies

 
Jon Thorne
Jon Thorne
Reply

I have found the same issue. And also noticed this has been removed from Swift v2 templates.

Anyone have a solution for how to add the product specific tags?

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

The ProductViewDetail.cshtml template does not exist on Swift2.

But in any template you can do like this (This is what we do on the master template):

Pageview.Meta.AddOpenGraphTag("type", "website");
Pageview.Meta.AddOpenGraphTag("url", Pageview.Meta.Canonical?.ToString()); 
Pageview.Meta.AddOpenGraphTag("title", Model.Title);
Pageview.Meta.AddOpenGraphTag("description", Model.Description);

if (Model.Area?.Item?.TryGetImageFile("MetaImage", out ImageFileViewModel? image) ?? false) {
    Pageview.Meta.AddOpenGraphTag("image", image.Path);
    Pageview.Meta.AddOpenGraphTag("image:alt", Model.Area?.Item?.GetString("MetaImageALT"));
}

What we have planned is that both a page, group and product can have an image related to it - and if it is, it will automatically be added to the open graph.

But for now you have to do the above.

BR Nicolai

 
Jon Thorne
Jon Thorne
Reply

I tried to use that code in the Swift Master in v1. But the Model.Description is empty as is the MetaImage. Perhaps there is some more configuration needed for that?

As a workaround I did this instead in the master template.

if (!string.IsNullOrEmpty(Dynamicweb.Context.Current.Request.QueryString["ProductID"]))
{
ProductService ps = new ProductService();
Product? product = ps.GetProductById(Dynamicweb.Context.Current.Request.QueryString["ProductID"],"","LANG1");
if( product != null )
{
string description = Regex.Replace(product.LongDescription, "<.*?>", string.Empty);
if (description.Length > 200)
{
description = description.Substring(0, 200).TrimEnd() + "...";
}
Pageview.Meta.AddOpenGraphTag("description", $"{description}");
 
ProductImageService productImageService = new ProductImageService();
string productImagePath = productImageService.GetImagePath(product);
Pageview.Meta.AddOpenGraphTag("image", $"{productImagePath}");
 }
}
 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

That was just sample code - model there was the Pageviewmodel - and they do not exist in ProductViewmodel yet and does not have the values.

In what template do you add this? What is your model?

Be careful to use new *service() - the service instances are accessible from Dynamicweb.Ecommerce.Service

 
Jon Thorne
Jon Thorne
Reply

I need it on the product detail page. But the Pageview.Meta.AddOpenGraphTag or the Pageview.Meta.AddTag does not seem to work in those templates at all, so I can only put it in the page master template for now.

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

Hi Jon

The pageview is available on all templates - but you might be missing the using directive to Dynamicweb.Frontend.

In Swift 2 you can do like this:

ProductDetailRenderGrid.cshtml

Which will produce this:

 

@inherits ViewModelTemplate<ProductViewModel>
@using Dynamicweb.Rendering
@using Dynamicweb.Frontend
@using Dynamicweb.Ecommerce.ProductCatalog

@{
 /* These can be left out as they are handled automatically if this code is in the master template
    Pageview.Meta.AddOpenGraphTag("type", "website");
    Pageview.Meta.AddOpenGraphTag("url", Pageview.Meta.Canonical?.ToString());
    Pageview.Meta.AddOpenGraphTag("title", Model.MetaTitle);
    Pageview.Meta.AddOpenGraphTag("description", Model.MetaDescription);
 */

    if (Model.DefaultImage.TryGetImageFileViewModel(out ImageFileViewModel? image)) {
        Pageview.Meta.AddOpenGraphTag("image", image.Path);
        Pageview.Meta.AddOpenGraphTag("image:alt", Model.Name);
    }
}

@{
 if (Dynamicweb.Context.Current?.Items.Contains("ProductDetails") ?? false)
 {
 Dynamicweb.Context.Current.Items["ProductDetails"] = Model;
 }
 else
 {
 Dynamicweb.Context.Current?.Items.Add("ProductDetails", Model);
 }
 var detailPage = GetPageIdByNavigationTag("ProductDetailPage");
}

@RenderGrid(Model.PrimaryOrDefaultGroup.PrimaryPageId > 0 ? Model.PrimaryOrDefaultGroup.PrimaryPageId : detailPage)
 
Christoffer Rosendahl Frede
Reply
Hi Jon.
We got it working in swift 2.
we get the products default image from the ProductViewModel with: pagePvViewModel = (ProductViewModel)Dynamicweb.Context.Current.Items["ProductDetails"];
here is the code we use in the template: /Swift-v2/eCom/ProductCatalog/ProductDetailRenderGrid.cshtml
ProductViewModel pagePvViewModel = null;
    string pvDefaultImagePath = "";
    if (Dynamicweb.Context.Current?.Items.Contains("ProductDetails") ?? false) {
        pagePvViewModel = (ProductViewModel)Dynamicweb.Context.Current.Items["ProductDetails"];
        pvDefaultImagePath = pagePvViewModel?.DefaultImage?.Value ?? "";
    }
    if (!string.IsNullOrWhiteSpace(pvDefaultImagePath)) {
    var imageUrl = MakeAbsoluteUrl(pvDefaultImagePath);
    Pageview?.Meta.AddOpenGraphTag("image", imageUrl);
    }
 
/best regards Christoffer

 

You must be logged in to post in the forum