Developer forum

Forum » Ecommerce - Standard features » Json feed output for app "Product catalog for ViewModel".

Json feed output for app "Product catalog for ViewModel".

Simon Nordahl
Simon Nordahl
Reply

Hi,

We have started using the new Product catalog with viewmodels and it works great, but we are missing the JSON-feed-feature from the standard Ecom catalog.

Being able to output the content of an APP as JSON is an awesome feature that allows async rendering and it's sad to see it missing from the new ecom with viewmodels.

Are there plans for having this implemented? Currently we making by, by "hacking" the output through an observer, but I'm wondering if you have a better alternative?

    [Subscribe(Dynamicweb.Ecommerce.Notifications.Ecommerce.ProductCatalog.OnBeforeContent)]
    [Subscribe(Standard.Page.OnOutput)]
    public class ProductPageFeedObserver : NotificationSubscriber
    {
        private const string IsJsonFeedParameter = "json";
        private const string ShouldRender = "ShouldRender";

        private static readonly HashSet<string> FeedCompatibleModuleSystemNames = new HashSet<string>
        {
            "eCom_ProductCatalog"
        };

        public override void OnNotify(string notification, NotificationArgs args)
        {
            if (!IsJsonFeed())
                return;

            SkipNormalRenderingForFeed(args);
            OverrideOutputWithJsonFeed(args);
        }

        private void SkipNormalRenderingForFeed(NotificationArgs args)
        {
            if (args is Dynamicweb.Ecommerce.Notifications.Ecommerce.ProductCatalog.OnBeforeContentArgs beforeContentArgs)
            {
                beforeContentArgs.StopExecution = !Converter.ToBoolean(Dynamicweb.Context.Current.Items[ShouldRender]);
            }
        }

        private void OverrideOutputWithJsonFeed(NotificationArgs args)
        {
            if (args is Standard.Page.OnOutputArgs arg)
            {
                var moduleOutput = RenderEcomCatalogOutput();
                if (!string.IsNullOrEmpty(moduleOutput))
                {
                    arg.PageViewTemplate.Html = moduleOutput;
                    Dynamicweb.Context.Current.Response.ContentType = "application/json";
                }
            }
        }

        public static bool IsJsonFeed()
        {
            return Dynamicweb.Context.Current.Request.GetBoolean(IsJsonFeedParameter);
        }

        private string RenderEcomCatalogOutput()
        {
            var paragraphService = ObjectFactory.GetInstance<ParagraphService>();
            var paragraphs = paragraphService.GetParagraphsByPageId(PageView.Current().Page.ID);
            foreach (var paragraph in paragraphs)
            {
                if (FeedCompatibleModuleSystemNames.Contains(paragraph.ModuleSystemName))
                {
                    Dynamicweb.Context.Current.Items[ShouldRender] = true;
                    return Content.GetModuleOutput(paragraph, PageView.Current());
                }
            }
            return null;
        }

 

 

 


Replies

 
Nicolai Pedersen
Reply

Hi Simon

The features of the viewmodel based catalog is also available as a built-in web-api - you should be able to use that. Take a look at this one:

https://doc.dynamicweb.com/extensibility/web-apis/ecommerce-web-api

BR Nicolai

 
Simon Nordahl
Simon Nordahl
Reply

Hi Nicolai,

Thanks for the quick reply.

WebApi is the future for sure, but since the api is stateless i would have to send a list of productids and userid/customer numbers through the frontend channel.

For now it would be awesome to invoke a repository query for a given user and have that response sent back as json :)

 

Regards

 

 
Nicolai Pedersen
Reply

Was that a question? In that case I do not understand it :-)

 

You must be logged in to post in the forum